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,18 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class SkipUnionType extends TestCase
{
public function test(string|int|bool|\DateTime $expected)
{
$this->assertEquals('value', $this->normalizeStringValue($expected));
}

private function normalizeStringValue(string|int|bool|\DateTime $input): string|int|bool|\DateTime
{
return $input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -126,6 +127,11 @@ private function shouldSkipLooseComparison(array $args): bool
$firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value);
$secondArgType = TypeCombinator::removeNull($this->nodeTypeResolver->getNativeType($args[1]->value));

// union type can hold different types that assertEquals() compares loosely, keep it safe
if ($secondArgType instanceof UnionType) {
return true;
}

// loose comparison
if ($firstArgType instanceof IntegerType && ($secondArgType instanceof FloatType || $secondArgType instanceof StringType)) {
return true;
Expand Down
Loading