This commit is contained in:
Peter Lupu 2022-04-20 01:14:39 +03:00 committed by GitHub
parent b4efb43c04
commit b92465d5c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 271 additions and 17 deletions

View File

@ -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];
});
}
}
?>

View File

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

View File

@ -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];
});
}
}
?>

View File

@ -41,7 +41,7 @@ class SideEffect {
}
private static function rsort( $a, $b ) {
return $a['bar'] <=> $b['bar'];
return $b['bar'] <=> $a['bar'];
}
}

View File

@ -63,7 +63,7 @@ class Fixture
public function runOneMore()
{
usort($languages, function ($a, $b) {
return $a[0] <=> $b[0];
return $b[0] <=> $a[0];
});
}
}

View File

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

View File

@ -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];
});
}
}
?>

View File

@ -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(
@ -108,11 +110,7 @@ CODE_SAMPLE
$this->reset();
$this->matchOnEqualFirstValueAndSecondValue($node);
if ($this->firstValue === null) {
return null;
}
if ($this->secondValue === null) {
if (! isset($this->firstValue, $this->secondValue)) {
return null;
}
@ -124,15 +122,14 @@ CODE_SAMPLE
}
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
@ -171,9 +168,8 @@ CODE_SAMPLE
} 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);
}
}
}
@ -224,6 +220,7 @@ CODE_SAMPLE
/** @var Return_ $returnNode */
$returnNode = $else->stmts[0];
if ($returnNode->expr instanceof Ternary) {
$this->ternary = $returnNode->expr;
$this->processTernary($returnNode->expr, null);
}
}
@ -252,4 +249,22 @@ CODE_SAMPLE
$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);
}
}