From 19ee19f4332a9b250a0db40758fc58aa3741846e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 4 Oct 2025 08:28:07 +0700 Subject: [PATCH] [CodeQuality] Use TypeComparator to verify Boolean vs Constant Boolean on UseIdenticalOverEqualWithSameTypeRector --- ...seIdenticalOverEqualWithSameTypeRector.php | 45 +++---------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php index e3cb73cd75d..092f06ed176 100644 --- a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php +++ b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php @@ -10,9 +10,8 @@ use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; use PhpParser\Node\Expr\BinaryOp\NotIdentical; -use PHPStan\Type\BooleanType; use PHPStan\Type\MixedType; -use PHPStan\Type\Type; +use Rector\NodeTypeResolver\TypeComparator\TypeComparator; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -22,6 +21,11 @@ */ final class UseIdenticalOverEqualWithSameTypeRector extends AbstractRector { + public function __construct( + private readonly TypeComparator $typeComparator + ) { + } + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -74,10 +78,6 @@ public function refactor(Node $node): ?Node $leftStaticType = $this->nodeTypeResolver->getNativeType($node->left); $rightStaticType = $this->nodeTypeResolver->getNativeType($node->right); - if ($this->shouldSkipCompareBoolToNumeric($leftStaticType, $rightStaticType)) { - return null; - } - // objects can be different by content if (! $leftStaticType->isObject()->no() || ! $rightStaticType->isObject()->no()) { return null; @@ -92,44 +92,13 @@ public function refactor(Node $node): ?Node } // different types - if (! $leftStaticType->equals( - $rightStaticType - ) && (! $leftStaticType instanceof BooleanType && ! $rightStaticType instanceof BooleanType)) { + if (! $this->typeComparator->areTypesEqual($leftStaticType, $rightStaticType)) { return null; } return $this->processIdenticalOrNotIdentical($node); } - private function shouldSkipCompareBoolToNumeric(Type $leftStaticType, Type $rightStaticType): bool - { - if ($leftStaticType instanceof BooleanType) { - return $this->shouldSkipNumericType($rightStaticType); - } - - if ($rightStaticType instanceof BooleanType) { - return $this->shouldSkipNumericType($leftStaticType); - } - - return false; - } - - private function shouldSkipNumericType(Type $type): bool - { - // use ! ->no() as to verify both yes and maybe - if (! $type->isNumericString()->no()) { - return true; - } - - if (! $type->isInteger() - ->no()) { - return true; - } - - return ! $type->isFloat() - ->no(); - } - private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical { if ($node instanceof Equal) {