Skip to content

[Config+DependencyInjection+VarDumper] Allow to work with global constant without FQCN #56824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Symfony/Component/Config/Resource/ReflectionClassResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
use Symfony\Component\VarDumper\Caster\ConstCaster;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
Expand Down Expand Up @@ -197,13 +198,21 @@ private function generateSignature(\ReflectionClass $class): iterable
continue;
}

if (!$p->isDefaultValueConstant() || $defined($p->getDefaultValueConstantName())) {
if (!$p->isDefaultValueConstant()) {
$defaults[$p->name] = $p->getDefaultValue();

continue;
}

$defaults[$p->name] = $p->getDefaultValueConstantName();
$constantName = ConstCaster::castParameterDefaultValue($p);

if ($defined($constantName)) {
$defaults[$p->name] = $p->getDefaultValue();

continue;
}

$defaults[$p->name] = $constantName;
$parametersWithUndefinedConstants[$p->name] = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\DependencyInjection\Dumper;

use Symfony\Component\VarDumper\Caster\ConstCaster;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
Expand Down Expand Up @@ -99,7 +101,7 @@ private static function doPreload(string $class, array &$preloaded): void
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $m) {
foreach ($m->getParameters() as $p) {
if ($p->isDefaultValueAvailable() && $p->isDefaultValueConstant()) {
$c = $p->getDefaultValueConstantName();
$c = ConstCaster::castParameterDefaultValue($p);

if ($i = strpos($c, '::')) {
self::doPreload(substr($c, 0, $i), $preloaded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public function setBar($value = null)
{
$this->bar = $value;
}

public function byPi(float $pi = M_PI): float
{
return $this->bar * $pi;
}
}
38 changes: 38 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ConstCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

/**
* Get the FQCN of a parameter default constant value.
*/
final class ConstCaster
{
public static function castParameterDefaultValue(\ReflectionParameter $param): ?string
{
echo (new \Exception())->getTraceAsString() . "\n\n";
$namespacedConstant = $param->getDefaultValueConstantName();
var_dump($namespacedConstant);

if (null !== $namespacedConstant && str_contains($namespacedConstant, '\\') && !\defined($namespacedConstant)) {
$globalConstant = '\\'.preg_replace('/^.*\\\\([^\\\\]+)$/', '$1', $namespacedConstant);

if (\defined($globalConstant) && $param->getDefaultValue() === \constant($globalConstant)) {
var_dump($globalConstant);
exit;
return $globalConstant;
}
}

exit;
return $namespacedConstant;
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
try {
$a[$prefix.'default'] = $v = $c->getDefaultValue();
if ($c->isDefaultValueConstant() && !\is_object($v)) {
$a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
$a[$prefix.'default'] = new ConstStub(ConstCaster::castParameterDefaultValue($c), $v);
}
if (null === $v) {
unset($a[$prefix.'allowsNull']);
Expand Down Expand Up @@ -383,7 +383,7 @@ public static function getSignature(array $a)
$signature .= ' = ';

if ($param->isDefaultValueConstant()) {
$signature .= substr(strrchr('\\'.$param->getDefaultValueConstantName(), '\\'), 1);
$signature .= substr(strrchr('\\'.ConstCaster::castParameterDefaultValue($param), '\\'), 1);
} elseif (null === $v) {
$signature .= 'null';
} elseif (\is_array($v)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Caster\ConstStub;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
Expand Down Expand Up @@ -95,7 +98,7 @@ public function testClosureCaster()
$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "88 to 88"
line: "91 to 91"
}
EOTXT
, $var
Expand Down Expand Up @@ -701,6 +704,24 @@ public function testReflectionClassConstantWithAttribute()
, $var);
}

public function testGlobalConstantAsDefaultValue()
{
$class = new class() {
public function foo(float $value = M_PI)
{
// Dummy content
}
};
$method = new \ReflectionMethod($class, 'foo');
$parameter = $method->getParameters()[0];
$cast = ReflectionCaster::castParameter($parameter, [], new Stub(), false);
/** @var ConstStub $defaultStub */
$defaultStub = $cast["\0~\0default"];

$this->assertInstanceOf(ConstStub::class, $defaultStub);
$this->assertSame('\\'.\M_PI::class, $defaultStub->class);
}

/**
* @requires PHP 8
*/
Expand Down