From 6dc2b2569c47094b73d276ca7e85836290d859f7 Mon Sep 17 00:00:00 2001 From: Maciej Malarz Date: Tue, 27 Sep 2022 23:38:40 +0200 Subject: [PATCH] Use var_export for scalar default values --- src/Proxy/ProxyGenerator.php | 3 ++- tests/Common/Proxy/PHP81NewInInitializers.php | 17 ++++++++++++++++- tests/Common/Proxy/ProxyGeneratorTest.php | 17 ++++++++++++++++- tests/Common/Util/TestAsset/ConstProvider.php | 3 ++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Proxy/ProxyGenerator.php b/src/Proxy/ProxyGenerator.php index 8adbf5dc4..3219a794d 100644 --- a/src/Proxy/ProxyGenerator.php +++ b/src/Proxy/ProxyGenerator.php @@ -36,6 +36,7 @@ use function interface_exists; use function is_callable; use function is_dir; +use function is_scalar; use function is_string; use function is_writable; use function lcfirst; @@ -1122,7 +1123,7 @@ private function getParameterDefaultValue(ReflectionParameter $parameter) return ''; } - if (PHP_VERSION_ID < 80100) { + if (PHP_VERSION_ID < 80100 || is_scalar($parameter->getDefaultValue())) { return ' = ' . var_export($parameter->getDefaultValue(), true); } diff --git a/tests/Common/Proxy/PHP81NewInInitializers.php b/tests/Common/Proxy/PHP81NewInInitializers.php index 82f54cf41..ff44d30e2 100644 --- a/tests/Common/Proxy/PHP81NewInInitializers.php +++ b/tests/Common/Proxy/PHP81NewInInitializers.php @@ -23,7 +23,22 @@ public function arrayInDefault(array $foo = [new \DateTimeImmutable('2022-08-22 } - public function constInDefault(string $foo = ConstProvider::FOO): void + public function scalarConstInDefault(string $foo = ConstProvider::FOO_SCALAR): void + { + + } + + public function constInDefault(array $foo = ConstProvider::FOO): void + { + + } + + public function globalEolInDefault(string $foo = \PHP_EOL): void + { + + } + + public function specialCharacterInDefault(string $foo = "\n"): void { } diff --git a/tests/Common/Proxy/ProxyGeneratorTest.php b/tests/Common/Proxy/ProxyGeneratorTest.php index 4322be43c..8fca420ce 100644 --- a/tests/Common/Proxy/ProxyGeneratorTest.php +++ b/tests/Common/Proxy/ProxyGeneratorTest.php @@ -602,7 +602,22 @@ public function testPhp81NewInInitializers() ); self::assertStringContainsString( - 'constInDefault(string $foo = \Doctrine\Tests\Common\Util\TestAsset\ConstProvider::FOO): void', + 'scalarConstInDefault(string $foo = \'foo\'): void', + file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php') + ); + + self::assertStringContainsString( + 'constInDefault(array $foo = \Doctrine\Tests\Common\Util\TestAsset\ConstProvider::FOO): void', + file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php') + ); + + self::assertStringContainsString( + "globalEolInDefault(string \$foo = '\n'): void", + file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php') + ); + + self::assertStringContainsString( + "specialCharacterInDefault(string \$foo = '\n'): void", file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php') ); } diff --git a/tests/Common/Util/TestAsset/ConstProvider.php b/tests/Common/Util/TestAsset/ConstProvider.php index 5f121efed..a1c8367ea 100644 --- a/tests/Common/Util/TestAsset/ConstProvider.php +++ b/tests/Common/Util/TestAsset/ConstProvider.php @@ -6,5 +6,6 @@ class ConstProvider { - public const FOO = 'foo'; + public const FOO = ['foo']; + public const FOO_SCALAR = 'foo'; }