mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 16:12:22 +00:00
101 lines
2.0 KiB
PHP
101 lines
2.0 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
|
||
|
namespace Rector\CodeQuality\Rector\If_;
|
||
|
|
||
|
use Nette\Utils\Strings;
|
||
|
use PhpParser\Node;
|
||
|
use PhpParser\Node\Expr;
|
||
|
use PhpParser\Node\Expr\Assign;
|
||
|
use PhpParser\Node\Expr\Ternary;
|
||
|
use PhpParser\Node\Stmt;
|
||
|
use PhpParser\Node\Stmt\Else_;
|
||
|
use PhpParser\Node\Stmt\ElseIf_;
|
||
|
use PhpParser\Node\Stmt\Expression;
|
||
|
use PhpParser\Node\Stmt\If_;
|
||
|
use Rector\Rector\AbstractRector;
|
||
|
use Rector\RectorDefinition\CodeSample;
|
||
|
use Rector\RectorDefinition\RectorDefinition;
|
||
|
|
||
|
/**
|
||
|
* @see \Rector\CodeQuality\Tests\Rector\If_\ShortenElseIfRector\ShortenElseIfRectorTest
|
||
|
*/
|
||
|
final class ShortenElseIfRector extends AbstractRector
|
||
|
{
|
||
|
public function getDefinition(): RectorDefinition
|
||
|
{
|
||
|
return new RectorDefinition('Shortens else/if to elseif', [
|
||
|
new CodeSample(
|
||
|
<<<'PHP'
|
||
|
class SomeClass
|
||
|
{
|
||
|
public function run()
|
||
|
{
|
||
|
if ($cond1) {
|
||
|
return $action1;
|
||
|
} else {
|
||
|
if ($cond2) {
|
||
|
return $action2;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
PHP
|
||
|
,
|
||
|
<<<'PHP'
|
||
|
class SomeClass
|
||
|
{
|
||
|
public function run()
|
||
|
{
|
||
|
if ($cond1) {
|
||
|
return $action1;
|
||
|
} elseif ($cond2) {
|
||
|
return $action2;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
PHP
|
||
|
),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return string[]
|
||
|
*/
|
||
|
public function getNodeTypes(): array
|
||
|
{
|
||
|
return [If_::class];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param If_ $node
|
||
|
*/
|
||
|
public function refactor(Node $node): ?Node
|
||
|
{
|
||
|
if (!$else = $node->else) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
if (count($else->stmts) > 1) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
$if = $else->stmts[0];
|
||
|
if (! $if instanceof If_) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
$if = $this->refactor($if) ?? $if;
|
||
|
|
||
|
$node->elseifs[] = new ElseIf_(
|
||
|
$if->cond,
|
||
|
$if->stmts
|
||
|
);
|
||
|
|
||
|
$node->else = $if->else;
|
||
|
|
||
|
$node->elseifs = array_merge($node->elseifs, $if->elseifs);
|
||
|
|
||
|
return $node;
|
||
|
}
|
||
|
}
|