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
This commit is contained in:
commit
b4ff1c8767
@ -219,8 +219,15 @@ class AutowirePass extends AbstractRecursivePass
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (method_exists($parameter, 'getType')) {
|
||||||
if (!$typeHint = $parameter->getClass()) {
|
if ($typeName = $parameter->getType()) {
|
||||||
|
$typeName = $typeName->isBuiltin() ? null : ($typeName instanceof \ReflectionNamedType ? $typeName->getName() : $typeName->__toString());
|
||||||
|
}
|
||||||
|
} elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $typeName)) {
|
||||||
|
$typeName = 'callable' === $typeName[1] || 'array' === $typeName[1] ? null : $typeName[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$typeName) {
|
||||||
// no default value? Then fail
|
// no default value? Then fail
|
||||||
if (!$parameter->isOptional()) {
|
if (!$parameter->isOptional()) {
|
||||||
if ($mustAutowire) {
|
if ($mustAutowire) {
|
||||||
@ -242,10 +249,21 @@ class AutowirePass extends AbstractRecursivePass
|
|||||||
$this->populateAvailableTypes();
|
$this->populateAvailableTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->types[$typeHint->name])) {
|
if (isset($this->types[$typeName])) {
|
||||||
$value = new Reference($this->types[$typeHint->name]);
|
$arguments[$index] = new Reference($this->types[$typeName]);
|
||||||
$didAutowire = true;
|
$didAutowire = true;
|
||||||
} else {
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$typeHint = new \ReflectionClass($typeName);
|
||||||
|
} catch (\ReflectionException $e) {
|
||||||
|
// Typehint against a non-existing class
|
||||||
|
$typeHint = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($typeHint) {
|
||||||
try {
|
try {
|
||||||
$value = $this->createAutowiredDefinition($typeHint);
|
$value = $this->createAutowiredDefinition($typeHint);
|
||||||
$didAutowire = true;
|
$didAutowire = true;
|
||||||
@ -263,10 +281,7 @@ class AutowirePass extends AbstractRecursivePass
|
|||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} catch (\ReflectionException $e) {
|
|
||||||
// Typehint against a non-existing class
|
|
||||||
|
|
||||||
if (!$parameter->isDefaultValueAvailable()) {
|
if (!$parameter->isDefaultValueAvailable()) {
|
||||||
if ($mustAutowire) {
|
if ($mustAutowire) {
|
||||||
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);
|
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);
|
||||||
|
Reference in New Issue
Block a user