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

Fix consts in default values #985

Merged
merged 1 commit into from
Sep 26, 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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think lines 1131, 1134 and 1135 warrant explanations/code comments, I can't say I understand what they do. I've tried dumping $value on line 1133 and it does not seem to do any splitting in the case of the constant. Also, removing the dot in the regex on line 1132 produces no effect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas maybe you can help? What should the preg_split part produce? Why are we acting only on even items of the array it produces?

}
}

$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';
}