From f6a37f8cb76a08d364788fd6724ccad06dae0e0a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 27 Jan 2026 14:53:35 +0100 Subject: [PATCH] make PropertyFetchFinder more generic (to be used in rector-phpunit) --- .../Rector/Class_/SleepToSerializeRector.php | 2 +- src/PhpParser/NodeFinder/PropertyFetchFinder.php | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rules/Php85/Rector/Class_/SleepToSerializeRector.php b/rules/Php85/Rector/Class_/SleepToSerializeRector.php index 8eb2583bf5b..a738cc852d4 100644 --- a/rules/Php85/Rector/Class_/SleepToSerializeRector.php +++ b/rules/Php85/Rector/Class_/SleepToSerializeRector.php @@ -5,8 +5,8 @@ namespace Rector\Php85\Rector\Class_; use PhpParser\Node; -use PhpParser\Node\Expr\Array_; use PhpParser\Node\ArrayItem; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; diff --git a/src/PhpParser/NodeFinder/PropertyFetchFinder.php b/src/PhpParser/NodeFinder/PropertyFetchFinder.php index d2f41462c5b..ee9fbbcf43c 100644 --- a/src/PhpParser/NodeFinder/PropertyFetchFinder.php +++ b/src/PhpParser/NodeFinder/PropertyFetchFinder.php @@ -17,6 +17,7 @@ use PhpParser\Node\Param; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Trait_; use PHPStan\Analyser\Scope; @@ -71,13 +72,14 @@ public function findPrivatePropertyFetches( } /** + * @api used by other Rector packages * @return PropertyFetch[]|StaticPropertyFetch[]|NullsafePropertyFetch[] */ - public function findLocalPropertyFetchesByName(Class_ $class, string $paramName): array + public function findLocalPropertyFetchesByName(Class_|ClassMethod $node, string $paramName): array { /** @var PropertyFetch[]|StaticPropertyFetch[]|NullsafePropertyFetch[] $foundPropertyFetches */ $foundPropertyFetches = $this->betterNodeFinder->find( - $this->resolveNodesToLocate($class), + $this->resolveNodesToLocate($node), function (Node $subNode) use ($paramName): bool { if ($subNode instanceof PropertyFetch) { return $this->propertyFetchAnalyzer->isLocalPropertyFetchName($subNode, $paramName); @@ -162,14 +164,18 @@ public function isLocalPropertyFetchByName(Expr $expr, Class_|Trait_ $class, str /** * @return Stmt[] */ - private function resolveNodesToLocate(Class_ $class): array + private function resolveNodesToLocate(Class_|ClassMethod $node): array { + if ($node instanceof ClassMethod) { + return [$node]; + } + $propertyWithHooks = array_filter( - $class->getProperties(), + $node->getProperties(), fn (Property $property): bool => $property->hooks !== [] ); - return [...$propertyWithHooks, ...$class->getMethods()]; + return [...$propertyWithHooks, ...$node->getMethods()]; } /**