Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 SkipLooseCompare extends TestCase
{
public function test()
{
$this->assertEquals(1, 1.0);
}
}
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 SkipLooseCompare2 extends TestCase
{
public function test()
{
$this->assertEquals(1.0, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class SkipMixedArrayDimFetch extends TestCase
{
public function test()
{
$data = $this->getData();
$this->assertEquals(1.0, $data[0]);
}

private function getData(): array
{
return [1];
}
}
21 changes: 18 additions & 3 deletions rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading