Skip to content
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

Use var_export for scalar default values #989

Merged
merged 1 commit into from
Sep 28, 2022
Merged
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
3 changes: 2 additions & 1 deletion src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 16 additions & 1 deletion tests/Common/Proxy/PHP81NewInInitializers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

}
Expand Down
17 changes: 16 additions & 1 deletion tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Common/Util/TestAsset/ConstProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

class ConstProvider
{
public const FOO = 'foo';
public const FOO = ['foo'];
public const FOO_SCALAR = 'foo';
}