From 30bddcd8ab321e9e0fe7e24c480cca7d0df4b90a Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Mon, 26 Apr 2021 11:30:49 +0200 Subject: [PATCH] Move JMSInject part from AnnotatedPropertyInjectToConstructorInjectionRector to rector-symfony --- .../class_with_injects_35.php.inc | 30 ---- .../class_with_injects_7.php.inc | 41 ------ .../class_with_public_injects.php.inc | 30 ---- .../inject_from_var.php.inc | 31 ----- .../inject_from_var2.php.inc | 31 ----- .../inject_from_var3.php.inc | 47 ------- .../skip_parameter_inject.php.inc | 13 -- .../some_controller.php.inc | 35 ----- .../some_controller_2.php.inc | 30 ---- .../TagValueNodesTest.php | 33 ----- ...ertyInjectToConstructorInjectionRector.php | 18 +-- ...njectTagValueNodeToServiceTypeResolver.php | 41 ------ .../TypeAnalyzer/JMSDITypeResolver.php | 128 ------------------ 13 files changed, 1 insertion(+), 507 deletions(-) delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_35.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_7.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_public_injects.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var2.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var3.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/skip_parameter_inject.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/some_controller.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/some_controller_2.php.inc delete mode 100644 rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/TagValueNodesTest.php delete mode 100644 rules/DependencyInjection/TypeAnalyzer/InjectTagValueNodeToServiceTypeResolver.php delete mode 100644 rules/DependencyInjection/TypeAnalyzer/JMSDITypeResolver.php diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_35.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_35.php.inc deleted file mode 100644 index 32aa5549e438..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_35.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_7.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_7.php.inc deleted file mode 100644 index 160c74dc52b4..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_injects_7.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_public_injects.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_public_injects.php.inc deleted file mode 100644 index 1dc895ed130d..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/class_with_public_injects.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var.php.inc deleted file mode 100644 index 138ef4c017a1..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var2.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var2.php.inc deleted file mode 100644 index 7174e21f4a64..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var2.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var3.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var3.php.inc deleted file mode 100644 index d57c2077c41a..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/inject_from_var3.php.inc +++ /dev/null @@ -1,47 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/skip_parameter_inject.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/skip_parameter_inject.php.inc deleted file mode 100644 index b27f9cfeae62..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/skip_parameter_inject.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/some_controller_2.php.inc b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/some_controller_2.php.inc deleted file mode 100644 index b110412fca88..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/FixtureTagValueNodes/some_controller_2.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ - diff --git a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/TagValueNodesTest.php b/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/TagValueNodesTest.php deleted file mode 100644 index a30f3b558f9d..000000000000 --- a/rules-tests/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/TagValueNodesTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureTagValueNodes'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector.php b/rules/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector.php index 1913f60c30f8..049b56aa2ef2 100644 --- a/rules/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector.php +++ b/rules/DependencyInjection/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector.php @@ -17,7 +17,6 @@ use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\DependencyInjection\NodeAnalyzer\NetteInjectPropertyAnalyzer; -use Rector\DependencyInjection\TypeAnalyzer\InjectTagValueNodeToServiceTypeResolver; use Rector\FamilyTree\NodeAnalyzer\PropertyUsageAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -38,7 +37,7 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract /** * @var string[] */ - private const INJECT_ANNOTATION_CLASSES = ['JMS\DiExtraBundle\Annotation\Inject', 'DI\Annotation\Inject']; + private const INJECT_ANNOTATION_CLASSES = ['DI\Annotation\Inject']; /** * @var PropertyUsageAnalyzer @@ -50,11 +49,6 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract */ private $phpDocTypeChanger; - /** - * @var InjectTagValueNodeToServiceTypeResolver - */ - private $injectTagValueNodeToServiceTypeResolver; - /** * @var NetteInjectPropertyAnalyzer */ @@ -67,14 +61,12 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract public function __construct( PhpDocTypeChanger $phpDocTypeChanger, - InjectTagValueNodeToServiceTypeResolver $injectTagValueNodeToServiceTypeResolver, PropertyUsageAnalyzer $propertyUsageAnalyzer, NetteInjectPropertyAnalyzer $netteInjectPropertyAnalyzer, PhpDocTagRemover $phpDocTagRemover ) { $this->propertyUsageAnalyzer = $propertyUsageAnalyzer; $this->phpDocTypeChanger = $phpDocTypeChanger; - $this->injectTagValueNodeToServiceTypeResolver = $injectTagValueNodeToServiceTypeResolver; $this->netteInjectPropertyAnalyzer = $netteInjectPropertyAnalyzer; $this->phpDocTagRemover = $phpDocTagRemover; } @@ -147,14 +139,6 @@ public function refactor(Node $node): ?Node $serviceType = $phpDocInfo->getVarType(); } - if ($serviceType instanceof MixedType) { - $serviceType = $this->injectTagValueNodeToServiceTypeResolver->resolve( - $node, - $phpDocInfo, - $injectTagNode - ); - } - if ($serviceType instanceof MixedType) { return null; } diff --git a/rules/DependencyInjection/TypeAnalyzer/InjectTagValueNodeToServiceTypeResolver.php b/rules/DependencyInjection/TypeAnalyzer/InjectTagValueNodeToServiceTypeResolver.php deleted file mode 100644 index 4db83392686c..000000000000 --- a/rules/DependencyInjection/TypeAnalyzer/InjectTagValueNodeToServiceTypeResolver.php +++ /dev/null @@ -1,41 +0,0 @@ -jmsdiTypeResolver = $jmsdiTypeResolver; - } - - public function resolve( - Property $property, - PhpDocInfo $phpDocInfo, - DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode - ): Type { - // @see https://github.com/PHP-DI/PHP-DI/blob/master/src/Annotation/Inject.php - if ($doctrineAnnotationTagValueNode->getAnnotationClass() === 'JMS\DiExtraBundle\Annotation\Inject') { - return $this->jmsdiTypeResolver->resolve($property, $doctrineAnnotationTagValueNode); - } - - if ($doctrineAnnotationTagValueNode->getAnnotationClass() === 'Inject') { - return $phpDocInfo->getVarType(); - } - - throw new ShouldNotHappenException(); - } -} diff --git a/rules/DependencyInjection/TypeAnalyzer/JMSDITypeResolver.php b/rules/DependencyInjection/TypeAnalyzer/JMSDITypeResolver.php deleted file mode 100644 index c288a037cb67..000000000000 --- a/rules/DependencyInjection/TypeAnalyzer/JMSDITypeResolver.php +++ /dev/null @@ -1,128 +0,0 @@ -serviceMapProvider = $serviceMapProvider; - $this->phpDocInfoFactory = $phpDocInfoFactory; - $this->reflectionProvider = $reflectionProvider; - $this->nodeNameResolver = $nodeNameResolver; - $this->currentFileProvider = $currentFileProvider; - } - - public function resolve( - Property $property, - DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode - ): Type { - $serviceMap = $this->serviceMapProvider->provide(); - - $serviceName = $doctrineAnnotationTagValueNode->getValueWithoutQuotes( - 'serviceName' - ) ?: $doctrineAnnotationTagValueNode->getSilentValue() ?: $this->nodeNameResolver->getName($property); - - if ($serviceName) { - $serviceType = $this->resolveFromServiceName($serviceName, $serviceMap); - if (! $serviceType instanceof MixedType) { - return $serviceType; - } - } - - // 3. service is in @var annotation - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); - $varType = $phpDocInfo->getVarType(); - if (! $varType instanceof MixedType) { - return $varType; - } - - // the @var is missing and service name was not found → report it - $this->reportServiceNotFound($serviceName, $property); - - return new MixedType(); - } - - private function reportServiceNotFound(?string $serviceName, Property $property): void - { - if ($serviceName !== null) { - return; - } - - $file = $this->currentFileProvider->getFile(); - if (! $file instanceof File) { - throw new ShouldNotHappenException(); - } - - $errorMessage = sprintf('Service "%s" was not found in DI Container of your Symfony App.', $serviceName); - - $rectorError = new RectorError($errorMessage, $property->getLine()); - $file->addRectorError($rectorError); - } - - private function resolveFromServiceName(string $serviceName, ServiceMap $serviceMap): Type - { - // 1. service name-type - if ($this->reflectionProvider->hasClass($serviceName)) { - // single class service - return new ObjectType($serviceName); - } - - // 2. service name - if ($serviceMap->hasService($serviceName)) { - $serviceType = $serviceMap->getServiceType($serviceName); - if ($serviceType !== null) { - return $serviceType; - } - } - - return new MixedType(); - } -}