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\CodeQuality\Rector\If_;
|
2019-01-10 12:56:01 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
2023-06-07 14:47:58 +00:00
|
|
|
use PhpParser\Node\Expr\Throw_ as ExprThrow_;
|
2023-05-10 06:07:59 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2023-06-07 14:47:58 +00:00
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
2023-05-10 06:07:59 +00:00
|
|
|
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\ConsecutiveNullCompareReturnsToNullCoalesceQueueRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ConsecutiveNullCompareReturnsToNullCoalesceQueueRector extends AbstractRector implements MinPhpVersionInterface
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\IfManipulator
|
2019-01-10 12:56:01 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $ifManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(IfManipulator $ifManipulator)
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change multiple null compares to ?? queue', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-01-10 12:56:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
2023-05-10 06:07:59 +00:00
|
|
|
if ($this->orderItem !== null) {
|
2019-01-10 12:56:01 +00:00
|
|
|
return $this->orderItem;
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:07:59 +00:00
|
|
|
if ($this->orderItemUnit !== null) {
|
2019-01-10 12:56:01 +00:00
|
|
|
return $this->orderItemUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-01-10 12:56:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return $this->orderItem ?? $this->orderItemUnit;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-01-10 12:56:01 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2023-05-10 06:07:59 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-10 06:07:59 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2019-01-10 12:56:01 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2023-05-10 06:07:59 +00:00
|
|
|
if ($node->stmts === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$coalescingExprs = [];
|
|
|
|
$ifKeys = [];
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof If_) {
|
|
|
|
continue;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
$comparedExpr = $this->ifManipulator->matchIfNotNullReturnValue($stmt);
|
|
|
|
if (!$comparedExpr instanceof Expr) {
|
|
|
|
continue;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2023-06-08 10:08:29 +00:00
|
|
|
if (!isset($node->stmts[$key + 1])) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
$coalescingExprs[] = $comparedExpr;
|
|
|
|
$ifKeys[] = $key;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
|
|
|
// at least 2 coalescing nodes are needed
|
2023-05-10 06:07:59 +00:00
|
|
|
if (\count($coalescingExprs) < 2) {
|
2019-01-10 12:56:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
// remove last return null
|
2023-06-08 10:08:29 +00:00
|
|
|
$appendExpr = null;
|
2023-06-07 14:47:58 +00:00
|
|
|
$hasChanged = \false;
|
2023-06-08 10:08:29 +00:00
|
|
|
$originalStmts = $node->stmts;
|
2023-05-10 06:07:59 +00:00
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (\in_array($key, $ifKeys, \true)) {
|
|
|
|
unset($node->stmts[$key]);
|
2023-06-07 14:47:58 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$hasChanged) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($stmt instanceof Throw_) {
|
|
|
|
unset($node->stmts[$key]);
|
2023-06-08 10:08:29 +00:00
|
|
|
$appendExpr = new ExprThrow_($stmt->expr);
|
2023-05-10 06:07:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->isReturnNull($stmt)) {
|
2023-06-08 10:08:29 +00:00
|
|
|
if ($stmt instanceof Return_ && $stmt->expr instanceof Expr) {
|
|
|
|
unset($node->stmts[$key]);
|
|
|
|
$appendExpr = $stmt->expr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$node->stmts = $originalStmts;
|
|
|
|
return $node;
|
2023-05-10 06:07:59 +00:00
|
|
|
}
|
|
|
|
unset($node->stmts[$key]);
|
|
|
|
}
|
2023-06-08 10:08:29 +00:00
|
|
|
$node->stmts[] = $this->createCealesceReturn($coalescingExprs, $appendExpr);
|
2023-05-10 06:07:59 +00:00
|
|
|
return $node;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NULL_COALESCE;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
private function isReturnNull(Stmt $stmt) : bool
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2023-05-10 06:07:59 +00:00
|
|
|
if (!$stmt instanceof Return_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
if (!$stmt->expr instanceof Expr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-01-14 18:21:23 +00:00
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
return $this->valueResolver->isNull($stmt->expr);
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-10 06:07:59 +00:00
|
|
|
* @param Expr[] $coalescingExprs
|
2019-01-10 12:56:01 +00:00
|
|
|
*/
|
2023-06-08 10:08:29 +00:00
|
|
|
private function createCealesceReturn(array $coalescingExprs, ?Expr $appendExpr) : Return_
|
2019-01-10 12:56:01 +00:00
|
|
|
{
|
2023-05-10 06:07:59 +00:00
|
|
|
/** @var Expr $leftExpr */
|
|
|
|
$leftExpr = \array_shift($coalescingExprs);
|
|
|
|
/** @var Expr $rightExpr */
|
|
|
|
$rightExpr = \array_shift($coalescingExprs);
|
|
|
|
$coalesce = new Coalesce($leftExpr, $rightExpr);
|
|
|
|
foreach ($coalescingExprs as $coalescingExpr) {
|
|
|
|
$coalesce = new Coalesce($coalesce, $coalescingExpr);
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
2023-06-08 10:08:29 +00:00
|
|
|
if ($appendExpr instanceof Expr) {
|
|
|
|
return new Return_(new Coalesce($coalesce, $appendExpr));
|
2023-06-07 14:47:58 +00:00
|
|
|
}
|
2023-05-10 06:07:59 +00:00
|
|
|
return new Return_($coalesce);
|
2019-01-10 12:56:01 +00:00
|
|
|
}
|
|
|
|
}
|