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\For_;
|
2019-03-26 12:28:38 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2023-06-04 22:38:07 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\CallLike;
|
2023-10-11 04:48:30 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
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\For_;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeManipulator\StmtsManipulator;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\For_\RemoveDeadIfForeachForRector\RemoveDeadIfForeachForRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadIfForeachForRector extends AbstractRector
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
2021-06-14 10:57:44 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-06-14 10:57:44 +00:00
|
|
|
* @var \Rector\EarlyReturn\NodeTransformer\ConditionInverter
|
|
|
|
*/
|
|
|
|
private $conditionInverter;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-10-11 04:48:30 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\StmtsManipulator
|
2023-10-11 04:48:30 +00:00
|
|
|
*/
|
|
|
|
private $stmtsManipulator;
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
|
|
|
public function __construct(ConditionInverter $conditionInverter, BetterNodeFinder $betterNodeFinder, StmtsManipulator $stmtsManipulator)
|
2021-06-14 10:57:44 +00:00
|
|
|
{
|
|
|
|
$this->conditionInverter = $conditionInverter;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2023-10-11 04:48:30 +00:00
|
|
|
$this->stmtsManipulator = $stmtsManipulator;
|
2021-06-14 10:57:44 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove if, foreach and for that does not do anything', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-03-26 12:28:38 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2023-06-04 22:38:07 +00:00
|
|
|
public function run($value)
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
|
|
|
if ($value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-03-26 12:28:38 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2023-06-04 22:38:07 +00:00
|
|
|
public function run($value)
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-03-26 12:28:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-03-26 12:28:38 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
2023-10-11 04:48:30 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2019-03-26 12:28:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-10-11 04:48:30 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2023-06-05 09:32:57 +00:00
|
|
|
* @return \PhpParser\Node|null|int
|
2019-03-26 12:28:38 +00:00
|
|
|
*/
|
2023-06-05 09:32:57 +00:00
|
|
|
public function refactor(Node $node)
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
2023-10-11 04:48:30 +00:00
|
|
|
if ($node->stmts === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->hasChanged = \false;
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof If_ && !$stmt instanceof For_ && !$stmt instanceof Foreach_) {
|
|
|
|
continue;
|
2023-06-04 22:38:07 +00:00
|
|
|
}
|
2023-10-11 04:48:30 +00:00
|
|
|
if ($stmt->stmts !== []) {
|
|
|
|
continue;
|
2023-06-04 22:38:07 +00:00
|
|
|
}
|
2023-10-11 04:48:30 +00:00
|
|
|
if ($stmt instanceof If_) {
|
|
|
|
$this->processIf($stmt, $key, $node);
|
|
|
|
continue;
|
2023-06-05 09:32:57 +00:00
|
|
|
}
|
2023-10-11 04:48:30 +00:00
|
|
|
$this->processForForeach($stmt, $key, $node);
|
2019-03-26 12:28:38 +00:00
|
|
|
}
|
2023-10-11 04:48:30 +00:00
|
|
|
if ($this->hasChanged) {
|
|
|
|
return $node;
|
2023-06-05 09:32:57 +00:00
|
|
|
}
|
2019-10-02 22:32:59 +00:00
|
|
|
return null;
|
2019-03-26 12:28:38 +00:00
|
|
|
}
|
2023-10-11 04:48:30 +00:00
|
|
|
private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware) : void
|
|
|
|
{
|
|
|
|
if ($if->elseifs !== []) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// useless if ()
|
|
|
|
if (!$if->else instanceof Else_) {
|
|
|
|
if ($this->hasNodeSideEffect($if->cond)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unset($stmtsAware->stmts[$key]);
|
|
|
|
$this->hasChanged = \true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$if->cond = $this->conditionInverter->createInvertedCondition($if->cond);
|
|
|
|
$if->stmts = $if->else->stmts;
|
|
|
|
$if->else = null;
|
|
|
|
$this->hasChanged = \true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\For_|\PhpParser\Node\Stmt\Foreach_ $for
|
|
|
|
*/
|
|
|
|
private function processForForeach($for, int $key, StmtsAwareInterface $stmtsAware) : void
|
|
|
|
{
|
|
|
|
if ($for instanceof For_) {
|
|
|
|
$variables = $this->betterNodeFinder->findInstanceOf(\array_merge($for->init, $for->cond, $for->loop), Variable::class);
|
|
|
|
foreach ($variables as $variable) {
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($stmtsAware->stmts[$key]);
|
|
|
|
$this->hasChanged = \true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$exprs = \array_filter([$for->expr, $for->valueVar, $for->valueVar]);
|
|
|
|
$variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class);
|
|
|
|
foreach ($variables as $variable) {
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, (string) $this->getName($variable))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($stmtsAware->stmts[$key]);
|
|
|
|
$this->hasChanged = \true;
|
|
|
|
}
|
2023-06-04 22:38:07 +00:00
|
|
|
private function hasNodeSideEffect(Expr $expr) : bool
|
2019-03-26 12:28:38 +00:00
|
|
|
{
|
2023-06-04 22:38:07 +00:00
|
|
|
return $this->betterNodeFinder->hasInstancesOf($expr, [CallLike::class, Assign::class]);
|
2019-03-26 12:28:38 +00:00
|
|
|
}
|
|
|
|
}
|