8000 bug #34560 [Config][ReflectionClassResource] Handle parameters with u… · symfony/symfony@9eafff5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eafff5

Browse files
bug #34560 [Config][ReflectionClassResource] Handle parameters with undefined constant as their default values (fancyweb)
This PR was merged into the 3.4 branch. Discussion ---------- [Config][ReflectionClassResource] Handle parameters with undefined constant as their default values | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #34053 | License | MIT | Doc PR | - Basically we can fix this bug by "reimplementing" php src way of building the __toString() of the method except that we avoid to call the undefined constant. Obviously we cannot invalidate the resource if the value of the constant changes since we never knew it. However, it's still better than now. Commits ------- 8de2a22 [Config][ReflectionClassResource] Handle parameters with undefined constant as their default values
2 parents 2d2dd62 + 8de2a22 commit 9eafff5

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,56 @@ 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;
171+
}
172+
173+
if (!$parametersWithUndefinedConstants) {
174+
yield preg_replace('/^ @@.*/m', '', $m);
175+
} else {
176+
$stack = [
177+
$m->getDocComment(),
178+
$m->getName(),
179+
$m->isAbstract(),
180+
$m->isFinal(),
181+
$m->isStatic(),
182+
$m->isPublic(),
183+
$m->isPrivate(),
184+
$m->isProtected(),
185+
$m->returnsReference(),
186+
\PHP_VERSION_ID >= 70000 && $m->hasReturnType() ? (\PHP_VERSION_ID >= 70100 ? $m->getReturnType()->getName() : (string) $m->getReturnType()) : '',
187+
];
188+
189+
foreach ($m->getParameters() as $p) {
190+
if (!isset($parametersWithUndefinedConstants[$p->name])) {
191+
$stack[] = (string) $p;
192+
} else {
193+
$stack[] = $p->isOptional();
194+
$stack[] = \PHP_VERSION_ID >= 70000 && $p->hasType() ? (\PHP_VERSION_ID >= 70100 ? $p->getType()->getName() : (string) $p->getType()) : '';
195+
$stack[] = $p->isPassedByReference();
196+
$stack[] = \PHP_VERSION_ID >= 50600 ? $p->isVariadic() : '';
197+
$stack[] = $p->getName();
198+
}
199+
}
200+
201+
yield implode(',', $stack);
159202
}
203+
160204
yield print_r($defaults, true);
161205
}
162206
}

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

Lines changed: 15 additions & 3 deletions
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 () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
148160
}
149161

150162
public function testEventSubscriber()

0 commit comments

Comments
 (0)
0