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_;
|
2018-10-28 16:50:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-10-28 10:16:48 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
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_\SimplifyIfNotNullReturnRector\SimplifyIfNotNullReturnRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyIfNotNullReturnRector extends AbstractRector
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2019-01-10 12:55:50 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\IfManipulator
|
2019-01-10 12:55:50 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $ifManipulator;
|
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(IfManipulator $ifManipulator, ValueResolver $valueResolver)
|
2019-01-10 12:55:50 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2019-01-10 12:55:50 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes redundant null check to instant return', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-07-02 10:36:52 +00:00
|
|
|
$newNode = 'something';
|
2018-10-28 16:50:10 +00:00
|
|
|
if ($newNode !== null) {
|
|
|
|
return $newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-07-02 10:36:52 +00:00
|
|
|
$newNode = 'something';
|
2018-10-28 16:50:10 +00:00
|
|
|
return $newNode;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-28 16:50:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-28 16:50:10 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2023-03-25 05:15:57 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2018-10-28 16:50:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-03-25 05:15:57 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2018-10-28 16:50:10 +00:00
|
|
|
*/
|
2023-03-25 05:15:57 +00:00
|
|
|
public function refactor(Node $node) : ?StmtsAwareInterface
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2023-03-25 05:15:57 +00:00
|
|
|
foreach ((array) $node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof If_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($node->stmts[$key + 1])) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-25 05:15:57 +00:00
|
|
|
$nextNode = $node->stmts[$key + 1];
|
|
|
|
if (!$nextNode instanceof Return_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$expr = $this->ifManipulator->matchIfNotNullReturnValue($stmt);
|
|
|
|
if (!$expr instanceof Expr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$insideIfNode = $stmt->stmts[0];
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$nextNode->expr instanceof Expr) {
|
2023-03-25 05:15:57 +00:00
|
|
|
continue;
|
2019-01-10 12:55:50 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isNull($nextNode->expr)) {
|
2023-03-25 05:15:57 +00:00
|
|
|
continue;
|
2019-01-10 12:55:50 +00:00
|
|
|
}
|
2023-03-25 05:15:57 +00:00
|
|
|
unset($node->stmts[$key]);
|
|
|
|
$node->stmts[$key + 1] = $insideIfNode;
|
|
|
|
return $node;
|
2018-10-28 16:50:10 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|