Skip to content

Commit

Permalink
Merge pull request #985 from doctrine/const-in-default-value
Browse files Browse the repository at this point in the history
Fix consts in default values
  • Loading branch information
malarzm authored Sep 26, 2022
2 parents 4cc1af7 + cfd54ea commit 616d5fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
use function mkdir;
use function preg_match;
use function preg_match_all;
use function preg_replace;
use function preg_split;
use function random_bytes;
use function rename;
use function rtrim;
use function sprintf;
use function str_replace;
use function strpos;
use function strrev;
use function strtolower;
use function strtr;
Expand All @@ -58,6 +61,7 @@

use const DIRECTORY_SEPARATOR;
use const PHP_VERSION_ID;
use const PREG_SPLIT_DELIM_CAPTURE;

/**
* This factory is used to generate proxy classes.
Expand Down Expand Up @@ -1124,6 +1128,17 @@ private function getParameterDefaultValue(ReflectionParameter $parameter)

$value = rtrim(substr(explode('$' . $parameter->getName() . ' = ', (string) $parameter, 2)[1], 0, -2));

if (strpos($value, '\\') !== false || strpos($value, '::') !== false) {
$value = preg_split("/('(?:[^'\\\\]*+(?:\\\\.)*+)*+')/", $value, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($value as $i => $part) {
if ($i % 2 === 0) {
$value[$i] = preg_replace('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])[a-zA-Z0-9_\x7f-\xff]++(?:\\\\[a-zA-Z0-9_\x7f-\xff]++|::)++/', '\\\\\0', $part);
}
}

$value = implode('', $value);
}

return ' = ' . $value;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Common/Proxy/PHP81NewInInitializers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\Tests\Common\Proxy;

use Doctrine\Tests\Common\Util\TestAsset\ConstProvider;

class PHP81NewInInitializers
{
public function onlyInitializer($foo = new \stdClass()): void
Expand All @@ -20,4 +22,9 @@ public function arrayInDefault(array $foo = [new \DateTimeImmutable('2022-08-22
{

}

public function constInDefault(string $foo = ConstProvider::FOO): void
{

}
}
5 changes: 5 additions & 0 deletions tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ public function testPhp81NewInInitializers()
'arrayInDefault(array $foo = [new \DateTimeImmutable(\'2022-08-22 16:20\', new \DateTimeZone(\'Europe/Warsaw\'))]): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
);

self::assertStringContainsString(
'constInDefault(string $foo = \Doctrine\Tests\Common\Util\TestAsset\ConstProvider::FOO): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Common/Util/TestAsset/ConstProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Util\TestAsset;

class ConstProvider
{
public const FOO = 'foo';
}

0 comments on commit 616d5fc

Please sign in to comment.