Skip to content

Commit

Permalink
[Php70] Handle reverse sorting check on IfToSpaceshipRector (#1676)
Browse files Browse the repository at this point in the history
* Create a test for reverse sorting

* Closes #1674

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* remove applied skip

* moved skip to apply

Co-authored-by: John Blackbourn <johnbillion@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Jan 14, 2022
1 parent a17759c commit a9d2982
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

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

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

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

?>
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class Skip
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;
});

usort($languages, function ($a, $b) {
if ($a[0] === $b[0]) {
return 0;
Expand Down
21 changes: 15 additions & 6 deletions rules/Php70/Rector/If_/IfToSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,35 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SPACESHIP;
}

private function processReturnSpaceship(Expr $firstValue, Expr $secondValue): Return_
{
if ($this->nextNode !== null) {
$this->removeNode($this->nextNode);
}

// spaceship ready!
$spaceship = new Spaceship($this->secondValue, $this->firstValue);
$spaceship = new Spaceship($secondValue, $firstValue);

return new Return_($spaceship);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SPACESHIP;
}

private function reset(): void
{
$this->onEqual = null;
Expand Down

0 comments on commit a9d2982

Please sign in to comment.