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 fbaed3b

Browse filesBrowse files
committed
Allow to work with global constant without FQCN
1 parent 6284bb5 commit fbaed3b
Copy full SHA for fbaed3b

File tree

2 files changed

+38
-2
lines changed
Filter options

2 files changed

+38
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
+17-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
+21Lines changed: 21 additions & 0 deletions
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;
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.