2018-10-28 16:50:10 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\If_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2019-02-27 21:54:39 +00:00
|
|
|
use Rector\PhpParser\Node\Manipulator\IfManipulator;
|
2018-10-28 16:50:10 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\If_\SimplifyIfNotNullReturnRector\SimplifyIfNotNullReturnRectorTest
|
|
|
|
*/
|
2018-10-28 16:50:10 +00:00
|
|
|
final class SimplifyIfNotNullReturnRector extends AbstractRector
|
|
|
|
{
|
2019-01-10 12:55:50 +00:00
|
|
|
/**
|
2019-02-27 21:54:39 +00:00
|
|
|
* @var IfManipulator
|
2019-01-10 12:55:50 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $ifManipulator;
|
2019-01-10 12:55:50 +00:00
|
|
|
|
2019-02-27 21:54:39 +00:00
|
|
|
public function __construct(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
|
|
|
}
|
|
|
|
|
2018-10-28 16:50:10 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Changes redundant null check to instant return', [
|
|
|
|
new CodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-28 16:50:10 +00:00
|
|
|
$newNode = 'something ;
|
|
|
|
if ($newNode !== null) {
|
|
|
|
return $newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-10-28 16:50:10 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-28 16:50:10 +00:00
|
|
|
$newNode = 'something ;
|
|
|
|
return $newNode;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-10-28 16:50:10 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [If_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param If_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
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];
|
2018-10-28 16:50:10 +00:00
|
|
|
|
2019-04-13 09:20:27 +00:00
|
|
|
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
2019-01-14 18:21:23 +00:00
|
|
|
if (! $nextNode instanceof Return_ || $nextNode->expr === null) {
|
2019-01-10 12:55:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-28 16:50:10 +00:00
|
|
|
|
2019-01-10 12:55:50 +00:00
|
|
|
if (! $this->isNull($nextNode->expr)) {
|
|
|
|
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) {
|
2019-04-13 09:20:27 +00:00
|
|
|
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
2019-01-10 12:55:50 +00:00
|
|
|
if (! $nextNode instanceof Return_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->areNodesEqual($comparedNode, $nextNode->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:50:10 +00:00
|
|
|
$this->removeNode($nextNode);
|
|
|
|
return clone $nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|