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

Skip to content

Commit b4ff1c8

Browse files
minor #21488 [DI] Save a ReflectionClass instantiation in AutowirePass (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Save a ReflectionClass instantiation in AutowirePass | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - No behavioral change, just a small refacto that I'm needing for two different PRs I'm working on - will make reviewing them easier also. Best reviewed by ignoring whitespaces. Commits ------- b893c72 [DI] Save a ReflectionClass instanciation in AutowirePass
2 parents b3b3dac + b893c72 commit b4ff1c8

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -219,54 +219,69 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
219219
continue;
220220
}
221221

222-
try {
223-
if (!$typeHint = $parameter->getClass()) {
224-
// no default value? Then fail
225-
if (!$parameter->isOptional()) {
226-
if ($mustAutowire) {
227-
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
228-
}
229-
230-
return array();
231-
}
222+
if (method_exists($parameter, 'getType')) {
223+
if ($typeName = $parameter->getType()) {
224+
$typeName = $typeName->isBuiltin() ? null : ($typeName instanceof \ReflectionNamedType ? $typeName->getName() : $typeName->__toString());
225+
}
226+
} elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $typeName)) {
227+
$typeName = 'callable' === $typeName[1] || 'array' === $typeName[1] ? null : $typeName[1];
228+
}
232229

233-
if (!array_key_exists($index, $arguments)) {
234-
// specifically pass the default value
235-
$arguments[$index] = $parameter->getDefaultValue();
230+
if (!$typeName) {
231+
// no default value? Then fail
232+
if (!$parameter->isOptional()) {
233+
if ($mustAutowire) {
234+
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
236235
}
237236

238-
continue;
237+
return array();
239238
}
240239

241-
if (null === $this->types) {
242-
$this->populateAvailableTypes();
240+
if (!array_key_exists($index, $arguments)) {
241+
// specifically pass the default value
242+
$arguments[$index] = $parameter->getDefaultValue();
243243
}
244244

245-
if (isset($this->types[$typeHint->name])) {
246-
$value = new Reference($this->types[$typeHint->name]);
245+
continue;
246+
}
247+
248+
if (null === $this->types) {
249+
$this->populateAvailableTypes();
250+
}
251+
252+
if (isset($this->types[$typeName])) {
253+
$arguments[$index] = new Reference($this->types[$typeName]);
254+
$didAutowire = true;
255+
256+
continue;
257+
}
258+
259+
try {
260+
$typeHint = new \ReflectionClass($typeName);
261+
} catch (\ReflectionException $e) {
262+
// Typehint against a non-existing class
263+
$typeHint = false;
264+
}
265+
266+
if ($typeHint) {
267+
try {
268+
$value = $this->createAutowiredDefinition($typeHint);
247269
$didAutowire = true;
248-
} else {
249-
try {
250-
$value = $this->createAutowiredDefinition($typeHint);
251-
$didAutowire = true;
252-
} catch (RuntimeException $e) {
253-
if ($parameter->allowsNull()) {
254-
$value = null;
255-
} elseif ($parameter->isDefaultValueAvailable()) {
256-
$value = $parameter->getDefaultValue();
257-
} else {
258-
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
259-
if (1 === $e->getCode() || $mustAutowire) {
260-
throw $e;
261-
}
262-
263-
return array();
270+
} catch (RuntimeException $e) {
271+
if ($parameter->allowsNull()) {
272+
$value = null;
273+
} elseif ($parameter->isDefaultValueAvailable()) {
274+
$value = $parameter->getDefaultValue();
275+
} else {
276+
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
277+
if (1 === $e->getCode() || $mustAutowire) {
278+
throw $e;
264279
}
280+
281+
return array();
265282
}
266283
}
267-
} catch (\ReflectionException $e) {
268-
// Typehint against a non-existing class
269-
284+
} else {
270285
if (!$parameter->isDefaultValueAvailable()) {
271286
if ($mustAutowire) {
272287
throw new RuntimeException(sprintf('Cannot autowire argument $%s of method %s::%s() for service "%s": %s.', $parameter->name, $reflectionMethod->class, $reflectionMethod->name, $this->currentId, $e->getMessage()), 0, $e);

0 commit comments

Comments
 (0)