Skip to content

Commit

Permalink
[CodingStyle] Skip PreferThisOrSelfMethodCallRector to self when clas…
Browse files Browse the repository at this point in the history
…s method is not static (#779)

* PreferThisOrSelfMethodCallRector should only convert static methods

* Closes #778

* add fixture for assertEquals use self

* assert equals already has fixture

Co-authored-by: Brandon Olivares <programmer2188@gmail.com>
  • Loading branch information
samsonasik and devbanana committed Aug 27, 2021
1 parent b2c44bf commit e509007
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipCreateMock extends TestCase
{
public function test()
{
$this->createMock(\Foo::class);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\Enum\PreferenceSelfThis;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -36,6 +38,10 @@ final class PreferThisOrSelfMethodCallRector extends AbstractRector implements C
*/
private array $typeToPreference = [];

public function __construct(private AstResolver $astResolver)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Changes $this->... and static:: to self:: or vise versa for given types', [
Expand Down Expand Up @@ -119,6 +125,11 @@ private function processToSelf(MethodCall | StaticCall $node): ?StaticCall
return null;
}

$classMethod = $this->astResolver->resolveClassMethodFromCall($node);
if ($classMethod instanceof ClassMethod && ! $classMethod->isStatic()) {
return null;
}

$name = $this->getName($node->name);
if ($name === null) {
return null;
Expand Down

0 comments on commit e509007

Please sign in to comment.