Skip to content

Commit

Permalink
[CodeQuality] Skip loose comparison on assertNotEquals as well on Ass…
Browse files Browse the repository at this point in the history
…ertEqualsToSameRector (#436)

* [CodeQuality] Skip loose comparison on assertNotEquals as well on AssertEqualsToSameRector

* [CodeQuality] Skip loose comparison on assertNotEquals as well on AssertEqualsToSameRector
  • Loading branch information
samsonasik authored Dec 26, 2024
1 parent 918b33b commit 6b0e4f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipLooseCompareAssertNotEquals extends TestCase
{
public function test(float $float)
{
$this->assertNotEquals(1, $float);
}
}
59 changes: 33 additions & 26 deletions rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6b0e4f0

Please sign in to comment.