mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 03:10:50 +00:00
[Reconstructor] details
This commit is contained in:
parent
30c1624440
commit
aea9c41d3f
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user