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\DeadCode\Rector\If_;
|
2019-10-05 23:54:34 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Else_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-10-05 23:54:34 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector\SimplifyIfElseWithSameContentRectorTest
|
2019-10-05 23:54:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyIfElseWithSameContentRector extends AbstractRector
|
2019-10-05 23:54:34 +00:00
|
|
|
{
|
2022-04-09 22:42:24 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Printer\BetterStandardPrinter
|
2022-04-09 22:42:24 +00:00
|
|
|
*/
|
2023-06-08 14:23:03 +00:00
|
|
|
private $betterStandardPrinter;
|
|
|
|
public function __construct(BetterStandardPrinter $betterStandardPrinter)
|
2022-04-09 22:42:24 +00:00
|
|
|
{
|
2023-06-08 14:23:03 +00:00
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2022-04-09 22:42:24 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-10-05 23:54:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove if/else if they have same content', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-10-05 23:54:34 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (true) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-10-05 23:54:34 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-10-05 23:54:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-10-05 23:54:34 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-10-05 23:54:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2019-10-05 23:54:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2021-09-15 13:33:39 +00:00
|
|
|
* @return Stmt[]|null
|
2019-10-05 23:54:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?array
|
2019-10-05 23:54:34 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$node->else instanceof Else_) {
|
2019-10-05 23:54:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isIfWithConstantReturns($node)) {
|
2019-10-05 23:54:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-15 13:33:39 +00:00
|
|
|
return $node->stmts;
|
2019-10-05 23:54:34 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isIfWithConstantReturns(If_ $if) : bool
|
2019-10-05 23:54:34 +00:00
|
|
|
{
|
|
|
|
$possibleContents = [];
|
2023-06-08 14:23:03 +00:00
|
|
|
$possibleContents[] = $this->betterStandardPrinter->print($if->stmts);
|
2019-10-05 23:54:34 +00:00
|
|
|
foreach ($if->elseifs as $elseif) {
|
2023-06-08 14:23:03 +00:00
|
|
|
$possibleContents[] = $this->betterStandardPrinter->print($elseif->stmts);
|
2019-10-05 23:54:34 +00:00
|
|
|
}
|
2020-12-21 02:12:42 +00:00
|
|
|
$else = $if->else;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$else instanceof Else_) {
|
|
|
|
throw new ShouldNotHappenException();
|
2020-12-21 02:12:42 +00:00
|
|
|
}
|
2023-06-08 14:23:03 +00:00
|
|
|
$possibleContents[] = $this->betterStandardPrinter->print($else->stmts);
|
2021-05-09 20:15:43 +00:00
|
|
|
$uniqueContents = \array_unique($possibleContents);
|
2019-10-05 23:54:34 +00:00
|
|
|
// only one content for all
|
2021-05-09 20:15:43 +00:00
|
|
|
return \count($uniqueContents) === 1;
|
2019-10-05 23:54:34 +00:00
|
|
|
}
|
|
|
|
}
|