diff --git a/src/PhpParser/AstResolver.php b/src/PhpParser/AstResolver.php index 545b43ab272..66bab9ccd6b 100644 --- a/src/PhpParser/AstResolver.php +++ b/src/PhpParser/AstResolver.php @@ -27,6 +27,7 @@ use PHPStan\Reflection\Php\PhpPropertyReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\TypeWithClassName; +use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\Reflection\MethodReflectionResolver; use Rector\Core\ValueObject\MethodName; use Rector\NodeNameResolver\NodeNameResolver; @@ -57,8 +58,8 @@ public function __construct( private readonly NodeNameResolver $nodeNameResolver, private readonly ReflectionProvider $reflectionProvider, private readonly NodeTypeResolver $nodeTypeResolver, - private readonly ClassLikeAstResolver $classLikeAstResolver, - private readonly MethodReflectionResolver $methodReflectionResolver + private readonly MethodReflectionResolver $methodReflectionResolver, + private readonly BetterNodeFinder $betterNodeFinder, ) { } @@ -80,16 +81,7 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR $classReflection = $methodReflection->getDeclaringClass(); $fileName = $classReflection->getFileName(); - // probably native PHP method → un-parseable - if ($fileName === null) { - return null; - } - $nodes = $this->parseFileNameToDecoratedNodes($fileName); - if ($nodes === []) { - return null; - } - $classLikeName = $classReflection->getName(); $methodName = $methodReflection->getName(); @@ -133,14 +125,7 @@ public function resolveClassMethodOrFunctionFromCall( public function resolveFunctionFromFunctionReflection(FunctionReflection $functionReflection): ?Function_ { $fileName = $functionReflection->getFileName(); - if ($fileName === null) { - return null; - } - $nodes = $this->parseFileNameToDecoratedNodes($fileName); - if ($nodes === []) { - return null; - } $functionName = $functionReflection->getName(); $functionNode = null; @@ -205,7 +190,27 @@ public function resolveClassMethodFromCall(MethodCall | StaticCall $call): ?Clas public function resolveClassFromClassReflection( ClassReflection $classReflection ): Trait_ | Class_ | Interface_ | Enum_ | null { - return $this->classLikeAstResolver->resolveClassFromClassReflection($classReflection, $this); + if ($classReflection->isBuiltin()) { + return null; + } + + $fileName = $classReflection->getFileName(); + $stmts = $this->parseFileNameToDecoratedNodes($fileName); + $className = $classReflection->getName(); + + /** @var Class_|Trait_|Interface_|Enum_|null $classLike */ + $classLike = $this->betterNodeFinder->findFirst( + $stmts, + function (Node $node) use ($className): bool { + if (! $node instanceof ClassLike) { + return false; + } + + return $this->nodeNameResolver->isName($node, $className); + } + ); + + return $classLike; } /** @@ -218,15 +223,7 @@ public function parseClassReflectionTraits(ClassReflection $classReflection): ar $traits = []; foreach ($classLikes as $classLike) { $fileName = $classLike->getFileName(); - if ($fileName === null) { - continue; - } - $nodes = $this->parseFileNameToDecoratedNodes($fileName); - if ($nodes === []) { - continue; - } - $traitName = $classLike->getName(); $traitNode = null; @@ -262,10 +259,6 @@ public function resolvePropertyFromPropertyReflection( $classReflection = $phpPropertyReflection->getDeclaringClass(); $fileName = $classReflection->getFileName(); - if ($fileName === null) { - return null; - } - $nodes = $this->parseFileNameToDecoratedNodes($fileName); if ($nodes === []) { return null; @@ -309,8 +302,13 @@ function (Node $node) use ($desiredClassName, $desiredPropertyName, &$propertyNo /** * @return Stmt[] */ - public function parseFileNameToDecoratedNodes(string $fileName): array + public function parseFileNameToDecoratedNodes(?string $fileName): array { + // probably native PHP → un-parseable + if ($fileName === null) { + return []; + } + if (isset($this->parsedFileNodes[$fileName])) { return $this->parsedFileNodes[$fileName]; } diff --git a/src/PhpParser/ClassLikeAstResolver.php b/src/PhpParser/ClassLikeAstResolver.php deleted file mode 100644 index f5018c24d56..00000000000 --- a/src/PhpParser/ClassLikeAstResolver.php +++ /dev/null @@ -1,63 +0,0 @@ -isBuiltin()) { - return null; - } - - $fileName = $classReflection->getFileName(); - - // probably internal class - if ($fileName === null) { - return null; - } - - $stmts = $astResolver->parseFileNameToDecoratedNodes($fileName); - if ($stmts === []) { - return null; - } - - $className = $classReflection->getName(); - /** @var Class_|Trait_|Interface_|Enum_|null $classLike */ - $classLike = $this->betterNodeFinder->findFirst( - $stmts, - function (Node $node) use ($className): bool { - if (! $node instanceof ClassLike) { - return false; - } - - return $this->nodeNameResolver->isName($node, $className); - } - ); - - return $classLike; - } -}