diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare_assert_not_equals.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare_assert_not_equals.php.inc new file mode 100644 index 00000000..95cea6d3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare_assert_not_equals.php.inc @@ -0,0 +1,13 @@ +assertNotEquals(1, $float); + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 0ea48257..a645bf6b 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -5,6 +5,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; @@ -103,39 +104,45 @@ public function refactor(Node $node): ?Node return null; } - if ($this->isName($node->name, 'assertEquals')) { - $firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value); - $secondArgType = TypeCombinator::removeNull($this->nodeTypeResolver->getNativeType($args[1]->value)); + if ($this->shouldSkipLooseComparison($args)) { + return null; + } - // loose comparison - if ($firstArgType instanceof IntegerType && ($secondArgType instanceof FloatType || $secondArgType instanceof StringType)) { - return null; - } + $hasChanged = $this->identifierManipulator->renameNodeWithMap($node, self::RENAME_METHODS_MAP); + return $hasChanged ? $node : null; + } - if ($firstArgType instanceof FloatType && ($secondArgType instanceof IntegerType || $secondArgType instanceof StringType)) { - return null; - } + /** + * @param Arg[] $args + */ + private function shouldSkipLooseComparison(array $args): bool + { + $firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value); + $secondArgType = TypeCombinator::removeNull($this->nodeTypeResolver->getNativeType($args[1]->value)); - if ($firstArgType instanceof StringType && $secondArgType instanceof ObjectType && $this->isObjectType( - $args[1]->value, - new ObjectType('Stringable') - )) { - return null; - } + // loose comparison + if ($firstArgType instanceof IntegerType && ($secondArgType instanceof FloatType || $secondArgType instanceof StringType)) { + return true; + } - // compare to mixed type is can be anything - if ($secondArgType instanceof MixedType) { - return null; - } + if ($firstArgType instanceof FloatType && ($secondArgType instanceof IntegerType || $secondArgType instanceof StringType)) { + return true; + } - // can happen with magic process - if ($secondArgType instanceof NeverType) { - return null; - } + if ($firstArgType instanceof StringType && $secondArgType instanceof ObjectType && $this->isObjectType( + $args[1]->value, + new ObjectType('Stringable') + )) { + return true; } - $hasChanged = $this->identifierManipulator->renameNodeWithMap($node, self::RENAME_METHODS_MAP); - return $hasChanged ? $node : null; + // compare to mixed type is can be anything + if ($secondArgType instanceof MixedType) { + return true; + } + + // can happen with magic process + return $secondArgType instanceof NeverType; } private function shouldSkipConstantArrayType(Expr $expr): bool