From a665458b4eb5fb64999fb1895331441be74d259a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 4 Oct 2025 14:14:32 +0700 Subject: [PATCH 1/2] [CodeQuality] Allow compare float type on UseIdenticalOverEqualWithSameTypeRector --- .../Fixture/identical_float.php.inc | 51 +++++++++++++++++++ ...seIdenticalOverEqualWithSameTypeRector.php | 4 ++ 2 files changed, 55 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture/identical_float.php.inc diff --git a/rules-tests/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture/identical_float.php.inc b/rules-tests/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture/identical_float.php.inc new file mode 100644 index 00000000000..0dd52318d46 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture/identical_float.php.inc @@ -0,0 +1,51 @@ +getValue() == 1.0) { + return 'yes'; + } + + return 'no'; + } + + /** + * @return float<0, max> + */ + private function getValue(): float + { + return 1.0; + } +} + +?> +----- +getValue() === 1.0) { + return 'yes'; + } + + return 'no'; + } + + /** + * @return float<0, max> + */ + private function getValue(): float + { + return 1.0; + } +} + +?> diff --git a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php index cee3f4f3b05..8cdeee7c899 100644 --- a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php +++ b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php @@ -93,6 +93,10 @@ public function refactor(Node $node): ?Node return $this->processIdenticalOrNotIdentical($node); } + if ($leftStaticType->isFloat()->yes() && $rightStaticType->isFloat()->yes()) { + return $this->processIdenticalOrNotIdentical($node); + } + // different types if (! $leftStaticType->equals($rightStaticType)) { return null; From cb6bd3fac24a19ac359c95408a9df2e7e3097fa5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 4 Oct 2025 14:20:04 +0700 Subject: [PATCH 2/2] fix --- ...seIdenticalOverEqualWithSameTypeRector.php | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php index 8cdeee7c899..b5303723a21 100644 --- a/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php +++ b/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php @@ -10,7 +10,12 @@ 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\FloatType; +use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; +use PHPStan\Type\StringType; +use PHPStan\Type\Type; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -81,28 +86,35 @@ public function refactor(Node $node): ?Node return null; } - if ($leftStaticType->isString()->yes() && $rightStaticType->isString()->yes()) { - return $this->processIdenticalOrNotIdentical($node); + $normalizedLeftType = $this->normalizeScalarType($leftStaticType); + $normalizedRightType = $this->normalizeScalarType($rightStaticType); + + if (! $normalizedLeftType->equals($normalizedRightType)) { + return null; } - if ($leftStaticType->isBoolean()->yes() && $rightStaticType->isBoolean()->yes()) { - return $this->processIdenticalOrNotIdentical($node); + return $this->processIdenticalOrNotIdentical($node); + } + + private function normalizeScalarType(Type $type): Type + { + if ($type->isString()->yes()) { + return new StringType(); } - if ($leftStaticType->isInteger()->yes() && $rightStaticType->isInteger()->yes()) { - return $this->processIdenticalOrNotIdentical($node); + if ($type->isBoolean()->yes()) { + return new BooleanType(); } - if ($leftStaticType->isFloat()->yes() && $rightStaticType->isFloat()->yes()) { - return $this->processIdenticalOrNotIdentical($node); + if ($type->isInteger()->yes()) { + return new IntegerType(); } - // different types - if (! $leftStaticType->equals($rightStaticType)) { - return null; + if ($type->isFloat()->yes()) { + return new FloatType(); } - return $this->processIdenticalOrNotIdentical($node); + return $type; } private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical