Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ 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;
}
});

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;
}
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] > $b[0]) ? 1 : -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ 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;
}
});

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;
}
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] > $b[0]) ? -1 : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\Fixture;

final class IntTypedIdentical
{
public function run(int $first, int $second)
{
if ($first === $second) {
return 0;
}

return ($first < $second) ? -1 : 1;
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\Fixture;

final class IntTypedIdentical
{
public function run(int $first, int $second)
{
return $first <=> $second;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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;
}
});

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Reverse2
{
$languages = [];
usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
if ($a[0] == $b[0]) {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class Reverse3
{
$languages = [];
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\Fixture;

final class SkipIdenticalMixedTypes
{
public function run($first, $second)
{
// "1" and 1 are not identical, but <=> sees them equal
if ($first === $second) {
return 0;
}

return ($first < $second) ? -1 : 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\Fixture;

final class SkipIdenticalStrings
{
public function run(string $first, string $second)
{
// "1" and "01" are not identical, but <=> sees them equal
if ($first === $second) {
return 0;
} else {
return ($first < $second) ? -1 : 1;
}
}
}
17 changes: 16 additions & 1 deletion rules/Php70/Rector/If_/IfToSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading