1--TEST--
2ReflectionParameter::get/hasType and ReflectionType tests
3--FILE--
4<?php
5function foo(stdClass $a, array $b, callable $c, stdClass $d = null, $e = null, string $f, bool $g, int $h, float $i, NotExisting $j) { }
6
7function bar(): stdClass { return new stdClass; }
8
9class c extends stdClass {
10  function bar(self $x): int { return 1; }
11  function pbar(parent $x): int { return 1; }
12  function factory(): self { return new c; }
13  function pfactory(): parent { return new stdClass; }
14}
15
16$closure = function (Test $a): Test { return $a; };
17
18echo "*** functions\n";
19
20foreach ([
21  new ReflectionFunction('foo'),
22  new ReflectionFunction($closure),
23] as $idx => $rf) {
24  foreach ($rf->getParameters() as $idx2 => $rp) {
25    echo "** Function $idx - Parameter $idx2\n";
26    var_dump($rp->hasType());
27    $ra = $rp->getType();
28    if ($ra) {
29      var_dump($ra->allowsNull());
30      var_dump($ra->isBuiltin());
31      var_dump($ra->getName());
32    }
33  }
34}
35
36echo "\n*** methods\n";
37
38foreach ([
39  new ReflectionMethod('SplObserver', 'update'),
40  new ReflectionMethod('c', 'bar'),
41  new ReflectionMethod('c', 'pbar'),
42  new ReflectionMethod($closure, '__invoke'),
43] as $idx => $rm) {
44  foreach ($rm->getParameters() as $idx2 => $rp) {
45    echo "** Method $idx - parameter $idx2\n";
46    var_dump($rp->hasType());
47    $ra = $rp->getType();
48    if ($ra) {
49      var_dump($ra->allowsNull());
50      var_dump($ra->isBuiltin());
51      var_dump($ra->getName());
52    }
53  }
54}
55
56echo "\n*** return types\n";
57
58foreach ([
59  new ReflectionMethod('SplObserver', 'update'),
60  new ReflectionFunction('bar'),
61  new ReflectionMethod('c', 'bar'),
62  new ReflectionMethod('c', 'factory'),
63  new ReflectionMethod('c', 'pfactory'),
64  new ReflectionFunction($closure),
65  new ReflectionMethod($closure, '__invoke'),
66] as $idx => $rf) {
67  echo "** Function/method return type $idx\n";
68  var_dump($rf->hasReturnType());
69  $ra = $rf->getReturnType();
70  if ($ra) {
71    var_dump($ra->allowsNull());
72    var_dump($ra->isBuiltin());
73    var_dump($ra->getName());
74  }
75}
76
77?>
78--EXPECT--
79*** functions
80** Function 0 - Parameter 0
81bool(true)
82bool(false)
83bool(false)
84string(8) "stdClass"
85** Function 0 - Parameter 1
86bool(true)
87bool(false)
88bool(true)
89string(5) "array"
90** Function 0 - Parameter 2
91bool(true)
92bool(false)
93bool(true)
94string(8) "callable"
95** Function 0 - Parameter 3
96bool(true)
97bool(true)
98bool(false)
99string(8) "stdClass"
100** Function 0 - Parameter 4
101bool(false)
102** Function 0 - Parameter 5
103bool(true)
104bool(false)
105bool(true)
106string(6) "string"
107** Function 0 - Parameter 6
108bool(true)
109bool(false)
110bool(true)
111string(4) "bool"
112** Function 0 - Parameter 7
113bool(true)
114bool(false)
115bool(true)
116string(3) "int"
117** Function 0 - Parameter 8
118bool(true)
119bool(false)
120bool(true)
121string(5) "float"
122** Function 0 - Parameter 9
123bool(true)
124bool(false)
125bool(false)
126string(11) "NotExisting"
127** Function 1 - Parameter 0
128bool(true)
129bool(false)
130bool(false)
131string(4) "Test"
132
133*** methods
134** Method 0 - parameter 0
135bool(true)
136bool(false)
137bool(false)
138string(10) "SplSubject"
139** Method 1 - parameter 0
140bool(true)
141bool(false)
142bool(false)
143string(4) "self"
144** Method 2 - parameter 0
145bool(true)
146bool(false)
147bool(false)
148string(6) "parent"
149** Method 3 - parameter 0
150bool(true)
151bool(false)
152bool(false)
153string(4) "Test"
154
155*** return types
156** Function/method return type 0
157bool(false)
158** Function/method return type 1
159bool(true)
160bool(false)
161bool(false)
162string(8) "stdClass"
163** Function/method return type 2
164bool(true)
165bool(false)
166bool(true)
167string(3) "int"
168** Function/method return type 3
169bool(true)
170bool(false)
171bool(false)
172string(4) "self"
173** Function/method return type 4
174bool(true)
175bool(false)
176bool(false)
177string(6) "parent"
178** Function/method return type 5
179bool(true)
180bool(false)
181bool(false)
182string(4) "Test"
183** Function/method return type 6
184bool(true)
185bool(false)
186bool(false)
187string(4) "Test"
188