2020-01-15 19:41:42 +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_;
|
2020-01-15 19:41:42 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2023-09-20 12:41:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2023-10-10 08:33:07 +00:00
|
|
|
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;
|
2020-01-15 19:41:42 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\If_\CombineIfRector\CombineIfRectorTest
|
2020-01-15 19:41:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CombineIfRector extends AbstractRector
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
2020-12-04 23:22:12 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
|
2020-12-04 23:22:12 +00:00
|
|
|
*/
|
|
|
|
private $commentsMerger;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(CommentsMerger $commentsMerger, PhpDocInfoFactory $phpDocInfoFactory)
|
2020-12-04 23:22:12 +00:00
|
|
|
{
|
|
|
|
$this->commentsMerger = $commentsMerger;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2020-12-04 23:22:12 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Merges nested if statements', [new CodeSample(<<<'CODE_SAMPLE'
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-01-15 19:41:42 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2020-01-15 19:41:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
|
|
|
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];
|
2021-10-23 21:09:26 +00:00
|
|
|
if ($this->hasVarTag($subIf)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-10 08:33:07 +00:00
|
|
|
$node->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->cond = new BooleanAnd($node->cond, $subIf->cond);
|
2020-01-15 19:41:42 +00:00
|
|
|
$node->stmts = $subIf->stmts;
|
2020-12-04 23:22:12 +00:00
|
|
|
$this->commentsMerger->keepComments($node, [$subIf]);
|
2020-01-15 19:41:42 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(If_ $if) : bool
|
2020-01-15 19:41:42 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($if->else instanceof Else_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($if->stmts) !== 1) {
|
|
|
|
return \true;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($if->elseifs !== []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$if->stmts[0] instanceof If_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($if->stmts[0]->else instanceof Else_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2020-06-29 21:19:37 +00:00
|
|
|
return (bool) $if->stmts[0]->elseifs;
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasVarTag(If_ $if) : bool
|
2021-10-23 21:09:26 +00:00
|
|
|
{
|
|
|
|
$subIfPhpDocInfo = $this->phpDocInfoFactory->createFromNode($if);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$subIfPhpDocInfo instanceof PhpDocInfo) {
|
2021-10-23 21:09:26 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $subIfPhpDocInfo->getVarTagValueNode() instanceof VarTagValueNode;
|
2021-10-23 21:09:26 +00:00
|
|
|
}
|
2020-01-15 19:41:42 +00:00
|
|
|
}
|