From 081e240640bf3975f34bdb83e008d816583c5af0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 20 Mar 2021 01:37:33 +0100 Subject: [PATCH] [DX] Remove isInClassNamed() (#5914) --- .../NodeNameResolver/NodeNameResolver.php | 51 +------------------ ...PropertyClassMethodTypeToDynamicRector.php | 15 +++++- ...StaticPropertyFetchTypeToDynamicRector.php | 14 ++++- .../InferParamFromClassMethodReturnRector.php | 19 +++++-- .../InferParamFromClassMethodReturn.php | 6 +-- .../Assign/AssignNewDocParserRector.php | 19 ++++--- .../ChangeOriginalTypeToCustomRector.php | 17 +++++-- ...solverValueInConstantClassMethodRector.php | 14 ++++- ...veAnnotationRegistryRegisterFileRector.php | 20 ++++++-- 9 files changed, 96 insertions(+), 79 deletions(-) diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index a742be258133..bc1b926b26f5 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -10,12 +10,9 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\ClassLike; -use PHPStan\Reflection\ReflectionProvider; -use PHPStan\Type\ObjectType; use Rector\CodingStyle\Naming\ClassNaming; use Rector\Core\Contract\Rector\RectorInterface; use Rector\Core\Exception\ShouldNotHappenException; @@ -24,7 +21,6 @@ use Rector\NodeNameResolver\Contract\NodeNameResolverInterface; use Rector\NodeNameResolver\Regex\RegexPatternDetector; use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider; -use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\SmartFileSystem\SmartFileInfo; final class NodeNameResolver @@ -59,11 +55,6 @@ final class NodeNameResolver */ private $betterStandardPrinter; - /** - * @var ReflectionProvider - */ - private $reflectionProvider; - /** * @param NodeNameResolverInterface[] $nodeNameResolvers */ @@ -72,7 +63,6 @@ public function __construct( BetterStandardPrinter $betterStandardPrinter, CurrentFileInfoProvider $currentFileInfoProvider, ClassNaming $classNaming, - ReflectionProvider $reflectionProvider, array $nodeNameResolvers = [] ) { $this->regexPatternDetector = $regexPatternDetector; @@ -80,7 +70,6 @@ public function __construct( $this->currentFileInfoProvider = $currentFileInfoProvider; $this->betterStandardPrinter = $betterStandardPrinter; $this->classNaming = $classNaming; - $this->reflectionProvider = $reflectionProvider; } /** @@ -102,7 +91,7 @@ public function isNames(Node $node, array $names): bool */ public function isName($node, string $name): bool { - if ($node instanceof MethodCall) { + if ($node instanceof MethodCall || $node instanceof StaticCall) { $message = sprintf( 'Name called on "%s" is not possible. Use $this->getName($node->name) instead', get_class($node) @@ -212,44 +201,6 @@ public function endsWith(string $currentName, string $expectedName): bool return (bool) Strings::match($currentName, $suffixNamePattern); } - /** - * @param ObjectType[] $desiredObjectTypes - */ - public function isInClassNames(Node $node, array $desiredObjectTypes): bool - { - $classNode = $node->getAttribute(AttributeKey::CLASS_NODE); - if ($classNode === null) { - return false; - } - - foreach ($desiredObjectTypes as $desiredObjectType) { - if ($this->isInClassNamed($classNode, $desiredObjectType)) { - return true; - } - } - - return false; - } - - public function isInClassNamed(Node $node, ObjectType $objectType): bool - { - $className = $node->getAttribute(AttributeKey::CLASS_NAME); - if ($className === null) { - return false; - } - - if (! $this->reflectionProvider->hasClass($className)) { - return false; - } - - $classReflection = $this->reflectionProvider->getClass($className); - if ($classReflection->getName() === $objectType->getClassName()) { - return true; - } - - return $classReflection->isSubclassOf($objectType->getClassName()); - } - /** * @param string|Name|Identifier|ClassLike $name */ diff --git a/rules/RemovingStatic/Rector/Property/DesiredPropertyClassMethodTypeToDynamicRector.php b/rules/RemovingStatic/Rector/Property/DesiredPropertyClassMethodTypeToDynamicRector.php index 874fd2fe9667..544498251986 100644 --- a/rules/RemovingStatic/Rector/Property/DesiredPropertyClassMethodTypeToDynamicRector.php +++ b/rules/RemovingStatic/Rector/Property/DesiredPropertyClassMethodTypeToDynamicRector.php @@ -7,9 +7,12 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; use Rector\Core\Configuration\Option; use Rector\Core\Rector\AbstractRector; +use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\PackageBuilder\Parameter\ParameterProvider; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -74,8 +77,18 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + /** @var Scope $scope */ + $scope = $node->getAttribute(AttributeKey::SCOPE); + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + $classObjectType = new ObjectType($classReflection->getName()); + foreach ($this->staticObjectTypes as $staticObjectType) { - if (! $this->nodeNameResolver->isInClassNamed($node, $staticObjectType)) { + if (! $staticObjectType->isSuperTypeOf($classObjectType)->yes()) { continue; } diff --git a/rules/RemovingStatic/Rector/StaticPropertyFetch/DesiredStaticPropertyFetchTypeToDynamicRector.php b/rules/RemovingStatic/Rector/StaticPropertyFetch/DesiredStaticPropertyFetchTypeToDynamicRector.php index 43fe2600d5b4..7b0e57725b44 100644 --- a/rules/RemovingStatic/Rector/StaticPropertyFetch/DesiredStaticPropertyFetchTypeToDynamicRector.php +++ b/rules/RemovingStatic/Rector/StaticPropertyFetch/DesiredStaticPropertyFetchTypeToDynamicRector.php @@ -9,6 +9,8 @@ use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; use Rector\Core\Configuration\Option; use Rector\Core\Rector\AbstractRector; @@ -83,9 +85,19 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + /** @var Scope $scope */ + $scope = $node->getAttribute(AttributeKey::SCOPE); + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + $classObjectType = new ObjectType($classReflection->getName()); + // A. remove local fetch foreach ($this->staticObjectTypes as $staticObjectType) { - if (! $this->nodeNameResolver->isInClassNamed($node, $staticObjectType)) { + if (! $staticObjectType->isSuperTypeOf($classObjectType)->yes()) { continue; } diff --git a/rules/Restoration/Rector/ClassMethod/InferParamFromClassMethodReturnRector.php b/rules/Restoration/Rector/ClassMethod/InferParamFromClassMethodReturnRector.php index f0768eaae40b..8583c0c94433 100644 --- a/rules/Restoration/Rector/ClassMethod/InferParamFromClassMethodReturnRector.php +++ b/rules/Restoration/Rector/ClassMethod/InferParamFromClassMethodReturnRector.php @@ -8,6 +8,8 @@ use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; @@ -155,7 +157,7 @@ public function refactor(Node $node): ?Node } /** - * @param array $configuration + * @param array $configuration */ public function configure(array $configuration): void { @@ -169,10 +171,17 @@ private function matchReturnClassMethod( ClassMethod $classMethod, InferParamFromClassMethodReturn $inferParamFromClassMethodReturn ): ?ClassMethod { - if (! $this->nodeNameResolver->isInClassNamed( - $classMethod, - $inferParamFromClassMethodReturn->getObjectType() - )) { + $scope = $classMethod->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return null; + } + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + if (! $classReflection->isSubclassOf($inferParamFromClassMethodReturn->getClass())) { return null; } diff --git a/rules/Restoration/ValueObject/InferParamFromClassMethodReturn.php b/rules/Restoration/ValueObject/InferParamFromClassMethodReturn.php index 46857e2ba7d2..04f457bd2da9 100644 --- a/rules/Restoration/ValueObject/InferParamFromClassMethodReturn.php +++ b/rules/Restoration/ValueObject/InferParamFromClassMethodReturn.php @@ -4,8 +4,6 @@ namespace Rector\Restoration\ValueObject; -use PHPStan\Type\ObjectType; - final class InferParamFromClassMethodReturn { /** @@ -30,9 +28,9 @@ public function __construct(string $class, string $paramMethod, string $returnMe $this->returnMethod = $returnMethod; } - public function getObjectType(): ObjectType + public function getClass(): string { - return new ObjectType($this->class); + return $this->class; } public function getParamMethod(): string diff --git a/utils/doctrine-annotation-parser-syncer/src/Rector/Assign/AssignNewDocParserRector.php b/utils/doctrine-annotation-parser-syncer/src/Rector/Assign/AssignNewDocParserRector.php index 13a27e0bb623..1f0d28d329d5 100644 --- a/utils/doctrine-annotation-parser-syncer/src/Rector/Assign/AssignNewDocParserRector.php +++ b/utils/doctrine-annotation-parser-syncer/src/Rector/Assign/AssignNewDocParserRector.php @@ -8,8 +8,10 @@ use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\New_; use PhpParser\Node\Name\FullyQualified; -use PHPStan\Type\ObjectType; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use Rector\Core\Rector\AbstractRector; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -17,7 +19,7 @@ final class AssignNewDocParserRector extends AbstractRector implements ClassSyncerRectorInterface { /** - * @return array> + * @return array> */ public function getNodeTypes(): array { @@ -29,10 +31,15 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isInClassNamed( - $node, - new ObjectType('Doctrine\Common\Annotations\AnnotationReader') - )) { + /** @var Scope $scope */ + $scope = $node->getAttribute(AttributeKey::SCOPE); + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + if ($classReflection->getName() !== 'Doctrine\Common\Annotations\AnnotationReader') { return null; } diff --git a/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/ChangeOriginalTypeToCustomRector.php b/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/ChangeOriginalTypeToCustomRector.php index 0840e1dbe4a0..01d50b13e256 100644 --- a/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/ChangeOriginalTypeToCustomRector.php +++ b/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/ChangeOriginalTypeToCustomRector.php @@ -7,9 +7,11 @@ use PhpParser\Node; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Type\ObjectType; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\MethodName; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -29,10 +31,15 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isInClassNamed( - $node, - new ObjectType('Doctrine\Common\Annotations\AnnotationReader') - )) { + /** @var Scope $scope */ + $scope = $node->getAttribute(AttributeKey::SCOPE); + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + if ($classReflection->getName() !== 'Doctrine\Common\Annotations\AnnotationReader') { return null; } diff --git a/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/LogIdentifierAndResolverValueInConstantClassMethodRector.php b/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/LogIdentifierAndResolverValueInConstantClassMethodRector.php index d2e41d0b2dab..94cbd3028c5b 100644 --- a/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/LogIdentifierAndResolverValueInConstantClassMethodRector.php +++ b/utils/doctrine-annotation-parser-syncer/src/Rector/ClassMethod/LogIdentifierAndResolverValueInConstantClassMethodRector.php @@ -10,9 +10,11 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; -use PHPStan\Type\ObjectType; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use Rector\Core\Rector\AbstractRector; use Rector\DoctrineAnnotationGenerated\DataCollector\ResolvedConstantStaticCollector; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -35,7 +37,15 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->nodeNameResolver->isInClassNamed($node, new ObjectType('Doctrine\Common\Annotations\DocParser'))) { + /** @var Scope $scope */ + $scope = $node->getAttribute(AttributeKey::SCOPE); + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } + + if ($classReflection->getName() !== 'Doctrine\Common\Annotations\DocParser') { return null; } diff --git a/utils/doctrine-annotation-parser-syncer/src/Rector/StaticCall/RemoveAnnotationRegistryRegisterFileRector.php b/utils/doctrine-annotation-parser-syncer/src/Rector/StaticCall/RemoveAnnotationRegistryRegisterFileRector.php index b4e3d32b98de..f6d6fbbd7eca 100644 --- a/utils/doctrine-annotation-parser-syncer/src/Rector/StaticCall/RemoveAnnotationRegistryRegisterFileRector.php +++ b/utils/doctrine-annotation-parser-syncer/src/Rector/StaticCall/RemoveAnnotationRegistryRegisterFileRector.php @@ -6,8 +6,11 @@ use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -53,12 +56,19 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $desiredObjectTypes = [ - new ObjectType('Doctrine\Common\Annotations\DocParser'), - new ObjectType('Doctrine\Common\Annotations\AnnotationReader'), - ]; + $scope = $node->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return null; + } + + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return null; + } - if (! $this->nodeNameResolver->isInClassNames($node, $desiredObjectTypes)) { + if (! $classReflection->isSubclassOf( + 'Doctrine\Common\Annotations\DocParser' + ) && ! $classReflection->isSubclassOf('Doctrine\Common\Annotations\AnnotationReader')) { return null; }