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 3294b80

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

File tree

2 files changed

+33
-8
lines changed
Filter options

2 files changed

+33
-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
+5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.