diff --git a/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php b/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php index 512b2b46686..861622429c0 100644 --- a/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php +++ b/src/Rector/Architecture/RepositoryAsService/ServiceLocatorToDIRector.php @@ -2,17 +2,16 @@ namespace Rector\Rector\Architecture\RepositoryAsService; +use get_class; use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Stmt\Class_; -use Rector\Builder\Class_\VariableInfo; -use Rector\Builder\ConstructorMethodBuilder; +use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; +use PhpParser\Node\Scalar\String_; +use Rector\Builder\Class_\ClassPropertyCollector; use Rector\Contract\Bridge\RepositoryForDoctrineEntityProviderInterface; use Rector\Exception\Bridge\RectorProviderException; use Rector\Node\Attribute; -use Rector\Node\NodeFactory; use Rector\Node\PropertyFetchNodeFactory; use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\Rector\AbstractRector; @@ -33,27 +32,22 @@ final class ServiceLocatorToDIRector extends AbstractRector * @var RepositoryForDoctrineEntityProviderInterface */ private $repositoryForDoctrineEntityProvider; + /** - * @var ConstructorMethodBuilder + * @var ClassPropertyCollector */ - private $constructorMethodBuilder; - /** - * @var NodeFactory - */ - private $nodeFactory; + private $classPropertyCollector; public function __construct( MethodCallAnalyzer $methodCallAnalyzer, PropertyFetchNodeFactory $propertyFetchNodeFactory, - ConstructorMethodBuilder $constructorMethodBuilder, - NodeFactory $nodeFactory, - RepositoryForDoctrineEntityProviderInterface $repositoryForDoctrineEntityProvider + RepositoryForDoctrineEntityProviderInterface $repositoryForDoctrineEntityProvider, + ClassPropertyCollector $classPropertyCollector ) { $this->methodCallAnalyzer = $methodCallAnalyzer; $this->propertyFetchNodeFactory = $propertyFetchNodeFactory; $this->repositoryForDoctrineEntityProvider = $repositoryForDoctrineEntityProvider; - $this->constructorMethodBuilder = $constructorMethodBuilder; - $this->nodeFactory = $nodeFactory; + $this->classPropertyCollector = $classPropertyCollector; } public function isCandidate(Node $node): bool @@ -64,7 +58,7 @@ final class ServiceLocatorToDIRector extends AbstractRector $className = $node->getAttribute(Attribute::CLASS_NAME); - if($className === null){ + if ($className === null) { return false; } @@ -73,11 +67,15 @@ final class ServiceLocatorToDIRector extends AbstractRector public function refactor(Node $node): ?Node { - return $this->propertyFetchNodeFactory->createLocalWithPropertyName( + $this->classPropertyCollector->addPropertyForClass( + (string) $node->getAttribute(Attribute::CLASS_NAME), + [$this->repositoryFQN($node)], $this->repositoryVariableName($node) ); - return $node; + return $this->propertyFetchNodeFactory->createLocalWithPropertyName( + $this->repositoryVariableName($node) + ); } private function repositoryVariableName(Node $node): string @@ -94,15 +92,15 @@ final class ServiceLocatorToDIRector extends AbstractRector { $repositoryArgument = $node->args[0]->value; - if($repositoryArgument->class === null){ + if ($repositoryArgument instanceof String_) { $fqnOrAlias = $repositoryArgument->value; } - if($repositoryArgument->class instanceof Node\Name){ + if ($repositoryArgument->class instanceof Name) { $fqnOrAlias = $repositoryArgument->class->getAttribute(Attribute::TYPES)[0]; } - if($repositoryArgument->class instanceof Node\Name\FullyQualified){ + if ($repositoryArgument->class instanceof FullyQualified) { $fqnOrAlias = $repositoryArgument->class->toString(); } @@ -114,7 +112,7 @@ final class ServiceLocatorToDIRector extends AbstractRector throw new RectorProviderException(sprintf( 'A repository was not provided for "%s" entity by your "%s" class.', $fqnOrAlias, - \get_class($this->repositoryForDoctrineEntityProvider) + get_class($this->repositoryForDoctrineEntityProvider) )); } diff --git a/tests/Rector/Architecture/DoctrineRepositoryAsService/Source/RepositoryForDoctrineEntityProvider.php b/tests/Rector/Architecture/DoctrineRepositoryAsService/Source/RepositoryForDoctrineEntityProvider.php index e59b263c27b..494091ab7ed 100644 --- a/tests/Rector/Architecture/DoctrineRepositoryAsService/Source/RepositoryForDoctrineEntityProvider.php +++ b/tests/Rector/Architecture/DoctrineRepositoryAsService/Source/RepositoryForDoctrineEntityProvider.php @@ -15,12 +15,11 @@ final class RepositoryForDoctrineEntityProvider implements RepositoryForDoctrine public function provideRepositoryForEntity(string $name): ?string { - if($this->isAlias($name)){ + if ($this->isAlias($name)) { return $this->resoleFromAlias($name); } return $this->map[$name] ?? null; - } private function isAlias(string $name): bool @@ -34,8 +33,8 @@ final class RepositoryForDoctrineEntityProvider implements RepositoryForDoctrine $pattern = sprintf('/(%s{1}.*%s)/', $namespaceAlias, $simpleClassName); - foreach($this->map as $key => $value) { - if (preg_match($pattern, $key)){ + foreach ($this->map as $key => $value) { + if (preg_match($pattern, $key)) { return $value; } }