2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2018-10-28 16:50:10 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\If_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-02-08 12:33:17 +00:00
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +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
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class SimplifyIfNotNullReturnRector extends \Rector\Core\Rector\AbstractRector
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2019-01-10 12:55:50 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\IfManipulator
|
2019-01-10 12:55:50 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $ifManipulator;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\NodeManipulator\IfManipulator $ifManipulator)
|
2019-01-10 12:55:50 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
2019-01-10 12:55:50 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Changes redundant null check to instant return', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
2018-10-28 16:50:10 +00:00
|
|
|
$newNode = 'something ;
|
|
|
|
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'
|
2018-10-28 16:50:10 +00:00
|
|
|
$newNode = 'something ;
|
|
|
|
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
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Stmt\If_::class];
|
2018-10-28 16:50:10 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param If_ $node
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
2018-10-28 16:50:10 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$comparedNode = $this->ifManipulator->matchIfNotNullReturnValue($node);
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($comparedNode !== null) {
|
2019-01-10 12:55:50 +00:00
|
|
|
$insideIfNode = $node->stmts[0];
|
2021-05-10 22:23:08 +00:00
|
|
|
$nextNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE);
|
|
|
|
if (!$nextNode instanceof \PhpParser\Node\Stmt\Return_) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($nextNode->expr === null) {
|
2019-01-10 12:55:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isNull($nextNode->expr)) {
|
2019-01-10 12:55:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-28 16:50:10 +00:00
|
|
|
$this->removeNode($nextNode);
|
|
|
|
return $insideIfNode;
|
|
|
|
}
|
2019-02-27 21:54:39 +00:00
|
|
|
$comparedNode = $this->ifManipulator->matchIfValueReturnValue($node);
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($comparedNode !== null) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$nextNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE);
|
|
|
|
if (!$nextNode instanceof \PhpParser\Node\Stmt\Return_) {
|
2019-01-10 12:55:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($comparedNode, $nextNode->expr)) {
|
2019-01-10 12:55:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-28 16:50:10 +00:00
|
|
|
$this->removeNode($nextNode);
|
|
|
|
return clone $nextNode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|