Skip to content

Commit 7506334

Browse files
authored
add support for boolean vs constant boolean type in UseIdenticalOverEqualWithSameTypeRector (#7408)
* add support for boolean vs constant boolean type * fixup! add support for boolean vs constant boolean type
1 parent 544b4e6 commit 7506334

File tree

3 files changed

+94
-1
lines changed

3 files changed

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

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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;
1314
use PHPStan\Type\MixedType;
1415
use Rector\Rector\AbstractRector;
1516
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -86,7 +87,9 @@ public function refactor(Node $node): ?Node
8687
}
8788

8889
// different types
89-
if (! $leftStaticType->equals($rightStaticType)) {
90+
if (! $leftStaticType->equals(
91+
$rightStaticType
92+
) && (! $leftStaticType instanceof BooleanType && ! $rightStaticType instanceof BooleanType)) {
9093
return null;
9194
}
9295

0 commit comments

Comments
 (0)