Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Source\BaseClass;

final class ParentStaticMethod extends BaseClass
{
public function test(): string
{
return static::parentMethod();
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Source\BaseClass;

final class ParentStaticMethod extends BaseClass
{
public function test(): string
{
return self::parentMethod();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\Source;

class BaseClass
{
protected static function parentMethod(): string
{
return 'parent method';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\Configuration\Parameter\FeatureFlags;
use Rector\Enum\ObjectReference;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://3v4l.org/VbcrN
* @see \Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\StaticToSelfStaticMethodCallOnFinalClassRectorTest
*/
final class StaticToSelfStaticMethodCallOnFinalClassRector extends AbstractRector
Expand Down Expand Up @@ -76,8 +78,14 @@ public function refactor(Node $node): ?Class_
}

$hasChanged = false;
$scope = ScopeFetcher::fetch($node);
$classReflection = $scope->getClassReflection();

$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$hasChanged, $node): ?StaticCall {
if (! $classReflection instanceof ClassReflection) {
return null;
}

$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$hasChanged, $classReflection): ?StaticCall {
if (! $subNode instanceof StaticCall) {
return null;
}
Expand All @@ -92,15 +100,15 @@ public function refactor(Node $node): ?Class_
}

$methodName = (string) $this->getName($subNode->name);
$targetClassMethod = $node->getMethod($methodName);

// skip call non-existing method from current class to ensure transformation is safe
if (! $targetClassMethod instanceof ClassMethod) {
if (! $classReflection->hasNativeMethod($methodName)) {
return null;
}

$methodReflection = $classReflection->getNativeMethod($methodName);

// avoid overlapped change
if (! $targetClassMethod->isStatic()) {
if (! $methodReflection->isStatic()) {
return null;
}

Expand Down
Loading