Skip to content

Commit

Permalink
[Php70] Skip inside Encapsed on ThisCallOnStaticMethodToStaticCallRec…
Browse files Browse the repository at this point in the history
…tor (#5481)

* [Php70] Skip inside Encapsed on ThisCallOnStaticMethodToStaticCallRector

* Fixed 🎉

* fix phpstan
  • Loading branch information
samsonasik authored Jan 19, 2024
1 parent 76714b9 commit 7e2495e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\Fixture;

class SkipInsideEncapsed {
public function foo() {
echo "{$this->getLogDate()}";
}

private static function getLogDate(): string {
return 'foo';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 7e2495e

Please sign in to comment.