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_;
|
2019-10-04 12:16:26 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Else_;
|
|
|
|
use PhpParser\Node\Stmt\ElseIf_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2023-02-04 14:18:19 +00:00
|
|
|
use PhpParser\Node\Stmt\Nop;
|
|
|
|
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2023-02-04 14:18:19 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-10-04 12:16:26 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\If_\ShortenElseIfRector\ShortenElseIfRectorTest
|
2019-10-04 12:16:26 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ShortenElseIfRector extends AbstractRector
|
2019-10-04 12:16:26 +00:00
|
|
|
{
|
2023-02-04 14:18:19 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
|
|
|
|
*/
|
|
|
|
private $commentsMerger;
|
|
|
|
public function __construct(CommentsMerger $commentsMerger)
|
|
|
|
{
|
|
|
|
$this->commentsMerger = $commentsMerger;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-10-04 12:16:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Shortens else/if to elseif', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-10-04 12:16:26 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($cond1) {
|
|
|
|
return $action1;
|
|
|
|
} else {
|
|
|
|
if ($cond2) {
|
|
|
|
return $action2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-10-04 12:16:26 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($cond1) {
|
|
|
|
return $action1;
|
|
|
|
} elseif ($cond2) {
|
|
|
|
return $action2;
|
2019-11-05 10:13:53 +00:00
|
|
|
}
|
2019-10-04 12:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-10-04 12:16:26 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-10-04 12:16:26 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-10-04 12:16:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2019-10-04 12:16:26 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2019-10-04 12:16:26 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-10-04 12:16:26 +00:00
|
|
|
{
|
2019-10-04 13:08:10 +00:00
|
|
|
return $this->shortenElseIf($node);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shortenElseIf(If_ $node) : ?If_
|
2019-10-04 13:08:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->else instanceof Else_) {
|
2019-10-04 12:16:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-10-04 17:31:24 +00:00
|
|
|
$else = $node->else;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($else->stmts) !== 1) {
|
2019-10-04 12:16:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$if = $else->stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$if instanceof If_) {
|
2019-10-04 12:16:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-10-04 13:08:10 +00:00
|
|
|
// Try to shorten the nested if before transforming it to elseif
|
|
|
|
$refactored = $this->shortenElseIf($if);
|
2019-10-30 14:38:30 +00:00
|
|
|
if ($refactored !== null) {
|
2019-10-04 12:36:48 +00:00
|
|
|
$if = $refactored;
|
|
|
|
}
|
2023-02-04 14:18:19 +00:00
|
|
|
if ($if->stmts === []) {
|
|
|
|
$nop = new Nop();
|
|
|
|
$nop->setAttribute(AttributeKey::COMMENTS, $if->getComments());
|
|
|
|
$if->stmts[] = $nop;
|
|
|
|
} else {
|
|
|
|
$currentStmt = \current($if->stmts);
|
|
|
|
$this->commentsMerger->keepChildren($currentStmt, $if);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->elseifs[] = new ElseIf_($if->cond, $if->stmts);
|
2019-10-04 12:16:26 +00:00
|
|
|
$node->else = $if->else;
|
2021-05-09 20:15:43 +00:00
|
|
|
$node->elseifs = \array_merge($node->elseifs, $if->elseifs);
|
2019-10-04 12:16:26 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|