Skip to content

Commit

Permalink
Merge pull request #989 from doctrine/var-export-for-scalar-defaults
Browse files Browse the repository at this point in the history
Use var_export for scalar default values
  • Loading branch information
malarzm authored Sep 28, 2022
2 parents 616d5fc + 6dc2b25 commit 609c3a7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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';
}

0 comments on commit 609c3a7

Please sign in to comment.