Skip to content

Commit 6ea7b07

Browse files
committed
[Config][ReflectionClassResource] Handle parameters with undefined constant as their default values
1 parent 814bdeb commit 6ea7b07

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

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

+54-3
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,63 @@ private function generateSignature(\ReflectionClass $class)
151151
}
152152
} else {
153153
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
154-
yield preg_replace('/^ @@.*/m', '', $m);
155-
156154
$defaults = [];
155+
$parametersWithUndefinedConstants = [];
157156
foreach ($m->getParameters() as $p) {
158-
$defaults[$p->name] = $p->isDefaultValueAvailable() ? $p->getDefaultValue() : null;
157+
if (!$p->isDefaultValueAvailable()) {
158+
$defaults[$p->name] = null;
159+
160+
continue;
161+
}
162+
163+
if (!$p->isDefaultValueConstant() || \defined($p->getDefaultValueConstantName())) {
164+
$defaults[$p->name] = $p->getDefaultValue();
165+
166+
continue;
167+
}
168+
169+
$defaults[$p->name] = $p->getDefaultValueConstantName();
170+
$parametersWithUndefinedConstants[$p->name] = true;
159171
}
172+
173+
if (!$parametersWithUndefinedConstants) {
174+
yield preg_replace('/^ @@.*/m', '', $m);
175+
} else {
176+
yield $m->getDocComment();
177+
yield $m->getName();
178+
yield (int) $m->isAbstract();
179+
yield (int) $m->isFinal();
180+
yield (int) $m->isStatic();
181+
yield (int) $m->isPublic();
182+
yield (int) $m->isPrivate();
183+
yield (int) $m->isProtected();
184+
yield (int) $m->returnsReference();
185+
yield (int) $m->isVariadic();
186+
187+
foreach ($m->getParameters() as $p) {
188+
if (!isset($parametersWithUndefinedConstants[$p->name])) {
189+
yield (string) $p;
190+
} else {
191+
yield (int) $p->isOptional();
192+
yield $hasType = (int) $p->hasType();
193+
194+
if ($hasType) {
195+
yield (string) $p->getType();
196+
}
197+
198+
yield (int) $p->isPassedByReference();
199+
yield (int) $p->isVariadic();
200+
yield $p->getName();
201+
}
202+
}
203+
204+
yield $hasReturnType = (int) $m->hasReturnType();
205+
206+
if ($hasReturnType) {
207+
yield (string) $m->getReturnType();
208+
}
209+
}
210+
160211
yield print_r($defaults, true);
161212
}
162213
}

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ public function testIsFreshForDeletedResources()
6363
/**
6464
* @dataProvider provideHashedSignature
6565
*/
66-
public function testHashedSignature($changeExpected, $changedLine, $changedCode)
66+
public function testHashedSignature($changeExpected, $changedLine, $changedCode, $setContext = null)
6767
{
68+
if ($setContext) {
69+
$setContext();
70+
}
71+
6872
$code = <<<'EOPHP'
6973
/* 0*/
7074
/* 1*/ class %s extends ErrorException
@@ -82,7 +86,9 @@ public function testHashedSignature($changeExpected, $changedLine, $changedCode)
8286
/*13*/ protected function prot($a = []) {}
8387
/*14*/
8488
/*15*/ private function priv() {}
85-
/*16*/ }
89+
/*16*/
90+
/*17*/ public function ccc($bar = A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC) {}
91+
/*18*/ }
8692
EOPHP;
8793

8894
static $expectedSignature, $generateSignature;
@@ -97,7 +103,9 @@ public function testHashedSignature($changeExpected, $changedLine, $changedCode)
97103
}
98104

99105
$code = explode("\n", $code);
100-
$code[$changedLine] = $changedCode;
106+
if (null !== $changedCode) {
107+
$code[$changedLine] = $changedCode;
108+
}
101109
eval(sprintf(implode("\n", $code), $class = 'Foo'.str_replace('.', '_', uniqid('', true))));
102110
$signature = implode("\n", iterator_to_array($generateSignature(new \ReflectionClass($class))));
103111

@@ -145,6 +153,10 @@ public function provideHashedSignature()
145153
yield [0, 7, 'protected int $prot;'];
146154
yield [0, 9, 'private string $priv;'];
147155
}
156+
157+
yield [1, 17, 'public function ccc($bar = 187) {}'];
158+
yield [1, 17, 'public function ccc($bar = ANOTHER_ONE_THAT_WILL_NEVER_BE_DEFINED_CCCCCCCCC) {}'];
159+
yield [1, 17, null, static function (): void { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
148160
}
149161

150162
public function testEventSubscriber()

0 commit comments

Comments
 (0)