Skip to content

Commit

Permalink
fixes #7118 (#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterlupu committed Apr 19, 2022
1 parent b4efb43 commit b92465d
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

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

class Ascending
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
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]) {
return 0;
} else {
return ($b[0] < $a[0]) ? 1 : -1;
}
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});

usort($languages, function ($a, $b) {
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]) {
return 0;
}
return ($a[0] > $b[0]) ? 1 : -1;
});
}
}

?>
-----
<?php

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

class Ascending
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class Complex
return ($a[0] > $b[0]) ? -1 : 1;
}
});

usort($languages, function($a, $b) {
if (count($a) === count($b)) {
return 0;
}

return count($a) > count($b) ? -1 : 1;
});
}
}

Expand All @@ -39,7 +47,11 @@ class Complex
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return count($b) <=> count($a);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

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

class Descending
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
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]) {
return 0;
} else {
return ($b[0] < $a[0]) ? -1 : 1;
}
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? 1 : -1;
});

usort($languages, function ($a, $b) {
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]) {
return 0;
}
return ($a[0] > $b[0]) ? -1 : 1;
});
}
}

?>
-----
<?php

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

class Descending
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SideEffect {
}

private static function rsort( $a, $b ) {
return $a['bar'] <=> $b['bar'];
return $b['bar'] <=> $a['bar'];
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Fixture
public function runOneMore()
{
usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
return $b[0] <=> $a[0];
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class Reverse
return ($a[0] > $b[0]) ? 1 : -1;
}
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
} else {
return ($a[0] < $b[0]) ? -1 : 1;
}
});
}
}

Expand All @@ -29,7 +37,11 @@ class Reverse
{
$languages = [];
usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
return $a[0] <=> $b[0];
});

usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

class Reverse3
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
}

return ($a[0] > $b[0]) ? -1 : 1;
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
} else {
return ($a[0] > $b[0]) ? -1 : 1;
}
});
}
}

?>
-----
<?php

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

class Reverse3
{
public function run()
{
$languages = [];
usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});

usort($languages, function ($a, $b) {
return $b[0] <=> $a[0];
});
}
}

?>
41 changes: 28 additions & 13 deletions rules/Php70/Rector/If_/IfToSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ final class IfToSpaceshipRector extends AbstractRector implements MinPhpVersionI

private Node|null $nextNode = null;

private Ternary|null $ternary = null;

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -108,11 +110,7 @@ public function refactor(Node $node): ?Node
$this->reset();

$this->matchOnEqualFirstValueAndSecondValue($node);
if ($this->firstValue === null) {
return null;
}

if ($this->secondValue === null) {
if (! isset($this->firstValue, $this->secondValue)) {
return null;
}

Expand All @@ -124,15 +122,14 @@ public function refactor(Node $node): ?Node
}

if ([$this->onGreater, $this->onEqual, $this->onSmaller] === [1, 0, -1]) {
return $this->processReturnSpaceship($this->secondValue, $this->firstValue);
return $this->processAscendingSort($this->ternary, $this->firstValue, $this->secondValue);
}

// is spaceship return values?
if ([$this->onGreater, $this->onEqual, $this->onSmaller] !== [-1, 0, 1]) {
return null;
if ([$this->onGreater, $this->onEqual, $this->onSmaller] === [-1, 0, 1]) {
return $this->processDescendingSort($this->ternary, $this->firstValue, $this->secondValue);
}

return $this->processReturnSpaceship($this->firstValue, $this->secondValue);
return null;
}

public function provideMinPhpVersion(): int
Expand Down Expand Up @@ -171,9 +168,8 @@ private function matchOnEqualFirstValueAndSecondValue(If_ $if): void
} else {
$nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode instanceof Return_ && $nextNode->expr instanceof Ternary) {
/** @var Ternary $ternary */
$ternary = $nextNode->expr;
$this->processTernary($ternary, $nextNode);
$this->ternary = $nextNode->expr;
$this->processTernary($this->ternary, $nextNode);
}
}
}
Expand Down Expand Up @@ -224,6 +220,7 @@ private function processElse(Else_ $else): void
/** @var Return_ $returnNode */
$returnNode = $else->stmts[0];
if ($returnNode->expr instanceof Ternary) {
$this->ternary = $returnNode->expr;
$this->processTernary($returnNode->expr, null);
}
}
Expand Down Expand Up @@ -252,4 +249,22 @@ private function processTernary(Ternary $ternary, ?Return_ $return): void
$this->nextNode = $return;
}
}

private function processAscendingSort(?Ternary $ternary, Expr $firstValue, Expr $secondValue): Return_
{
if ($ternary === null || $ternary->cond instanceof Greater) {
return $this->processReturnSpaceship($firstValue, $secondValue);
}

return $this->processReturnSpaceship($secondValue, $firstValue);
}

private function processDescendingSort(?Ternary $ternary, Expr $firstValue, Expr $secondValue): Return_
{
if ($ternary === null || $ternary->cond instanceof Smaller) {
return $this->processReturnSpaceship($firstValue, $secondValue);
}

return $this->processReturnSpaceship($secondValue, $firstValue);
}
}

0 comments on commit b92465d

Please sign in to comment.