2019-12-29 21:11:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-12-12 16:59:09 +00:00
|
|
|
namespace Rector\EarlyReturn\Rector\If_;
|
2019-12-29 21:11:24 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-01-15 20:13:29 +00:00
|
|
|
use PhpParser\Node\Expr\Exit_;
|
|
|
|
use PhpParser\Node\Stmt\Continue_;
|
|
|
|
use PhpParser\Node\Stmt\ElseIf_;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2019-12-29 21:11:24 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2020-01-15 20:13:29 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-12-29 21:11:24 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-12 16:59:09 +00:00
|
|
|
* @see \Rector\EarlyReturn\Tests\Rector\If_\RemoveAlwaysElseRector\RemoveAlwaysElseRectorTest
|
2019-12-29 21:11:24 +00:00
|
|
|
*/
|
2019-12-29 21:55:54 +00:00
|
|
|
final class RemoveAlwaysElseRector extends AbstractRector
|
2019-12-29 21:11:24 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-12-29 21:11:24 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Split if statement, when if condition always break execution flow',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-12-29 21:11:24 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
if ($value) {
|
2019-12-29 21:55:54 +00:00
|
|
|
throw new \InvalidStateException;
|
2019-12-29 21:11:24 +00:00
|
|
|
} else {
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-12-29 21:11:24 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-12-29 21:11:24 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
if ($value) {
|
2019-12-29 21:55:54 +00:00
|
|
|
throw new \InvalidStateException;
|
2019-12-29 21:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-03-03 08:49:57 +00:00
|
|
|
),
|
2020-11-16 17:50:38 +00:00
|
|
|
]);
|
2019-12-29 21:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-12-29 21:11:24 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2019-12-29 21:55:54 +00:00
|
|
|
return [If_::class];
|
2019-12-29 21:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-29 21:55:54 +00:00
|
|
|
* @param If_ $node
|
2019-12-29 21:11:24 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-03-28 23:06:05 +00:00
|
|
|
if ($this->doesLastStatementBreakFlow($node)) {
|
2019-12-29 21:55:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-02-17 00:24:19 +00:00
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
if ($node->elseifs !== []) {
|
2020-06-29 21:19:37 +00:00
|
|
|
$if = new If_($node->cond);
|
|
|
|
$if->stmts = $node->stmts;
|
2020-02-17 00:24:19 +00:00
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
$this->addNodeBeforeNode($if, $node);
|
2020-02-17 00:24:19 +00:00
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
/** @var ElseIf_ $firstElseIf */
|
|
|
|
$firstElseIf = array_shift($node->elseifs);
|
|
|
|
$node->cond = $firstElseIf->cond;
|
|
|
|
$node->stmts = $firstElseIf->stmts;
|
2020-12-20 12:56:43 +00:00
|
|
|
$this->mirrorComments($node, $firstElseIf);
|
2020-11-27 16:33:16 +00:00
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2019-12-29 21:11:24 +00:00
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
if ($node->else !== null) {
|
2020-12-25 00:22:45 +00:00
|
|
|
$this->addNodesAfterNode($node->else->stmts, $node);
|
2020-01-15 20:13:29 +00:00
|
|
|
$node->else = null;
|
|
|
|
return $node;
|
2019-12-29 21:55:54 +00:00
|
|
|
}
|
2019-12-29 21:11:24 +00:00
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-12-29 21:11:24 +00:00
|
|
|
|
2020-12-20 12:50:55 +00:00
|
|
|
private function doesLastStatementBreakFlow(If_ $if): bool
|
2020-01-15 20:13:29 +00:00
|
|
|
{
|
2020-12-20 12:50:55 +00:00
|
|
|
$lastStmt = end($if->stmts);
|
|
|
|
|
2020-01-15 20:13:29 +00:00
|
|
|
return ! ($lastStmt instanceof Return_
|
|
|
|
|| $lastStmt instanceof Throw_
|
|
|
|
|| $lastStmt instanceof Continue_
|
|
|
|
|| ($lastStmt instanceof Expression && $lastStmt->expr instanceof Exit_));
|
2019-12-29 21:11:24 +00:00
|
|
|
}
|
|
|
|
}
|