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((string)$ra);
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((string)$ra);
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((string)$ra);
74  }
75}
76--EXPECT--
77*** functions
78** Function 0 - Parameter 0
79bool(true)
80bool(false)
81bool(false)
82string(8) "stdClass"
83** Function 0 - Parameter 1
84bool(true)
85bool(false)
86bool(true)
87string(5) "array"
88** Function 0 - Parameter 2
89bool(true)
90bool(false)
91bool(true)
92string(8) "callable"
93** Function 0 - Parameter 3
94bool(true)
95bool(true)
96bool(false)
97string(8) "stdClass"
98** Function 0 - Parameter 4
99bool(false)
100** Function 0 - Parameter 5
101bool(true)
102bool(false)
103bool(true)
104string(6) "string"
105** Function 0 - Parameter 6
106bool(true)
107bool(false)
108bool(true)
109string(4) "bool"
110** Function 0 - Parameter 7
111bool(true)
112bool(false)
113bool(true)
114string(3) "int"
115** Function 0 - Parameter 8
116bool(true)
117bool(false)
118bool(true)
119string(5) "float"
120** Function 0 - Parameter 9
121bool(true)
122bool(false)
123bool(false)
124string(11) "NotExisting"
125** Function 1 - Parameter 0
126bool(true)
127bool(false)
128bool(false)
129string(4) "Test"
130
131*** methods
132** Method 0 - parameter 0
133bool(true)
134bool(false)
135bool(false)
136string(10) "SplSubject"
137** Method 1 - parameter 0
138bool(true)
139bool(false)
140bool(false)
141string(4) "self"
142** Method 2 - parameter 0
143bool(true)
144bool(false)
145bool(false)
146string(6) "parent"
147** Method 3 - parameter 0
148bool(true)
149bool(false)
150bool(false)
151string(4) "Test"
152
153*** return types
154** Function/method return type 0
155bool(false)
156** Function/method return type 1
157bool(true)
158bool(false)
159bool(false)
160string(8) "stdClass"
161** Function/method return type 2
162bool(true)
163bool(false)
164bool(true)
165string(3) "int"
166** Function/method return type 3
167bool(true)
168bool(false)
169bool(false)
170string(4) "self"
171** Function/method return type 4
172bool(true)
173bool(false)
174bool(false)
175string(6) "parent"
176** Function/method return type 5
177bool(true)
178bool(false)
179bool(false)
180string(4) "Test"
181** Function/method return type 6
182bool(true)
183bool(false)
184bool(false)
185string(4) "Test"
186