Skip to content

Commit ab96ab0

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

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 17 additions & 2 deletions
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

Lines changed: 22 additions & 1 deletion
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;
@@ -95,7 +98,7 @@ public function testClosureCaster()
9598
$b: & 123
9699
}
97100
file: "%sReflectionCasterTest.php"
98-
line: "88 to 88"
101+
line: "91 to 91"
99102
}
100103
EOTXT
101104
, $var
@@ -701,6 +704,24 @@ public function testReflectionClassConstantWithAttribute()
701704
, $var);
702705
}
703706

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy