diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/get_class.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/get_class.php.inc new file mode 100644 index 00000000..ce97053b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/get_class.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/skip_first_param.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/skip_first_param.inc new file mode 100644 index 00000000..18562818 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture/skip_first_param.inc @@ -0,0 +1,27 @@ +assertSame(get_class($something), 'stdClass'); + } +} + +?> + ----- +assertSame(get_class($something), 'stdClass'); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector.php b/rules/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector.php index 0c77759e..076f3bee 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector.php @@ -6,10 +6,12 @@ use PhpParser\Node; use PhpParser\Node\Arg; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Identifier; use Rector\Exception\ShouldNotHappenException; use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; @@ -49,6 +51,10 @@ public function getRuleDefinition(): RuleDefinition '$this->assertFalse($foo instanceof Foo, "message");', '$this->assertNotInstanceOf("Foo", $foo, "message");', ), + new CodeSample( + '$this->assertNotEquals(SomeInstance::class, get_class($value));', + '$this->assertNotInstanceOf(SomeInstance::class, $value);' + ), ], ); } @@ -66,12 +72,19 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $oldMethodNames = array_keys(self::RENAME_METHODS_MAP); - if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) { + if ($node->isFirstClassCallable()) { return null; } - if ($node->isFirstClassCallable()) { + if ($this->testsNodeAnalyzer->isPHPUnitMethodCallNames( + $node, + ['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals'] + )) { + return $this->refactorGetClass($node); + } + + $oldMethodNames = array_keys(self::RENAME_METHODS_MAP); + if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) { return null; } @@ -87,6 +100,34 @@ public function refactor(Node $node): ?Node return $node; } + /** + * @param MethodCall|StaticCall $node + */ + private function refactorGetClass(Node $node): ?Node + { + // we need 2 args + if (! isset($node->args[1])) { + return null; + } + + $secondArgument = $node->getArgs()[1]; + $secondArgumentValue = $secondArgument->value; + + if ($secondArgumentValue instanceof FuncCall && $this->isName($secondArgumentValue->name, 'get_class')) { + $node->args[1] = $secondArgumentValue->getArgs()[0]; + + if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) { + $node->name = new Identifier('assertInstanceOf'); + } elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) { + $node->name = new Identifier('assertNotInstanceOf'); + } + + return $node; + } + + return null; + } + private function changeArgumentsOrder(MethodCall|StaticCall $node): void { $oldArguments = $node->getArgs();