Skip to content

Commit 031f23b

Browse files
authored
[CodeQuality] Use TypeComparator to verify Boolean vs Constant Boolean on UseIdenticalOverEqualWithSameTypeRector (#7414)
1 parent d38bff5 commit 031f23b

File tree

1 file changed

+7
-38
lines changed

1 file changed

+7
-38
lines changed

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
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;
1413
use PHPStan\Type\MixedType;
15-
use PHPStan\Type\Type;
14+
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
1615
use Rector\Rector\AbstractRector;
1716
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1817
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -22,6 +21,11 @@
2221
*/
2322
final class UseIdenticalOverEqualWithSameTypeRector extends AbstractRector
2423
{
24+
public function __construct(
25+
private readonly TypeComparator $typeComparator
26+
) {
27+
}
28+
2529
public function getRuleDefinition(): RuleDefinition
2630
{
2731
return new RuleDefinition(
@@ -74,10 +78,6 @@ public function refactor(Node $node): ?Node
7478
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
7579
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);
7680

77-
if ($this->shouldSkipCompareBoolToNumeric($leftStaticType, $rightStaticType)) {
78-
return null;
79-
}
80-
8181
// objects can be different by content
8282
if (! $leftStaticType->isObject()->no() || ! $rightStaticType->isObject()->no()) {
8383
return null;
@@ -92,44 +92,13 @@ public function refactor(Node $node): ?Node
9292
}
9393

9494
// different types
95-
if (! $leftStaticType->equals(
96-
$rightStaticType
97-
) && (! $leftStaticType instanceof BooleanType && ! $rightStaticType instanceof BooleanType)) {
95+
if (! $this->typeComparator->areTypesEqual($leftStaticType, $rightStaticType)) {
9896
return null;
9997
}
10098

10199
return $this->processIdenticalOrNotIdentical($node);
102100
}
103101

104-
private function shouldSkipCompareBoolToNumeric(Type $leftStaticType, Type $rightStaticType): bool
105-
{
106-
if ($leftStaticType instanceof BooleanType) {
107-
return $this->shouldSkipNumericType($rightStaticType);
108-
}
109-
110-
if ($rightStaticType instanceof BooleanType) {
111-
return $this->shouldSkipNumericType($leftStaticType);
112-
}
113-
114-
return false;
115-
}
116-
117-
private function shouldSkipNumericType(Type $type): bool
118-
{
119-
// use ! ->no() as to verify both yes and maybe
120-
if (! $type->isNumericString()->no()) {
121-
return true;
122-
}
123-
124-
if (! $type->isInteger()
125-
->no()) {
126-
return true;
127-
}
128-
129-
return ! $type->isFloat()
130-
->no();
131-
}
132-
133102
private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical
134103
{
135104
if ($node instanceof Equal) {

0 commit comments

Comments
 (0)