Skip to content

Navigation Menu

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 4420c35

Browse filesBrowse files
committed
Allow to work with global constant without FQCN
1 parent e4bd593 commit 4420c35
Copy full SHA for 4420c35

File tree

2 files changed

+34
-8
lines changed
Filter options

2 files changed

+34
-8
lines changed

‎src/Symfony/Component/VarExporter/ProxyHelper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/ProxyHelper.php
+28-8Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class ProxyHelperTest extends TestCase
2121
{
2222
/**
23+
* @group i
2324
* @dataProvider provideExportSignature
2425
*/
2526
public function testExportSignature(string $expected, \ReflectionMethod $method)
@@ -37,6 +38,7 @@ public static function provideExportSignature()
3738
$expected = str_replace(['.', ' . . . ', '\'$a\', \'$a\n\', "\$a\n"'], [' . ', '...', '\'$a\', "\$a\\\n", "\$a\n"'], $expected);
3839
$expected = str_replace('Bar', '\\'.Bar::class, $expected);
3940
$expected = str_replace('self', '\\'.TestForProxyHelper::class, $expected);
41+
$expected = str_replace('= M_PI', '= \\M_PI', $expected);
4042

4143
yield [$expected, $method];
4244
}
@@ -237,6 +239,10 @@ public static function foo8()
237239
public function foo9($a = self::BOB, $b = ['$a', '$a\n', "\$a\n"], $c = ['$a', '$a\n', "\$a\n", new \stdClass()])
238240
{
239241
}
242+
243+
public function foo10($a = M_PI)
244+
{
245+
}
240246
}
241247

242248
interface TestForProxyHelperInterface1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.