8000 bug #39631 [VarDumper] Fix display of nullable union return types (de… · symfony/symfony@0ed047f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ed047f

Browse files
committed
bug #39631 [VarDumper] Fix display of nullable union return types (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [VarDumper] Fix display of nullable union return types | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #39629 (review) | License | MIT | Doc PR | N/A Commits ------- efeb2dc [VarDumper] Fix display of nullable union return types.
2 parents 018e33a + efeb2dc commit 0ed047f

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

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

Lines 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

Lines 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