Skip to content

Commit

Permalink
bug #43267 [Config] Fix signature generation with nested attributes o…
Browse files Browse the repository at this point in the history
…n PHP 8.1 (agustingomes)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Config] Fix signature generation with nested attributes on PHP 8.1

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony/symfony#43260, part of #41552
| License       | MIT
| Doc PR        | -

This fix allows the new PHP 8.1 syntax of [new in initializers](https://wiki.php.net/rfc/new_in_initializers)

It allows the method signature to be inferred from the object that is the default parameter value.

Commits
-------

6300a17be2 [Config] Fix signature generation with nested attributes on PHP 8.1
  • Loading branch information
nicolas-grekas committed Oct 29, 2021
2 parents 25c1193 + c0f2a2c commit e99b65a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Resource/ReflectionClassResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function generateSignature(\ReflectionClass $class): iterable
if (\PHP_VERSION_ID >= 80000) {
$attributes = [];
foreach ($class->getAttributes() as $a) {
$attributes[] = [$a->getName(), $a->getArguments()];
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
}
yield print_r($attributes, true);
$attributes = [];
Expand All @@ -146,7 +146,7 @@ private function generateSignature(\ReflectionClass $class): iterable
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) {
if (\PHP_VERSION_ID >= 80000) {
foreach ($p->getAttributes() as $a) {
$attributes[] = [$a->getName(), $a->getArguments()];
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
}
yield print_r($attributes, true);
$attributes = [];
Expand All @@ -166,7 +166,7 @@ private function generateSignature(\ReflectionClass $class): iterable
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
if (\PHP_VERSION_ID >= 80000) {
foreach ($m->getAttributes() as $a) {
$attributes[] = [$a->getName(), $a->getArguments()];
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
}
yield print_r($attributes, true);
$attributes = [];
Expand All @@ -177,7 +177,7 @@ private function generateSignature(\ReflectionClass $class): iterable
foreach ($m->getParameters() as $p) {
if (\PHP_VERSION_ID >= 80000) {
foreach ($p->getAttributes() as $a) {
$attributes[] = [$a->getName(), $a->getArguments()];
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
}
yield print_r($attributes, true);
$attributes = [];
Expand All @@ -189,6 +189,12 @@ private function generateSignature(\ReflectionClass $class): iterable
continue;
}

if (\PHP_VERSION_ID >= 80100) {
$defaults[$p->name] = (string) $p;

continue;
}

if (!$p->isDefaultValueConstant() || $defined($p->getDefaultValueConstantName())) {
$defaults[$p->name] = $p->getDefaultValue();

Expand Down
22 changes: 21 additions & 1 deletion Tests/Resource/ReflectionClassResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function provideHashedSignature(): iterable
yield [true, 0, '#[Foo]'];
}

if (\PHP_VERSION_ID >= 80100) {
yield [true, 0, '#[Foo(new MissingClass)]'];
}

yield [true, 1, 'abstract class %s'];
yield [true, 1, 'final class %s'];
yield [true, 1, 'class %s extends Exception'];
Expand All @@ -135,6 +139,11 @@ public function provideHashedSignature(): iterable
yield [true, 4, '/** pub docblock */'];
yield [true, 5, 'protected $pub = [];'];
yield [true, 5, 'public $pub = [123];'];

if (\PHP_VERSION_ID >= 80100) {
yield [true, 5, '#[Foo(new MissingClass)] public $pub = [];'];
}

yield [true, 6, '/** prot docblock */'];
yield [true, 7, 'private $prot;'];
yield [false, 8, '/** priv docblock */'];
Expand All @@ -151,6 +160,11 @@ public function provideHashedSignature(): iterable
yield [true, 13, 'protected function prot(#[Foo] $a = []) {}'];
}

if (\PHP_VERSION_ID >= 80100) {
yield [true, 13, '#[Foo(new MissingClass)] protected function prot($a = []) {}'];
yield [true, 13, 'protected function prot(#[Foo(new MissingClass)] $a = []) {}'];
}

yield [false, 14, '/** priv docblock */'];
yield [false, 15, ''];

Expand All @@ -162,10 +176,16 @@ public function provideHashedSignature(): iterable
yield [false, 9, 'private string $priv;'];
}

if (\PHP_VERSION_ID >= 80100) {
yield [true, 17, 'public function __construct(private $bar = new \stdClass()) {}'];
yield [true, 17, 'public function ccc($bar = new \stdClass()) {}'];
yield [true, 17, 'public function ccc($bar = new MissingClass()) {}'];
}

yield [true, 17, 'public function ccc($bar = 187) {}'];
yield [true, 17, 'public function ccc($bar = ANOTHER_ONE_THAT_WILL_NEVER_BE_DEFINED_CCCCCCCCC) {}'];
yield [true, 17, 'public function ccc($bar = parent::BOOM) {}'];
yield [true, 17, null, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
yield [\PHP_VERSION_ID < 80100, 17, null, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
}

public function testEventSubscriber()
Expand Down

0 comments on commit e99b65a

Please sign in to comment.