diff --git a/packages/PHPUnit/src/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php b/packages/PHPUnit/src/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php index a45c1ad7c3af..cf2650824823 100644 --- a/packages/PHPUnit/src/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php +++ b/packages/PHPUnit/src/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php @@ -14,7 +14,6 @@ use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocTagNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\FileSystemRector\Parser\FileInfoParser; -use Rector\NodeContainer\ParsedNodesByType; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator; use Rector\Rector\AbstractPHPUnitRector; @@ -31,11 +30,6 @@ */ final class AddDoesNotPerformAssertionToNonAssertingTestRector extends AbstractPHPUnitRector { - /** - * @var ParsedNodesByType - */ - private $parsedNodesByType; - /** * @var FileInfoParser */ @@ -58,12 +52,10 @@ final class AddDoesNotPerformAssertionToNonAssertingTestRector extends AbstractP public function __construct( DocBlockManipulator $docBlockManipulator, - ParsedNodesByType $parsedNodesByType, FileInfoParser $fileInfoParser, ClassMethodReflectionFactory $classMethodReflectionFactory ) { $this->docBlockManipulator = $docBlockManipulator; - $this->parsedNodesByType = $parsedNodesByType; $this->fileInfoParser = $fileInfoParser; $this->classMethodReflectionFactory = $classMethodReflectionFactory; } @@ -234,12 +226,12 @@ private function hasNestedAssertCall(ClassMethod $classMethod): bool private function findClassMethod(Node $node): ?ClassMethod { if ($node instanceof MethodCall) { - $classMethod = $this->parsedNodesByType->findClassMethodByMethodCall($node); + $classMethod = $this->functionLikeParsedNodesFinder->findClassMethodByMethodCall($node); if ($classMethod !== null) { return $classMethod; } } elseif ($node instanceof StaticCall) { - $classMethod = $this->parsedNodesByType->findClassMethodByStaticCall($node); + $classMethod = $this->functionLikeParsedNodesFinder->findClassMethodByStaticCall($node); if ($classMethod !== null) { return $classMethod; } diff --git a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php index d6898268b490..879f97c2bd7c 100644 --- a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php +++ b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php @@ -14,7 +14,6 @@ use PhpParser\Node\Stmt\Return_; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocNode\Sensio\SensioTemplateTagValueNode; -use Rector\NodeContainer\ParsedNodesByType; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -36,18 +35,9 @@ final class TemplateAnnotationRector extends AbstractRector */ private $templateGuesser; - /** - * @var ParsedNodesByType - */ - private $parsedNodesByType; - - public function __construct( - TemplateGuesser $templateGuesser, - ParsedNodesByType $parsedNodesByType, - int $version = 3 - ) { + public function __construct(TemplateGuesser $templateGuesser, int $version = 3) + { $this->templateGuesser = $templateGuesser; - $this->parsedNodesByType = $parsedNodesByType; $this->version = $version; } @@ -242,7 +232,7 @@ private function refactorClassMethod( if ($returnNode !== null && $returnNode->expr instanceof MethodCall) { // go inside called method - $innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr); + $innerClassMethod = $this->functionLikeParsedNodesFinder->findClassMethodByMethodCall($returnNode->expr); if ($innerClassMethod !== null) { $this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode); diff --git a/src/NodeContainer/FunctionLikeParsedNodesFinder.php b/src/NodeContainer/FunctionLikeParsedNodesFinder.php new file mode 100644 index 000000000000..a2e288148b60 --- /dev/null +++ b/src/NodeContainer/FunctionLikeParsedNodesFinder.php @@ -0,0 +1,77 @@ +parsedNodesByType = $parsedNodesByType; + $this->nameResolver = $nameResolver; + $this->nodeTypeResolver = $nodeTypeResolver; + } + + public function findClassMethodByMethodCall(MethodCall $methodCall): ?ClassMethod + { + /** @var string|null $className */ + $className = $methodCall->getAttribute(AttributeKey::CLASS_NAME); + if ($className === null) { + return null; + } + + $methodName = $this->nameResolver->getName($methodCall->name); + if ($methodName === null) { + return null; + } + + return $this->parsedNodesByType->findMethod($methodName, $className); + } + + public function findClassMethodByStaticCall(StaticCall $staticCall): ?ClassMethod + { + $methodName = $this->nameResolver->getName($staticCall->name); + if ($methodName === null) { + return null; + } + + $objectType = $this->nodeTypeResolver->resolve($staticCall->class); + + $classNames = TypeUtils::getDirectClassNames($objectType); + foreach ($classNames as $className) { + $foundMethod = $this->parsedNodesByType->findMethod($methodName, $className); + if ($foundMethod !== null) { + return $foundMethod; + } + } + + return null; + } +} diff --git a/src/NodeContainer/ParsedNodesByType.php b/src/NodeContainer/ParsedNodesByType.php index 5bd9e54b2e9a..194a22020efd 100644 --- a/src/NodeContainer/ParsedNodesByType.php +++ b/src/NodeContainer/ParsedNodesByType.php @@ -24,7 +24,6 @@ use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -use PHPStan\Type\TypeUtils; use PHPStan\Type\UnionType; use Rector\Exception\NotImplementedException; use Rector\Exception\ShouldNotHappenException; @@ -195,42 +194,6 @@ public function findFunction(string $name): ?Function_ return $this->simpleParsedNodesByType[Function_::class][$name] ?? null; } - public function findClassMethodByMethodCall(MethodCall $methodCall): ?ClassMethod - { - /** @var string|null $className */ - $className = $methodCall->getAttribute(AttributeKey::CLASS_NAME); - if ($className === null) { - return null; - } - - $methodName = $this->nameResolver->getName($methodCall->name); - if ($methodName === null) { - return null; - } - - return $this->findMethod($methodName, $className); - } - - public function findClassMethodByStaticCall(StaticCall $staticCall): ?ClassMethod - { - $methodName = $this->nameResolver->getName($staticCall->name); - if ($methodName === null) { - return null; - } - - $objectType = $this->nodeTypeResolver->resolve($staticCall->class); - - $classNames = TypeUtils::getDirectClassNames($objectType); - foreach ($classNames as $className) { - $foundMethod = $this->findMethod($methodName, $className); - if ($foundMethod !== null) { - return $foundMethod; - } - } - - return null; - } - public function findMethod(string $methodName, string $className): ?ClassMethod { if (isset($this->methodsByType[$className][$methodName])) { diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 4017529c645d..9460e4477c0a 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -23,6 +23,7 @@ use Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector; use Rector\Exclusion\ExclusionManager; use Rector\NodeContainer\ClassLikeParsedNodesFinder; +use Rector\NodeContainer\FunctionLikeParsedNodesFinder; use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator; @@ -86,6 +87,10 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn * @var ClassLikeParsedNodesFinder */ protected $classLikeParsedNodesFinder; + /** + * @var FunctionLikeParsedNodesFinder + */ + protected $functionLikeParsedNodesFinder; /** * Run once in the every end of one processed file @@ -107,7 +112,8 @@ public function autowireAbstractRectorDependencies( PhpDocInfoPrinter $phpDocInfoPrinter, DocBlockManipulator $docBlockManipulator, StaticTypeMapper $staticTypeMapper, - ClassLikeParsedNodesFinder $classLikeParsedNodesFinder + ClassLikeParsedNodesFinder $classLikeParsedNodesFinder, + FunctionLikeParsedNodesFinder $functionLikeParsedNodesFinder ): void { $this->symfonyStyle = $symfonyStyle; $this->phpVersionProvider = $phpVersionProvider; @@ -119,6 +125,7 @@ public function autowireAbstractRectorDependencies( $this->docBlockManipulator = $docBlockManipulator; $this->staticTypeMapper = $staticTypeMapper; $this->classLikeParsedNodesFinder = $classLikeParsedNodesFinder; + $this->functionLikeParsedNodesFinder = $functionLikeParsedNodesFinder; } /**