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..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,24 +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; + } + + return $this->processIdenticalOrNotIdentical($node); + } + + private function normalizeScalarType(Type $type): Type + { + if ($type->isString()->yes()) { + return new StringType(); } - if ($leftStaticType->isBoolean()->yes() && $rightStaticType->isBoolean()->yes()) { - return $this->processIdenticalOrNotIdentical($node); + if ($type->isBoolean()->yes()) { + return new BooleanType(); } - if ($leftStaticType->isInteger()->yes() && $rightStaticType->isInteger()->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