Skip to content

Commit a009e6e

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: [VarDumper] Fix display of nullable union return types. [VarDumper] fixed displaying "mixed" as "?mixed" Handle failure when sending DATA
2 parents 13e7e88 + fa84224 commit a009e6e

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

Caster/ReflectionCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
195195
if (isset($a[$prefix.'returnType'])) {
196196
$v = $a[$prefix.'returnType'];
197197
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
198-
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
198+
$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 : '', '']);
199199
}
200200
if (isset($a[$prefix.'class'])) {
201201
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
@@ -351,7 +351,7 @@ public static function getSignature(array $a)
351351
if (!$type instanceof \ReflectionNamedType) {
352352
$signature .= $type.' ';
353353
} else {
354-
if (!$param->isOptional() && $param->allowsNull()) {
354+
if (!$param->isOptional() && $param->allowsNull() && 'mixed' !== $type->getName()) {
355355
$signature .= '?';
356356
}
357357
$signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' ';

Tests/Caster/ReflectionCasterTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,68 @@ public function testReflectionParameterScalar()
165165
);
166166
}
167167

168+
/**
169+
* @requires PHP 8
170+
*/
171+
public function testReflectionParameterMixed()
172+
{
173+
$f = eval('return function (mixed $a) {};');
174+
$var = new \ReflectionParameter($f, 0);
175+
176+
$this->assertDumpMatchesFormat(
177+
<<<'EOTXT'
178+
ReflectionParameter {
179+
+name: "a"
180+
position: 0
181+
allowsNull: true
182+
typeHint: "mixed"
183+
}
184+
EOTXT
185+
, $var
186+
);
187+
}
188+
189+
/**
190+
* @requires PHP 8
191+
*/
192+
public function testReflectionParameterUnion()
193+
{
194+
$f = eval('return function (int|float $a) {};');
195+
$var = new \ReflectionParameter($f, 0);
196+
197+
$this->assertDumpMatchesFormat(
198+
<<<'EOTXT'
199+
ReflectionParameter {
200+
+name: "a"
201+
position: 0
202+
typeHint: "int|float"
203+
}
204+
EOTXT
205+
, $var
206+
);
207+
}
208+
209+
/**
210+
* @requires PHP 8
211+
*/
212+
public function testReflectionParameterNullableUnion()
213+
{
214+
$f = eval('return function (int|float|null $a) {};');
215+
$var = new \ReflectionParameter($f, 0);
216+
217+
$this->assertDumpMatchesFormat(
218+
<<<'EOTXT'
219+
ReflectionParameter {
220+
+name: "a"
221+
position: 0
222+
allowsNull: true
223+
typeHint: "int|float|null"
224+
}
225+
EOTXT
226+
, $var
227+
);
228+
}
229+
168230
public function testReturnType()
169231
{
170232
$f = eval('return function ():int {};');
@@ -184,6 +246,72 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
184246
);
185247
}
186248

249+
/**
250+
* @requires PHP 8
251+
*/
252+
public function testMixedReturnType()
253+
{
254+
$f = eval('return function (): mixed {};');
255+
$line = __LINE__ - 1;
256+
257+
$this->assertDumpMatchesFormat(
258+
<<<EOTXT
259+
Closure(): mixed {
260+
returnType: "mixed"
261+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
262+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
263+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
264+
line: "1 to 1"
265+
}
266+
EOTXT
267+
, $f
268+
);
269+
}
270+
271+
/**
272+
* @requires PHP 8
273+
*/
274+
public function testUnionReturnType()
275+
{
276+
$f = eval('return function (): int|float {};');
277+
$line = __LINE__ - 1;
278+
279+
$this->assertDumpMatchesFormat(
280+
<<<EOTXT
281+
Closure(): int|float {
282+
returnType: "int|float"
283+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
284+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
285+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
286+
line: "1 to 1"
287+
}
288+
EOTXT
289+
, $f
290+
);
291+
}
292+
293+
/**
294+
* @requires PHP 8
295+
*/
296+
public function testNullableUnionReturnType()
297+
{
298+
$f = eval('return function (): int|float|null {};');
299+
$line = __LINE__ - 1;
300+
301+
$this->assertDumpMatchesFormat(
302+
<<<EOTXT
303+
Closure(): int|float|null {
304+
returnType: "int|float|null"
305+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
306+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
307+
file: "%sReflectionCasterTest.php($line) : eval()'d code"
308+
line: "1 to 1"
309+
}
310+
EOTXT
311+
, $f
312+
);
313+
}
314+
187315
public function testGenerator()
188316
{
189317
if (\extension_loaded('xdebug')) {

0 commit comments

Comments
 (0)