Skip to content

Commit 616d5fc

Browse files
authored
Merge pull request #985 from doctrine/const-in-default-value
Fix consts in default values
2 parents 4cc1af7 + cfd54ea commit 616d5fc

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/Proxy/ProxyGenerator.php

+15
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@
4444
use function mkdir;
4545
use function preg_match;
4646
use function preg_match_all;
47+
use function preg_replace;
48+
use function preg_split;
4749
use function random_bytes;
4850
use function rename;
4951
use function rtrim;
5052
use function sprintf;
5153
use function str_replace;
54+
use function strpos;
5255
use function strrev;
5356
use function strtolower;
5457
use function strtr;
@@ -58,6 +61,7 @@
5861

5962
use const DIRECTORY_SEPARATOR;
6063
use const PHP_VERSION_ID;
64+
use const PREG_SPLIT_DELIM_CAPTURE;
6165

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

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

1131+
if (strpos($value, '\\') !== false || strpos($value, '::') !== false) {
1132+
$value = preg_split("/('(?:[^'\\\\]*+(?:\\\\.)*+)*+')/", $value, -1, PREG_SPLIT_DELIM_CAPTURE);
1133+
foreach ($value as $i => $part) {
1134+
if ($i % 2 === 0) {
1135+
$value[$i] = preg_replace('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])[a-zA-Z0-9_\x7f-\xff]++(?:\\\\[a-zA-Z0-9_\x7f-\xff]++|::)++/', '\\\\\0', $part);
1136+
}
1137+
}
1138+
1139+
$value = implode('', $value);
1140+
}
1141+
11271142
return ' = ' . $value;
11281143
}
11291144

tests/Common/Proxy/PHP81NewInInitializers.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\Tests\Common\Proxy;
66

7+
use Doctrine\Tests\Common\Util\TestAsset\ConstProvider;
8+
79
class PHP81NewInInitializers
810
{
911
public function onlyInitializer($foo = new \stdClass()): void
@@ -20,4 +22,9 @@ public function arrayInDefault(array $foo = [new \DateTimeImmutable('2022-08-22
2022
{
2123

2224
}
25+
26+
public function constInDefault(string $foo = ConstProvider::FOO): void
27+
{
28+
29+
}
2330
}

tests/Common/Proxy/ProxyGeneratorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ public function testPhp81NewInInitializers()
600600
'arrayInDefault(array $foo = [new \DateTimeImmutable(\'2022-08-22 16:20\', new \DateTimeZone(\'Europe/Warsaw\'))]): void',
601601
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
602602
);
603+
604+
self::assertStringContainsString(
605+
'constInDefault(string $foo = \Doctrine\Tests\Common\Util\TestAsset\ConstProvider::FOO): void',
606+
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
607+
);
603608
}
604609

605610
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Common\Util\TestAsset;
6+
7+
class ConstProvider
8+
{
9+
public const FOO = 'foo';
10+
}

0 commit comments

Comments
 (0)