From 54f2e0f419e28db1f1c3d73ae41a6d06dce21b69 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 12 Jul 2026 13:04:11 +0200 Subject: [PATCH] [CodeQuality] Skip union types in AssertEqualsToSameRector to preserve loose type comparison --- .../Fixture/skip_union_type.php.inc | 18 ++++++++++++++++++ .../MethodCall/AssertEqualsToSameRector.php | 6 ++++++ 2 files changed, 24 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_union_type.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_union_type.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_union_type.php.inc new file mode 100644 index 00000000..588281c4 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_union_type.php.inc @@ -0,0 +1,18 @@ +assertEquals('value', $this->normalizeStringValue($expected)); + } + + private function normalizeStringValue(string|int|bool|\DateTime $input): string|int|bool|\DateTime + { + return $input; + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 34239f45..256b70dd 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -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; @@ -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;