Skip to content

Commit 42742a1

Browse files
[CodeQuality] Skip bool equal integer on UseIdenticalOverEqualWithSameTypeRector (#7411)
* [CodeQuality] Skip bool equal integer on UseIdenticalOverEqualWithSameTypeRector * Fix * Fix * [ci-review] Rector Rectify * re-run rector --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent fb4abd2 commit 42742a1

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
4+
5+
/**
6+
* @see https://3v4l.org/i3oCg
7+
*/
8+
final class SkipBoolEqualInteger
9+
{
10+
public function equal()
11+
{
12+
// maybe compare to numeric string
13+
$value = rand(0, 1) ? 1 : null;
14+
15+
var_dump(true == $value);
16+
var_dump($value == true);
17+
}
18+
}

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node
7474
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
7575
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);
7676

77-
if ($this->shouldSkipCompareNumericString($leftStaticType, $rightStaticType)) {
77+
if ($this->shouldSkipCompareBoolToNumeric($leftStaticType, $rightStaticType)) {
7878
return null;
7979
}
8080

@@ -101,15 +101,25 @@ public function refactor(Node $node): ?Node
101101
return $this->processIdenticalOrNotIdentical($node);
102102
}
103103

104-
private function shouldSkipCompareNumericString(Type $leftStaticType, Type $rightStaticType): bool
104+
private function shouldSkipCompareBoolToNumeric(Type $leftStaticType, Type $rightStaticType): bool
105105
{
106-
// use ! ->no() as to support both yes and maybe
106+
// use ! ->no() as to verify both yes and maybe
107107
if ($leftStaticType instanceof BooleanType) {
108-
return ! $rightStaticType->isNumericString()->no();
108+
if (! $rightStaticType->isNumericString()->no()) {
109+
return true;
110+
}
111+
112+
return ! $rightStaticType->isInteger()
113+
->no();
109114
}
110115

111116
if ($rightStaticType instanceof BooleanType) {
112-
return ! $leftStaticType->isNumericString()->no();
117+
if (! $leftStaticType->isNumericString()->no()) {
118+
return true;
119+
}
120+
121+
return ! $leftStaticType->isInteger()
122+
->no();
113123
}
114124

115125
return false;

0 commit comments

Comments
 (0)