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

Skip to content

Commit 1a728dd

Browse files
committed
Provide possible fix to allow new in initializers
It implements a new private method to go through all the possible scenarios of a parameter to avoid casting objects to string that potentially won't implement Serializable interface.
1 parent f3906cb commit 1a728dd

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

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

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -199,39 +199,35 @@ private function generateSignature(\ReflectionClass $class): iterable
199199
$parametersWithUndefinedConstants[$p->name] = true;
200200
}
201201

202-
if (!$parametersWithUndefinedConstants) {
203-
yield preg_replace('/^ @@.*/m', '', $m);
204-
} else {
205-
$t = $m->getReturnType();
206-
$stack = [
207-
$m->getDocComment(),
208-
$m->getName(),
209-
$m->isAbstract(),
210-
$m->isFinal(),
211-
$m->isStatic(),
212-
$m->isPublic(),
213-
$m->isPrivate(),
214-
$m->isProtected(),
215-
$m->returnsReference(),
216-
$t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t,
217-
];
218-
219-
foreach ($m->getParameters() as $p) {
220-
if (!isset($parametersWithUndefinedConstants[$p->name])) {
221-
$stack[] = (string) $p;
222-
} else {
223-
$t = $p->getType();
224-
$stack[] = $p->isOptional();
225-
$stack[] = $t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t;
226-
$stack[] = $p->isPassedByReference();
227-
$stack[] = $p->isVariadic();
228-
$stack[] = $p->getName();
229-
}
230-
}
202+
$t = $m->getReturnType();
203+
$stack = [
204+
$m->getDocComment(),
205+
$m->getName(),
206+
$m->isAbstract(),
207+
$m->isFinal(),
208+
$m->isStatic(),
209+
$m->isPublic(),
210+
$m->isPrivate(),
211+
$m->isProtected(),
212+
$m->returnsReference(),
213+
$t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t,
214+
];
231215

232-
yield implode(',', $stack);
216+
foreach ($m->getParameters() as $p) {
217+
if (!isset($parametersWithUndefinedConstants[$p->name])) {
218+
$stack[] = $this->extractParameterData($p);
219+
} else {
220+
$t = $p->getType();
221+
$stack[] = $p->isOptional();
222+
$stack[] = $t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t;
223+
$stack[] = $p->isPassedByReference();
224+
$stack[] = $p->isVariadic();
225+
$stack[] = $p->getName();
226+
}
233227
}
234228

229+
yield implode(',', $stack);
230+
235231
yield print_r($defaults, true);
236232
}
237233

@@ -259,4 +255,34 @@ private function generateSignature(\ReflectionClass $class): iterable
259255
yield print_r($class->name::getSubscribedServices(), true);
260256
}
261257
}
258+
259+
private function extractParameterData(\ReflectionParameter $p): string
260+
{
261+
if (false === $p->isOptional()) {
262+
return (string) $p;
263+
}
264+
265+
if (false === $p->isDefaultValueAvailable()) {
266+
return (string) $p;
267+
}
268+
269+
$defaultValue = $p->getDefaultValue();
270+
if (false === \is_object($defaultValue)) {
271+
return (string) $p;
272+
}
273+
274+
$class = \get_class($defaultValue);
275+
$type = $p->getType();
276+
277+
if (null === $type) {
278+
return 'untyped = '.$class;
279+
}
280+
281+
$typeName = (string) $type;
282+
if ($type instanceof \ReflectionNamedType) {
283+
$typeName = $type->getName();
284+
}
285+
286+
return $typeName.' = '.$class;
287+
}
262288
}

0 commit comments

Comments
 (0)