8000 Allow to work with global constant without FQCN · symfony/symfony@ddf9e3b · GitHub
[go: up one dir, main page]

Skip to content
Dismiss alert

Commit ddf9e3b

Browse files
committed
Allow to work with global constant without FQCN
1 parent 6284bb5 commit ddf9e3b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
299299
try {
300300
$a[$prefix.'default'] = $v = $c->getDefaultValue();
301301
if ($c->isDefaultValueConstant() && !\is_object($v)) {
302-
$a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
302+
$a[$prefix.'default'] = new ConstStub(self::getDefaultValueConstantName($c), $v);
303303
}
304304
if (null === $v) {
305305
unset($a[$prefix.'allowsNull']);
@@ -383,7 +383,7 @@ public static function getSignature(array $a)
383383
$signature .= ' = ';
384384

385385
if ($param->isDefaultValueConstant()) {
386-
$signature .= substr(strrchr('\\'.$param->getDefaultValueConstantName(), '\\'), 1);
386+
$signature .= substr(strrchr('\\'.self::getDefaultValueConstantName($param), '\\'), 1);
387387
} elseif (null === $v) {
388388
$signature .= 'null';
389389
} elseif (\is_array($v)) {
@@ -445,4 +445,19 @@ private static function addAttributes(array &$a, \Reflector $c, string $prefix =
445445
}
446446
}
447447
}
448+
449+
private static function getDefaultValueConstantName(\ReflectionParameter $param): ?string
450+
{
451+
$namespacedConstant = $param->getDefaultValueConstantName();
452+
453+
if (null !== $namespacedConstant && str_contains($namespacedConstant, '\\') && !\defined($namespacedConstant)) {
454+
$globalConstant = '\\'.preg_replace('/^.*\\\\([^\\\\]+)$/', '$1', $namespacedConstant);
455+
456+
if (\defined($globalConstant) && $param->getDefaultValue() === \constant($globalConstant)) {
457+
return $globalConstant;
458+
}
459+
}
460+
461+
return $namespacedConstant;
462+
}
448463
}

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

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\VarDumper\Caster\Caster;
16+
use Symfony\Component\VarDumper\Caster\ConstStub;
17+
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
18+
use Symfony\Component\VarDumper\Cloner\Stub;
1619
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1720
use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture;
1821
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
@@ -85,7 +88,9 @@ public function testReflectionCaster()
8588
public function testClosureCaster()
8689
{
8790
$a = $b = 123;
91+
// @codingStandardsIgnoreStart
8892
$var = function ($x) use ($a, &$b) {};
93+
// @codingStandardsIgnoreEnd
8994

9095
$this->assertDumpMatchesFormat(
9196
<<<'EOTXT'
@@ -95,7 +100,7 @@ public function testClosureCaster()
95100
$b: & 123
96101
}
97102
file: "%sReflectionCasterTest.php"
98-
line: "88 to 88"
103+
line: "91 to 91"
99104
}
100105
EOTXT
101106
, $var
@@ -550,7 +555,7 @@ public function testGenerator()
550555
Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo()
551556
› yield 1;
552557
› }
553-
558+
554559
}
555560
%A }
556561
closed: false
@@ -701,6 +706,27 @@ public function testReflectionClassConstantWithAttribute()
701706
, $var);
702707
}
703708

709+
public function testGlobalConstantAsDefaultValue()
710+
{
711+
$class = new class() {
712+
/**
713+
* @codingStandardsIgnore
714+
*/
715+
public function foo(float $value = M_PI)
716+
{
717+
// Dummy content
718+
}
719+
};
720+
$method = new \ReflectionMethod($class, 'foo');
721+
$parameter = $method->getParameters()[0];
722+
$cast = ReflectionCaster::castParameter($parameter, [], new Stub(), false);
723+
/** @var ConstStub $defaultStub */
724+
$defaultStub = $cast["\0~\0default"];
725+
726+
$this->assertInstanceOf(ConstStub::class, $defaultStub);
727+
$this->assertSame('\\'.\M_PI::class, $defaultStub->class);
728+
}
729+
704730
/**
705731
* @requires PHP 8
706732
*/

0 commit comments

Comments
 (0)
0