diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare.php.inc new file mode 100644 index 00000000..b8f18099 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare.php.inc @@ -0,0 +1,13 @@ +assertEquals(1, 1.0); + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare2.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare2.php.inc new file mode 100644 index 00000000..435be0ad --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_loose_compare2.php.inc @@ -0,0 +1,13 @@ +assertEquals(1.0, 1); + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_mixed_array_dim_fetch.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_mixed_array_dim_fetch.php.inc new file mode 100644 index 00000000..73fb616e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_mixed_array_dim_fetch.php.inc @@ -0,0 +1,19 @@ +getData(); + $this->assertEquals(1.0, $data[0]); + } + + private function getData(): array + { + return [1]; + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 6d5ae2e4..a92eca87 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -15,6 +15,7 @@ use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; +use PHPStan\Type\MixedType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator; @@ -86,7 +87,7 @@ public function refactor(Node $node): ?Node } $args = $node->getArgs(); - if (! isset($args[0])) { + if (! isset($args[0], $args[1])) { return null; } @@ -99,13 +100,27 @@ public function refactor(Node $node): ?Node return null; } + if ($this->isName($node->name, 'assertEquals')) { + $firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value); + $secondArgType = $this->nodeTypeResolver->getNativeType($args[1]->value); + + // loose comparison + if ($firstArgType instanceof IntegerType && ($secondArgType instanceof FloatType || $secondArgType instanceof MixedType)) { + return null; + } + + if ($firstArgType instanceof FloatType && ($secondArgType instanceof IntegerType || $secondArgType instanceof MixedType)) { + return null; + } + } + $hasChanged = $this->identifierManipulator->renameNodeWithMap($node, self::RENAME_METHODS_MAP); return $hasChanged ? $node : null; } private function shouldSkipConstantArrayType(Expr $expr): bool { - $type = $this->getType($expr); + $type = $this->nodeTypeResolver->getNativeType($expr); if (! $type instanceof ConstantArrayType) { return false; @@ -162,7 +177,7 @@ private function isScalarOrEnumValue(Expr $expr): bool return true; } - $valueNodeType = $this->nodeTypeResolver->getType($expr); + $valueNodeType = $this->nodeTypeResolver->getNativeType($expr); return $this->isScalarType($valueNodeType); } }