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

Skip to content

[DI] Save a ReflectionClass instantiation in AutowirePass #21488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 52 additions & 37 deletions src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,54 +219,69 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
continue;
}

try {
if (!$typeHint = $parameter->getClass()) {
// no default value? Then fail
if (!$parameter->isOptional()) {
if ($mustAutowire) {
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));
}

return array();
}
if (method_exists($parameter, 'getType')) {
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use $typeName to store the regex matches. Repurposing the same variable name for something else makes the code much harder to read.

Same is true for the output of $parameter->getType() btw

$typeName = 'callable' === $typeName[1] || 'array' === $typeName[1] ? null : $typeName[1];
}

if (!array_key_exists($index, $arguments)) {
// specifically pass the default value
$arguments[$index] = $parameter->getDefaultValue();
if (!$typeName) {
// no default value? Then fail
if (!$parameter->isOptional()) {
if ($mustAutowire) {
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));
}

continue;
return array();
}

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

if (isset($this->types[$typeHint->name])) {
$value = new Reference($this->types[$typeHint->name]);
continue;
}

if (null === $this->types) {
$this->populateAvailableTypes();
}

if (isset($this->types[$typeName])) {
$arguments[$index] = new Reference($this->types[$typeName]);
$didAutowire = true;

continue;
}

try {
$typeHint = new \ReflectionClass($typeName);
} catch (\ReflectionException $e) {
// Typehint against a non-existing class
$typeHint = false;
}

if ($typeHint) {
try {
$value = $this->createAutowiredDefinition($typeHint);
$didAutowire = true;
} else {
try {
$value = $this->createAutowiredDefinition($typeHint);
$didAutowire = true;
} catch (RuntimeException $e) {
if ($parameter->allowsNull()) {
$value = null;
} elseif ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
} else {
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
if (1 === $e->getCode() || $mustAutowire) {
throw $e;
}

return array();
} catch (RuntimeException $e) {
if ($parameter->allowsNull()) {
$value = null;
} elseif ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
} else {
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
if (1 === $e->getCode() || $mustAutowire) {
throw $e;
}

return array();
}
}
} catch (\ReflectionException $e) {
// Typehint against a non-existing class

} else {
if (!$parameter->isDefaultValueAvailable()) {
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);
Expand Down