diff --git a/src/Rector/Contrib/Symfony/FrameworkBundle/AbstractToConstructorInjectionRector.php b/src/Rector/Contrib/Symfony/FrameworkBundle/AbstractToConstructorInjectionRector.php new file mode 100644 index 00000000000..9c6c05dfe92 --- /dev/null +++ b/src/Rector/Contrib/Symfony/FrameworkBundle/AbstractToConstructorInjectionRector.php @@ -0,0 +1,81 @@ +propertyNaming = $propertyNaming; + $this->classPropertyCollector = $classPropertyCollector; + $this->propertyFetchNodeFactory = $propertyFetchNodeFactory; + $this->serviceTypeForNameProvider = $serviceTypeForNameProvider; + $this->methodCallAnalyzer = $methodCallAnalyzer; + } + + /** + * @param MethodCall $methodCallNode + */ + public function refactor(Node $methodCallNode): ?Node + { + /** @var String_ $stringArgument */ + $stringArgument = $methodCallNode->args[0]->value; + + $serviceName = $stringArgument->value; + $serviceType = $this->serviceTypeForNameProvider->provideTypeForName($serviceName); + if ($serviceType === null) { + return null; + } + + $propertyName = $this->propertyNaming->typeToName($serviceType); + + $this->classPropertyCollector->addPropertyForClass( + (string) $methodCallNode->getAttribute(Attribute::CLASS_NAME), + [$serviceType], + $propertyName + ); + + return $this->propertyFetchNodeFactory->createLocalWithPropertyName($propertyName); + } +} diff --git a/src/Rector/Contrib/Symfony/FrameworkBundle/ContainerGetToConstructorInjectionRector.php b/src/Rector/Contrib/Symfony/FrameworkBundle/ContainerGetToConstructorInjectionRector.php index 101feb4d291..2c25718d299 100644 --- a/src/Rector/Contrib/Symfony/FrameworkBundle/ContainerGetToConstructorInjectionRector.php +++ b/src/Rector/Contrib/Symfony/FrameworkBundle/ContainerGetToConstructorInjectionRector.php @@ -3,15 +3,7 @@ namespace Rector\Rector\Contrib\Symfony\FrameworkBundle; use PhpParser\Node; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Scalar\String_; -use Rector\Builder\Class_\ClassPropertyCollector; -use Rector\Contract\Bridge\ServiceTypeForNameProviderInterface; -use Rector\Naming\PropertyNaming; use Rector\Node\Attribute; -use Rector\Node\PropertyFetchNodeFactory; -use Rector\NodeAnalyzer\MethodCallAnalyzer; -use Rector\Rector\AbstractRector; /** * Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#console @@ -36,33 +28,8 @@ use Rector\Rector\AbstractRector; * $this->someService * } */ -final class ContainerGetToConstructorInjectionRector extends AbstractRector +final class ContainerGetToConstructorInjectionRector extends AbstractToConstructorInjectionRector { - /** - * @var ClassPropertyCollector - */ - private $classPropertyCollector; - - /** - * @var PropertyNaming - */ - private $propertyNaming; - - /** - * @var PropertyFetchNodeFactory - */ - private $propertyFetchNodeFactory; - - /** - * @var ServiceTypeForNameProviderInterface - */ - private $serviceTypeForNameProvider; - - /** - * @var MethodCallAnalyzer - */ - private $methodCallAnalyzer; - /** * @var string[] */ @@ -71,20 +38,6 @@ final class ContainerGetToConstructorInjectionRector extends AbstractRector 'Symfony\Bundle\FrameworkBundle\Controller\Controller', ]; - public function __construct( - ClassPropertyCollector $classPropertyCollector, - PropertyNaming $propertyNaming, - PropertyFetchNodeFactory $propertyFetchNodeFactory, - ServiceTypeForNameProviderInterface $serviceTypeForNameProvider, - MethodCallAnalyzer $methodCallAnalyzer - ) { - $this->classPropertyCollector = $classPropertyCollector; - $this->propertyNaming = $propertyNaming; - $this->propertyFetchNodeFactory = $propertyFetchNodeFactory; - $this->serviceTypeForNameProvider = $serviceTypeForNameProvider; - $this->methodCallAnalyzer = $methodCallAnalyzer; - } - public function isCandidate(Node $node): bool { if (! $this->methodCallAnalyzer->isTypeAndMethod( @@ -99,29 +52,4 @@ final class ContainerGetToConstructorInjectionRector extends AbstractRector return in_array($parentClassName, $this->containerAwareParentTypes, true); } - - /** - * @param MethodCall $methodCallNode - */ - public function refactor(Node $methodCallNode): ?Node - { - /** @var String_ $serviceNameArgument */ - $serviceNameArgument = $methodCallNode->args[0]->value; - - $serviceName = $serviceNameArgument->value; - $serviceType = $this->serviceTypeForNameProvider->provideTypeForName($serviceName); - if ($serviceType === null) { - return null; - } - - $propertyName = $this->propertyNaming->typeToName($serviceType); - - $this->classPropertyCollector->addPropertyForClass( - (string) $methodCallNode->getAttribute(Attribute::CLASS_NAME), - [$serviceType], - $propertyName - ); - - return $this->propertyFetchNodeFactory->createLocalWithPropertyName($propertyName); - } } diff --git a/src/Rector/Contrib/Symfony/FrameworkBundle/GetToConstructorInjectionRector.php b/src/Rector/Contrib/Symfony/FrameworkBundle/GetToConstructorInjectionRector.php index fcc92e0cd6b..37933b30608 100644 --- a/src/Rector/Contrib/Symfony/FrameworkBundle/GetToConstructorInjectionRector.php +++ b/src/Rector/Contrib/Symfony/FrameworkBundle/GetToConstructorInjectionRector.php @@ -4,14 +4,6 @@ namespace Rector\Rector\Contrib\Symfony\FrameworkBundle; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Scalar\String_; -use Rector\Builder\Class_\ClassPropertyCollector; -use Rector\Contract\Bridge\ServiceTypeForNameProviderInterface; -use Rector\Naming\PropertyNaming; -use Rector\Node\Attribute; -use Rector\Node\PropertyFetchNodeFactory; -use Rector\NodeAnalyzer\MethodCallAnalyzer; -use Rector\Rector\AbstractRector; /** * Before: @@ -20,47 +12,8 @@ use Rector\Rector\AbstractRector; * After: * - $this->someService # where "someService" is type of the service */ -final class GetToConstructorInjectionRector extends AbstractRector +final class GetToConstructorInjectionRector extends AbstractToConstructorInjectionRector { - /** - * @var PropertyNaming - */ - private $propertyNaming; - - /** - * @var ClassPropertyCollector - */ - private $classPropertyCollector; - - /** - * @var PropertyFetchNodeFactory - */ - private $propertyFetchNodeFactory; - - /** - * @var ServiceTypeForNameProviderInterface - */ - private $serviceTypeForNameProvider; - - /** - * @var MethodCallAnalyzer - */ - private $methodCallAnalyzer; - - public function __construct( - PropertyNaming $propertyNaming, - ClassPropertyCollector $classPropertyCollector, - PropertyFetchNodeFactory $propertyFetchNodeFactory, - ServiceTypeForNameProviderInterface $serviceTypeForNameProvider, - MethodCallAnalyzer $methodCallAnalyzer - ) { - $this->propertyNaming = $propertyNaming; - $this->classPropertyCollector = $classPropertyCollector; - $this->propertyFetchNodeFactory = $propertyFetchNodeFactory; - $this->serviceTypeForNameProvider = $serviceTypeForNameProvider; - $this->methodCallAnalyzer = $methodCallAnalyzer; - } - public function isCandidate(Node $node): bool { if (! $node instanceof MethodCall) { @@ -73,29 +26,4 @@ final class GetToConstructorInjectionRector extends AbstractRector 'get' ); } - - /** - * @param MethodCall $methodCallNode - */ - public function refactor(Node $methodCallNode): ?Node - { - /** @var String_ $stringArgument */ - $stringArgument = $methodCallNode->args[0]->value; - - $serviceName = $stringArgument->value; - $serviceType = $this->serviceTypeForNameProvider->provideTypeForName($serviceName); - if ($serviceType === null) { - return null; - } - - $propertyName = $this->propertyNaming->typeToName($serviceType); - - $this->classPropertyCollector->addPropertyForClass( - (string) $methodCallNode->getAttribute(Attribute::CLASS_NAME), - [$serviceType], - $propertyName - ); - - return $this->propertyFetchNodeFactory->createLocalWithPropertyName($propertyName); - } }