diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending.php.inc index c55ce0af9a4..46ae43b5cfe 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending.php.inc @@ -8,7 +8,7 @@ class Ascending { $languages = []; usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] < $b[0]) ? -1 : 1; @@ -16,14 +16,14 @@ class Ascending }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } return ($a[0] < $b[0]) ? -1 : 1; }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] > $b[0]) ? 1 : -1; @@ -31,7 +31,7 @@ class Ascending }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } return ($a[0] > $b[0]) ? 1 : -1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending_flip.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending_flip.php.inc index 127e0bfa3b2..db3c8afaad8 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending_flip.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/ascending_flip.php.inc @@ -7,7 +7,7 @@ final class AscendingFlip public function run($languages) { usort($languages, function ($ascendingFirst, $ascendingSecond) { - if ($ascendingFirst[0] === $ascendingSecond[0]) { + if ($ascendingFirst[0] == $ascendingSecond[0]) { return 0; } else { return ($ascendingSecond[0] < $ascendingFirst[0]) ? 1 : -1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/complex.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/complex.php.inc index 15c548f8ccc..fa55cb27dbe 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/complex.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/complex.php.inc @@ -15,7 +15,7 @@ class Complex }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] > $b[0]) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending.php.inc index 4978b06631f..89c8c2d3b3d 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending.php.inc @@ -8,7 +8,7 @@ class Descending { $languages = []; usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] < $b[0]) ? 1 : -1; @@ -16,14 +16,14 @@ class Descending }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } return ($a[0] < $b[0]) ? 1 : -1; }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] > $b[0]) ? -1 : 1; @@ -31,7 +31,7 @@ class Descending }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } return ($a[0] > $b[0]) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending_flip.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending_flip.php.inc index fa807fd38d8..6079d28e45f 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending_flip.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/descending_flip.php.inc @@ -7,7 +7,7 @@ final class DescendingFlip public function run(array $languages) { usort($languages, function ($firstLanguage, $secondLanguage) { - if ($firstLanguage[0] === $secondLanguage[0]) { + if ($firstLanguage[0] == $secondLanguage[0]) { return 0; } else { return ($secondLanguage[0] < $firstLanguage[0]) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/do_not_take_side_effect.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/do_not_take_side_effect.php.inc index 9bd5ff94782..f1054873108 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/do_not_take_side_effect.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/do_not_take_side_effect.php.inc @@ -14,7 +14,7 @@ class SideEffect { } private static function rsort( $a, $b ) { - if ( $a['bar'] === $b['bar'] ) { + if ( $a['bar'] == $b['bar'] ) { return 0; } else { return ( $a['bar'] > $b['bar'] ) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/int_typed_identical.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/int_typed_identical.php.inc new file mode 100644 index 00000000000..e58496bcf8f --- /dev/null +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/int_typed_identical.php.inc @@ -0,0 +1,31 @@ + +----- + $second; + } +} + +?> diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse.php.inc index 32d2bd0a22f..e79c4fde902 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse.php.inc @@ -8,7 +8,7 @@ class Reverse { $languages = []; usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] > $b[0]) ? 1 : -1; @@ -16,7 +16,7 @@ class Reverse }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] < $b[0]) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse2.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse2.php.inc index ded73fd6d70..921a1b0f152 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse2.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse2.php.inc @@ -8,7 +8,7 @@ class Reverse2 { $languages = []; usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse3.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse3.php.inc index 936a6e12157..88d519602b7 100644 --- a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse3.php.inc +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/reverse3.php.inc @@ -8,7 +8,7 @@ class Reverse3 { $languages = []; usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } @@ -16,7 +16,7 @@ class Reverse3 }); usort($languages, function ($a, $b) { - if ($a[0] === $b[0]) { + if ($a[0] == $b[0]) { return 0; } else { return ($a[0] > $b[0]) ? -1 : 1; diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_mixed_types.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_mixed_types.php.inc new file mode 100644 index 00000000000..14c67bef9ac --- /dev/null +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_mixed_types.php.inc @@ -0,0 +1,16 @@ + sees them equal + if ($first === $second) { + return 0; + } + + return ($first < $second) ? -1 : 1; + } +} diff --git a/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_strings.php.inc b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_strings.php.inc new file mode 100644 index 00000000000..7de15147555 --- /dev/null +++ b/rules-tests/Php70/Rector/If_/IfToSpaceshipRector/Fixture/skip_identical_strings.php.inc @@ -0,0 +1,16 @@ + sees them equal + if ($first === $second) { + return 0; + } else { + return ($first < $second) ? -1 : 1; + } + } +} diff --git a/rules/Php70/Rector/If_/IfToSpaceshipRector.php b/rules/Php70/Rector/If_/IfToSpaceshipRector.php index 765edb2b40e..404b1dc37cc 100644 --- a/rules/Php70/Rector/If_/IfToSpaceshipRector.php +++ b/rules/Php70/Rector/If_/IfToSpaceshipRector.php @@ -43,7 +43,7 @@ public function getRuleDefinition(): RuleDefinition new CodeSample( <<<'CODE_SAMPLE' usort($languages, function ($first, $second) { -if ($first[0] === $second[0]) { +if ($first[0] == $second[0]) { return 0; } @@ -168,6 +168,12 @@ private function matchExprComparedExprsReturnZero(If_ $if): ?ComparedExprs } $binaryOp = $if->cond; + + // <=> compares loosely, so a strict guard is only safe when loose and strict equality coincide + if ($binaryOp instanceof Identical && ! $this->areBothIntegers($binaryOp)) { + return null; + } + if (count($if->stmts) !== 1) { return null; } @@ -206,6 +212,15 @@ private function createReturnSpaceship(?string $battleshipCompareOrder, Compared return new Return_($spaceship); } + private function areBothIntegers(Identical $identical): bool + { + if (! $this->getType($identical->left)->isInteger()->yes()) { + return false; + } + + return $this->getType($identical->right)->isInteger()->yes(); + } + private function matchElseOnlyStmtTernary(Else_ $else): Ternary|null { if (count($else->stmts) !== 1) {