[Reconstructor] details

This commit is contained in:
TomasVotruba 2017-07-16 19:54:09 +02:00
parent 30c1624440
commit aea9c41d3f
2 changed files with 16 additions and 17 deletions

View File

@ -9,7 +9,8 @@ This tool will *reconstruct* (change) your code - **run it only in a new clean g
## All Reconstructors
- `InjectAnnotationToConstructorReconstructor`
- `InjectAnnotationToConstructorReconstructor` ([Nette](https://github.com/nette/))
- `NamedServicesToConstructorReconstructor` ([Symfony](https://github.com/symfony/))
## Install

View File

@ -71,22 +71,13 @@ final class NamedServicesToConstructorReconstructor implements ReconstructorInte
continue;
}
$methodCallNode = $methodCallNode->var;
// 3. Accept only "$this->get()"
if ($methodCallNode->name !== 'get') {
if ($methodCallNode->var->name !== 'get') {
continue;
}
// 4. Accept only strings in "$this->get('string')"
$argument = $methodCallNode->args[0]->value;
if (! $methodCallNode->args[0]->value instanceof String_) {
continue;
}
/** @var String_ $argument */
$serviceName = $argument->value;
$serviceName = $this->getServiceNameFromGetCall($methodCallNode->var);
$container = $this->getContainerFromKernelClass();
if (! $container->has($serviceName)) {
// service name could not be found
@ -98,12 +89,8 @@ final class NamedServicesToConstructorReconstructor implements ReconstructorInte
// 6. Save Services
$serviceType = get_class($service);
$propertyName = $this->createPropertyNameFromClass($serviceType);
$collectedServices[$propertyName] = $serviceType;
// 7. Replace "$this->get()" => "$this->{$propertyName}"
// A.
// 7.1 Replace "$this" with "$this->propertyName"
// 7. Replace "$this->get()->" => "$this->$propertyName->"
$methodCallNode->var = new PropertyFetch(
new Variable('this', [
'name' => $propertyName
@ -141,4 +128,15 @@ final class NamedServicesToConstructorReconstructor implements ReconstructorInte
return lcfirst($lastNamePart);
}
private function getServiceNameFromGetCall(MethodCall $methodCallNode): ?string
{
$argument = $methodCallNode->args[0]->value;
if (! $methodCallNode->args[0]->value instanceof String_) {
return null;
}
/** @var String_ $argument */
return $argument->value;
}
}