Skip to content

Commit 3294b80

Browse files
committed
Allow to work with global constant without FQCN
1 parent e4bd593 commit 3294b80

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,24 +349,44 @@ private static function exportDefault(\ReflectionParameter $param): string
349349

350350
$regexp = '/([\[\( ]|^)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z0-9_\x7f-\xff]++)*+)(?!: )/';
351351
$callback = (false !== strpbrk($default, "\\:('") && $class = $param->getDeclaringClass())
352-
? fn ($m) => $m[1].match ($m[2]) {
352+
? (fn ($m) => $m[1].match ($m[2]) {
353353
'new', 'false', 'true', 'null' => $m[2],
354-
'NULL' => 'null',
355354
'self' => '\\'.$class->name,
356355
'namespace\\parent',
357356
'parent' => ($parent = $class->getParentClass()) ? '\\'.$parent->name : 'parent',
358-
default => '\\'.$m[2],
359-
}
360-
: fn ($m) => $m[1].match ($m[2]) {
357+
default => self::exportDefaultValue($param, $m[2]),
358+
})
359+
: (fn ($m) => $m[1].match ($m[2]) {
361360
'new', 'false', 'true', 'null', 'self', 'parent' => $m[2],
362-
'NULL' => 'null',
363-
default => '\\'.$m[2],
364-
};
361+
default => self::exportDefaultValue($param, $m[2]),
362+
});
365363

366364
return implode('', array_map(fn ($part) => match ($part[0]) {
367365
'"' => $part, // for internal classes only
368366
"'" => false !== strpbrk($part, "\\\0\r\n") ? '"'.substr(str_replace(['$', "\0", "\r", "\n"], ['\$', '\0', '\r', '\n'], $part), 1, -1).'"' : $part,
369367
default => preg_replace_callback($regexp, $callback, $part),
370368
}, $parts));
371369
}
370+
371+
private static function exportDefaultValue(\ReflectionParameter $param, string $value): string
372+
{
373+
if ('NULL' === $value) {
374+
return 'null';
375+
}
376+
377+
if (
378+
$param->isDefaultValueConstant()
379+
&& $value === $param->getDefaultValueConstantName()
380+
&& str_contains($value, '\\')
381+
&& !\defined($value)
382+
) {
383+
$globalConstant = '\\'.preg_replace('/^.*\\\\([^\\\\]+)$/', '$1', $value);
384+
385+
if (\defined($globalConstant) && $param->getDefaultValue() === \constant($globalConstant)) {
386+
return $globalConstant;
387+
}
388+
}
389+
390+
return '\\'.$value;
391+
}
372392
}

src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static function provideExportSignature()
3737
$expected = str_replace(['.', ' . . . ', '\'$a\', \'$a\n\', "\$a\n"'], [' . ', '...', '\'$a\', "\$a\\\n", "\$a\n"'], $expected);
3838
$expected = str_replace('Bar', '\\'.Bar::class, $expected);
3939
$expected = str_replace('self', '\\'.TestForProxyHelper::class, $expected);
40+
$expected = str_replace('= M_PI', '= \\M_PI', $expected);
4041

4142
yield [$expected, $method];
4243
}
@@ -237,6 +238,10 @@ public static function foo8()
237238
public function foo9($a = self::BOB, $b = ['$a', '$a\n', "\$a\n"], $c = ['$a', '$a\n', "\$a\n", new \stdClass()])
238239
{
239240
}
241+
242+
public function foo10($a = M_PI)
243+
{
244+
}
240245
}
241246

242247
interface TestForProxyHelperInterface1

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