2021-01-15 19:16:44 +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\DeadCode\Rector\Return_;
|
2021-01-15 19:16:44 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2024-01-02 20:16:55 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2024-01-02 20:16:55 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
2024-01-02 20:16:55 +00:00
|
|
|
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-01-15 19:16:44 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Return_\RemoveDeadConditionAboveReturnRector\RemoveDeadConditionAboveReturnRectorTest
|
2021-01-15 19:16:44 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadConditionAboveReturnRector extends AbstractRector
|
2021-01-15 19:16:44 +00:00
|
|
|
{
|
2024-01-02 20:16:55 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\DeadCode\SideEffect\SideEffectNodeDetector
|
|
|
|
*/
|
|
|
|
private $sideEffectNodeDetector;
|
|
|
|
public function __construct(SideEffectNodeDetector $sideEffectNodeDetector)
|
|
|
|
{
|
|
|
|
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-01-15 19:16:44 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove dead condition above return', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-01-15 19:16:44 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
if (1 === 1) {
|
|
|
|
return 'yes';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'yes';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-15 19:16:44 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
return 'yes';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-01-15 19:16:44 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-15 19:16:44 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-15 19:16:44 +00:00
|
|
|
{
|
2023-03-29 20:32:46 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2021-01-15 19:16:44 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-03-29 20:32:46 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2021-01-15 19:16:44 +00:00
|
|
|
*/
|
2023-03-29 20:32:46 +00:00
|
|
|
public function refactor(Node $node) : ?StmtsAwareInterface
|
2021-01-15 19:16:44 +00:00
|
|
|
{
|
2023-03-29 20:32:46 +00:00
|
|
|
foreach ((array) $node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Return_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$previousNode = $node->stmts[$key - 1] ?? null;
|
2024-01-02 20:16:55 +00:00
|
|
|
if (!$this->isBareIf($previousNode)) {
|
|
|
|
continue;
|
2023-03-29 20:32:46 +00:00
|
|
|
}
|
2024-01-02 20:16:55 +00:00
|
|
|
/** @var Scope $scope */
|
|
|
|
$scope = $stmt->getAttribute(AttributeKey::SCOPE);
|
|
|
|
/** @var If_ $previousNode */
|
|
|
|
if ($this->sideEffectNodeDetector->detect($previousNode->cond, $scope)) {
|
|
|
|
continue;
|
2023-03-29 20:32:46 +00:00
|
|
|
}
|
|
|
|
$countStmt = \count($previousNode->stmts);
|
|
|
|
if ($countStmt === 0) {
|
|
|
|
unset($node->stmts[$key - 1]);
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
if ($countStmt > 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$previousFirstStmt = $previousNode->stmts[0];
|
|
|
|
if (!$previousFirstStmt instanceof Return_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($previousFirstStmt, $stmt)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
unset($node->stmts[$key - 1]);
|
2021-01-15 19:16:44 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2023-03-29 20:32:46 +00:00
|
|
|
return null;
|
2021-01-15 19:16:44 +00:00
|
|
|
}
|
2024-01-02 20:16:55 +00:00
|
|
|
private function isBareIf(?Stmt $stmt) : bool
|
|
|
|
{
|
|
|
|
if (!$stmt instanceof If_) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($stmt->elseifs !== []) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !$stmt->else instanceof Else_;
|
|
|
|
}
|
2021-01-15 19:16:44 +00:00
|
|
|
}
|