Skip to content

Commit 1b4fb1c

Browse files
authored
[CodeQuality] Allow compare float to constant float type on UseIdenticalOverEqualWithSameTypeRector (#7417)
* [CodeQuality] Allow compare float type on UseIdenticalOverEqualWithSameTypeRector * fix
1 parent 8ec94ef commit 1b4fb1c

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
4+
5+
final class IdenticalFloat
6+
{
7+
public function equal()
8+
{
9+
if ($this->getValue() == 1.0) {
10+
return 'yes';
11+
}
12+
13+
return 'no';
14+
}
15+
16+
/**
17+
* @return float<0, max>
18+
*/
19+
private function getValue(): float
20+
{
21+
return 1.0;
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
30+
31+
final class IdenticalFloat
32+
{
33+
public function equal()
34+
{
35+
if ($this->getValue() === 1.0) {
36+
return 'yes';
37+
}
38+
39+
return 'no';
40+
}
41+
42+
/**
43+
* @return float<0, max>
44+
*/
45+
private function getValue(): float
46+
{
47+
return 1.0;
48+
}
49+
}
50+
51+
?>

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
use PhpParser\Node\Expr\BinaryOp\Identical;
1111
use PhpParser\Node\Expr\BinaryOp\NotEqual;
1212
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
13+
use PHPStan\Type\BooleanType;
14+
use PHPStan\Type\FloatType;
15+
use PHPStan\Type\IntegerType;
1316
use PHPStan\Type\MixedType;
17+
use PHPStan\Type\StringType;
18+
use PHPStan\Type\Type;
1419
use Rector\Rector\AbstractRector;
1520
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1621
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -81,24 +86,35 @@ public function refactor(Node $node): ?Node
8186
return null;
8287
}
8388

84-
if ($leftStaticType->isString()->yes() && $rightStaticType->isString()->yes()) {
85-
return $this->processIdenticalOrNotIdentical($node);
89+
$normalizedLeftType = $this->normalizeScalarType($leftStaticType);
90+
$normalizedRightType = $this->normalizeScalarType($rightStaticType);
91+
92+
if (! $normalizedLeftType->equals($normalizedRightType)) {
93+
return null;
94+
}
95+
96+
return $this->processIdenticalOrNotIdentical($node);
97+
}
98+
99+
private function normalizeScalarType(Type $type): Type
100+
{
101+
if ($type->isString()->yes()) {
102+
return new StringType();
86103
}
87104

88-
if ($leftStaticType->isBoolean()->yes() && $rightStaticType->isBoolean()->yes()) {
89-
return $this->processIdenticalOrNotIdentical($node);
105+
if ($type->isBoolean()->yes()) {
106+
return new BooleanType();
90107
}
91108

92-
if ($leftStaticType->isInteger()->yes() && $rightStaticType->isInteger()->yes()) {
93-
return $this->processIdenticalOrNotIdentical($node);
109+
if ($type->isInteger()->yes()) {
110+
return new IntegerType();
94111
}
95112

96-
// different types
97-
if (! $leftStaticType->equals($rightStaticType)) {
98-
return null;
113+
if ($type->isFloat()->yes()) {
114+
return new FloatType();
99115
}
100116

101-
return $this->processIdenticalOrNotIdentical($node);
117+
return $type;
102118
}
103119

104120
private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical

0 commit comments

Comments
 (0)