Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 078df14

Browse files
bug #43267 [Config] Fix signature generation with nested attributes on 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 #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 ------- 6300a17 [Config] Fix signature generation with nested attributes on PHP 8.1
2 parents 95c0c10 + 6300a17 commit 078df14

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Symfony/Component/Config/Resource/ReflectionClassResource.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private function generateSignature(\ReflectionClass $class): iterable
122122
if (\PHP_VERSION_ID >= 80000) {
123123
$attributes = [];
124124
foreach ($class->getAttributes() as $a) {
125-
$attributes[] = [$a->getName(), $a->getArguments()];
125+
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
126126
}
127127
yield print_r($attributes, true);
128128
$attributes = [];
@@ -146,7 +146,7 @@ private function generateSignature(\ReflectionClass $class): iterable
146146
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) {
147147
if (\PHP_VERSION_ID >= 80000) {
148148
foreach ($p->getAttributes() as $a) {
149-
$attributes[] = [$a->getName(), $a->getArguments()];
149+
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
150150
}
151151
yield print_r($attributes, true);
152152
$attributes = [];
@@ -166,7 +166,7 @@ private function generateSignature(\ReflectionClass $class): iterable
166166
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
167167
if (\PHP_VERSION_ID >= 80000) {
168168
foreach ($m->getAttributes() as $a) {
169-
$attributes[] = [$a->getName(), $a->getArguments()];
169+
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
170170
}
171171
yield print_r($attributes, true);
172172
$attributes = [];
@@ -177,7 +177,7 @@ private function generateSignature(\ReflectionClass $class): iterable
177177
foreach ($m->getParameters() as $p) {
178178
if (\PHP_VERSION_ID >= 80000) {
179179
foreach ($p->getAttributes() as $a) {
180-
$attributes[] = [$a->getName(), $a->getArguments()];
180+
$attributes[] = [$a->getName(), \PHP_VERSION_ID >= 80100 ? (string) $a : $a->getArguments()];
181181
}
182182
yield print_r($attributes, true);
183183
$attributes = [];
@@ -189,6 +189,12 @@ private function generateSignature(\ReflectionClass $class): iterable
189189
continue;
190190
}
191191

192+
if (\PHP_VERSION_ID >= 80100) {
193+
$defaults[$p->name] = (string) $p;
194+
195+
continue;
196+
}
197+
192198
if (!$p->isDefaultValueConstant() || $defined($p->getDefaultValueConstantName())) {
193199
$defaults[$p->name] = $p->getDefaultValue();
194200

src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public function provideHashedSignature(): iterable
126126
yield [true, 0, '#[Foo]'];
127127
}
128128

129+
if (\PHP_VERSION_ID >= 80100) {
130+
yield [true, 0, '#[Foo(new MissingClass)]'];
131+
}
132+
129133
yield [true, 1, 'abstract class %s'];
130134
yield [true, 1, 'final class %s'];
131135
yield [true, 1, 'class %s extends Exception'];
@@ -135,6 +139,11 @@ public function provideHashedSignature(): iterable
135139
yield [true, 4, '/** pub docblock */'];
136140
yield [true, 5, 'protected $pub = [];'];
137141
yield [true, 5, 'public $pub = [123];'];
142+
143+
if (\PHP_VERSION_ID >= 80100) {
144+
yield [true, 5, '#[Foo(new MissingClass)] public $pub = [];'];
145+
}
146+
138147
yield [true, 6, '/** prot docblock */'];
139148
yield [true, 7, 'private $prot;'];
140149
yield [false, 8, '/** priv docblock */'];
@@ -151,6 +160,11 @@ public function provideHashedSignature(): iterable
151160
yield [true, 13, 'protected function prot(#[Foo] $a = []) {}'];
152161
}
153162

163+
if (\PHP_VERSION_ID >= 80100) {
164+
yield [true, 13, '#[Foo(new MissingClass)] protected function prot($a = []) {}'];
165+
yield [true, 13, 'protected function prot(#[Foo(new MissingClass)] $a = []) {}'];
166+
}
167+
154168
yield [false, 14, '/** priv docblock */'];
155169
yield [false, 15, ''];
156170

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

179+
if (\PHP_VERSION_ID >= 80100) {
180+
yield [true, 17, 'public function __construct(private $bar = new \stdClass()) {}'];
181+
yield [true, 17, 'public function ccc($bar = new \stdClass()) {}'];
182+
yield [true, 17, 'public function ccc($bar = new MissingClass()) {}'];
183+
}
184+
165185
yield [true, 17, 'public function ccc($bar = 187) {}'];
166186
yield [true, 17, 'public function ccc($bar = ANOTHER_ONE_THAT_WILL_NEVER_BE_DEFINED_CCCCCCCCC) {}'];
167187
yield [true, 17, 'public function ccc($bar = parent::BOOM) {}'];
168-
yield [true, 17, null, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
188+
yield [\PHP_VERSION_ID < 80100, 17, null, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
169189
}
170190

171191
public function testEventSubscriber()

0 commit comments

Comments
 (0)