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,37 @@
<?php

namespace Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;

class SameClassNonStatic
{
public function run()
{
return SameClassNonStatic::doWork();
}

public function doWork()
{
return 'work done';
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;

class SameClassNonStatic
{
public function run()
{
return $this->doWork();
}

public function doWork()
{
return 'work done';
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
Expand Down Expand Up @@ -123,6 +124,11 @@ public function refactor(Node $node): ?Node
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $classReflection->getName() === $className) {
return new MethodCall(new Variable('this'), $node->name, $node->args);
}

if ($this->isInstantiable($className, $scope)) {
$new = new New_($node->class);
return new MethodCall($new, $node->name, $node->args);
Expand Down Expand Up @@ -168,8 +174,11 @@ private function shouldSkip(string $methodName, string $className, StaticCall $s
return true;
}

$reflection = $scope->getClassReflection();
if ($reflection instanceof ClassReflection && $reflection->is($className)) {
$currentClassReflection = $scope->getClassReflection();
if ($currentClassReflection instanceof ClassReflection
&& $this->reflectionProvider->hasClass($className)
&& $currentClassReflection->isSubclassOfClass($this->reflectionProvider->getClass($className))
) {
return true;
}

Expand Down