Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit efeb2dc

Browse filesBrowse files
committed
[VarDumper] Fix display of nullable union return types.
1 parent 018e33a commit efeb2dc
Copy full SHA for efeb2dc

File tree

Expand file treeCollapse file tree

2 files changed

+129
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+129
-1
lines changed

‎src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
183183
if (isset($a[$prefix.'returnType'])) {
184184
$v = $a[$prefix.'returnType'];
185185
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
186-
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
186+
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
187187
}
188188
if (isset($a[$prefix.'class'])) {
189189
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);

‎src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
+128Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,68 @@ public function testReflectionParameterScalar()
149149
);
150150
}
151151

152+
/**
153+
* @requires PHP 8
154+
*/
155+
public function testReflectionParameterMixed()
156+
{
157+
$f = eval('return function (mixed $a) {};');
158+
$var = new \ReflectionParameter($f, 0);
159+
160+
$this->assertDumpMatchesFormat(
161+
<<<'EOTXT'
162+
ReflectionParameter {
163+
+name: "a"
164+
position: 0
165+
allowsNull: true
166+
typeHint: "mixed"
167+
}
168+
EOTXT
169+
, $var
170+
);
171+
}
172+
173+
/**
174+
* @requires PHP 8
175+
*/
176+
public function testReflectionParameterUnion()
177+
{
178+
$f = eval('return function (int|float $a) {};');
179+
$var = new \ReflectionParameter($f, 0);
180+
181+
$this->assertDumpMatchesFormat(
182+
<<<'EOTXT'
183+
ReflectionParameter {
184+
+name: "a"
185+
position: 0
186+
typeHint: "int|float"
187+
}
188+
EOTXT
189+
, $var
190+
);
191+
}
192+
193+
/**
194+
* @requires PHP 8
195+
*/
196+
public function testReflectionParameterNullableUnion()
197+
{
198+
$f = eval('return function (int|float|null $a) {};');
199+
$var = new \ReflectionParameter($f, 0);
200+
201+
$this->assertDumpMatchesFormat(
202+
<<<'EOTXT'
203+
ReflectionParameter {
204+
+name: "a"
205+
position: 0
206+
allowsNull: true
207+
typeHint: "int|float|null"
208+
}
209+
EOTXT
210+
, $var
211+
);
212+
}
213+
152214
public function testReturnType()
153215
{
154216
$f = eval('return function ():int {};');
@@ -168,6 +230,72 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
168230
);
169231
}
170232

233+
/**
234+
* @requires PHP 8
235+
*/
236+
public function testMixedReturnType()
237+
{
238+
$f = eval('return function (): mixed {};');
239+
$line = __LINE__ - 1;
240+
241+
$this->assertDumpMatchesFormat(
242+
<<<EOTXT
243+
Closure(): mixed {
244+
returnType: "mixed"
245+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
246+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
247+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
248+
line: "1 to 1"
249+
}
250+
EOTXT
251+
, $f
252+
);
253+
}
254+
255+
/**
256+
* @requires PHP 8
257+
*/
258+
public function testUnionReturnType()
259+
{
260+
$f = eval('return function (): int|float {};');
261+
$line = __LINE__ - 1;
262+
263+
$this->assertDumpMatchesFormat(
264+
<<<EOTXT
265+
Closure(): int|float {
266+
returnType: "int|float"
267+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
268+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
269+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
270+
line: "1 to 1"
271+
}
272+
EOTXT
273+
, $f
274+
);
275+
}
276+
277+
/**
278+
* @requires PHP 8
279+
*/
280+
public function testNullableUnionReturnType()
281+
{
282+
$f = eval('return function (): int|float|null {};');
283+
$line = __LINE__ - 1;
284+
285+
$this->assertDumpMatchesFormat(
286+
<<<EOTXT
287+
Closure(): int|float|null {
288+
returnType: "int|float|null"
289+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
290+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
291+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
292+
line: "1 to 1"
293+
}
294+
EOTXT
295+
, $f
296+
);
297+
}
298+
171299
public function testGenerator()
172300
{
173301
if (\extension_loaded('xdebug')) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.