2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php70\Rector\Ternary;
|
2018-12-15 12:20:07 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Greater;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Smaller;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Spaceship;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-15 12:20:07 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/combined-comparison-operator
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php70\Rector\Ternary\TernaryToSpaceshipRector\TernaryToSpaceshipRectorTest
|
2018-12-15 12:20:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class TernaryToSpaceshipRector extends AbstractRector implements MinPhpVersionInterface
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Use <=> spaceship instead of ternary with same effect', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-15 12:20:07 +00:00
|
|
|
function order_func($a, $b) {
|
|
|
|
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-15 12:20:07 +00:00
|
|
|
function order_func($a, $b) {
|
|
|
|
return $a <=> $b;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-15 12:20:07 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2018-12-15 12:20:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Ternary $nestedTernary */
|
|
|
|
$nestedTernary = $node->else;
|
|
|
|
$spaceshipNode = $this->processSmallerThanTernary($node, $nestedTernary);
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($spaceshipNode instanceof Spaceship) {
|
2018-12-15 12:20:07 +00:00
|
|
|
return $spaceshipNode;
|
|
|
|
}
|
|
|
|
return $this->processGreaterThanTernary($node, $nestedTernary);
|
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::SPACESHIP;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(Ternary $ternary) : bool
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$ternary->cond instanceof BinaryOp) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$ternary->else instanceof Ternary) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
2019-02-22 17:25:31 +00:00
|
|
|
$nestedTernary = $ternary->else;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nestedTernary->cond instanceof BinaryOp) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-02-18 05:44:27 +00:00
|
|
|
}
|
2018-12-15 12:20:07 +00:00
|
|
|
// $a X $b ? . : ($a X $b ? . : .)
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($ternary->cond->left, $nestedTernary->cond->left)) {
|
|
|
|
return \true;
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
// $a X $b ? . : ($a X $b ? . : .)
|
2021-05-09 20:15:43 +00:00
|
|
|
return !$this->nodeComparator->areNodesEqual($ternary->cond->right, $nestedTernary->cond->right);
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Matches "$a < $b ? -1 : ($a > $b ? 1 : 0)"
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processSmallerThanTernary(Ternary $node, Ternary $nestedTernary) : ?Spaceship
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->cond instanceof Smaller) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nestedTernary->cond instanceof Greater) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-29 13:37:16 +00:00
|
|
|
if (!$this->valueResolver->areValuesEqual([$node->if, $nestedTernary->if, $nestedTernary->else], [-1, 1, 0])) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Spaceship($node->cond->left, $node->cond->right);
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Matches "$a > $b ? -1 : ($a < $b ? 1 : 0)"
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processGreaterThanTernary(Ternary $node, Ternary $nestedTernary) : ?Spaceship
|
2018-12-15 12:20:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->cond instanceof Greater) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nestedTernary->cond instanceof Smaller) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-29 13:37:16 +00:00
|
|
|
if (!$this->valueResolver->areValuesEqual([$node->if, $nestedTernary->if, $nestedTernary->else], [-1, 1, 0])) {
|
2021-02-19 00:13:43 +00:00
|
|
|
return null;
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Spaceship($node->cond->right, $node->cond->left);
|
2018-12-15 12:20:07 +00:00
|
|
|
}
|
|
|
|
}
|