From 7e2495e5f572bb566a845a0e1a6329cf141f872d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 19 Jan 2024 21:03:24 +0700 Subject: [PATCH] [Php70] Skip inside Encapsed on ThisCallOnStaticMethodToStaticCallRector (#5481) * [Php70] Skip inside Encapsed on ThisCallOnStaticMethodToStaticCallRector * Fixed :tada: * fix phpstan --- .../Fixture/skip_inside_encapsed.php.inc | 13 ++++++ ...isCallOnStaticMethodToStaticCallRector.php | 40 ++++++++++++------- 2 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 rules-tests/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector/Fixture/skip_inside_encapsed.php.inc diff --git a/rules-tests/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector/Fixture/skip_inside_encapsed.php.inc b/rules-tests/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector/Fixture/skip_inside_encapsed.php.inc new file mode 100644 index 00000000000..5fb764731cd --- /dev/null +++ b/rules-tests/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector/Fixture/skip_inside_encapsed.php.inc @@ -0,0 +1,13 @@ +getLogDate()}"; + } + + private static function getLogDate(): string { + return 'foo'; + } +} diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index 26fc537fccc..f20cf756272 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -9,7 +9,9 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Stmt\Class_; +use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\Php\PhpMethodReflection; @@ -28,6 +30,8 @@ */ final class ThisCallOnStaticMethodToStaticCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface { + private bool $hasChanged = false; + public function __construct( private readonly StaticAnalyzer $staticAnalyzer, private readonly ReflectionResolver $reflectionResolver, @@ -101,13 +105,27 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node return null; } - $hasChanged = false; + $this->hasChanged = false; + + $this->processThisToStatic($node, $classReflection); + + if ($this->hasChanged) { + return $node; + } + + return null; + } + + private function processThisToStatic(Class_ $class, ClassReflection $classReflection): void + { + $this->traverseNodesWithCallable($class, function (Node $subNode) use ( + $class, + $classReflection + ): null|StaticCall|int { + if ($subNode instanceof Encapsed) { + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } - $this->traverseNodesWithCallable($node, function (Node $subNode) use ( - $node, - $classReflection, - &$hasChanged - ): ?StaticCall { if (! $subNode instanceof MethodCall) { return null; } @@ -129,7 +147,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node return null; } - $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $node); + $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $class); if (! $isStaticMethod) { return null; } @@ -138,17 +156,11 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node return null; } - $hasChanged = true; + $this->hasChanged = true; $objectReference = $this->resolveClassSelf($classReflection, $subNode); return $this->nodeFactory->createStaticCall($objectReference, $methodName, $subNode->args); }); - - if ($hasChanged) { - return $node; - } - - return null; } /**