2020-01-15 19:41:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\If_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-01-15 20:48:14 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
2020-01-15 19:41:42 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2020-02-16 21:29:32 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-01-15 19:41:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\CombineIfRectorTest
|
|
|
|
*/
|
|
|
|
final class CombineIfRector extends AbstractRector
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Merges nested if statements', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'PHP'
|
2020-08-01 11:41:16 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2020-01-15 19:41:42 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($cond1) {
|
|
|
|
if ($cond2) {
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
,
|
|
|
|
<<<'PHP'
|
2020-08-01 11:41:16 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2020-01-15 19:41:42 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($cond1 && $cond2) {
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [If_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param If_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-01 16:04:38 +00:00
|
|
|
/** @var If_ $subIf */
|
2020-01-15 19:41:42 +00:00
|
|
|
$subIf = $node->stmts[0];
|
|
|
|
$node->cond = new BooleanAnd($node->cond, $subIf->cond);
|
|
|
|
$node->stmts = $subIf->stmts;
|
2020-01-19 19:16:57 +00:00
|
|
|
|
2020-01-30 21:11:29 +00:00
|
|
|
$this->combineComments($node, $subIf);
|
2020-01-19 19:16:57 +00:00
|
|
|
|
2020-01-15 19:41:42 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
private function shouldSkip(If_ $if): bool
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($if->else !== null) {
|
2020-01-15 19:41:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if (count($if->stmts) !== 1) {
|
2020-01-15 19:41:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($if->elseifs !== []) {
|
2020-01-15 19:41:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if (! $if->stmts[0] instanceof If_) {
|
2020-01-15 19:41:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($if->stmts[0]->else !== null) {
|
2020-01-15 19:41:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-02-16 21:29:32 +00:00
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
return (bool) $if->stmts[0]->elseifs;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2020-01-30 21:11:29 +00:00
|
|
|
|
|
|
|
private function combineComments(Node $firstNode, Node $secondNode): void
|
|
|
|
{
|
2020-02-16 21:29:32 +00:00
|
|
|
// merge comments
|
2020-02-02 18:15:36 +00:00
|
|
|
$comments = array_merge($firstNode->getComments(), $secondNode->getComments());
|
|
|
|
if ($comments === []) {
|
2020-01-30 21:11:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:00:27 +00:00
|
|
|
$firstNode->setAttribute(AttributeKey::COMMENTS, $comments);
|
2020-02-16 21:29:32 +00:00
|
|
|
$firstNode->setAttribute(AttributeKey::PHP_DOC_INFO, null);
|
2020-01-30 21:11:29 +00:00
|
|
|
}
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|