Skip to content

Commit 3f54bb4

Browse files
authored
[CodeQuality] Skip bool equal float on UseIdenticalOverEqualWithSameTypeRector (#7412)
* [CodeQuality] Skip bool equal float on UseIdenticalOverEqualWithSameTypeRector * Fix * fix
1 parent 42742a1 commit 3f54bb4

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
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/I3GQS
7+
*/
8+
final class SkipBoolEqualFloat
9+
{
10+
public function equal()
11+
{
12+
// maybe compare to numeric string
13+
$value = rand(0, 1) ? 1.0 : null;
14+
15+
var_dump(true == $value);
16+
var_dump($value == true);
17+
}
18+
}

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,33 @@ public function refactor(Node $node): ?Node
103103

104104
private function shouldSkipCompareBoolToNumeric(Type $leftStaticType, Type $rightStaticType): bool
105105
{
106-
// use ! ->no() as to verify both yes and maybe
107106
if ($leftStaticType instanceof BooleanType) {
108-
if (! $rightStaticType->isNumericString()->no()) {
109-
return true;
110-
}
111-
112-
return ! $rightStaticType->isInteger()
113-
->no();
107+
return $this->shouldSkipNumericType($rightStaticType);
114108
}
115109

116110
if ($rightStaticType instanceof BooleanType) {
117-
if (! $leftStaticType->isNumericString()->no()) {
118-
return true;
119-
}
120-
121-
return ! $leftStaticType->isInteger()
122-
->no();
111+
return $this->shouldSkipNumericType($leftStaticType);
123112
}
124113

125114
return false;
126115
}
127116

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+
128133
private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical
129134
{
130135
if ($node instanceof Equal) {

0 commit comments

Comments
 (0)