mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
f674f1dd71
72df0f63c5
[DeadCode] Mirror comment on RemoveUnusedNonEmptyArrayBeforeForeachRector (#610)
108 lines
3.7 KiB
PHP
108 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\Rector\If_;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
use PhpParser\Node\Stmt\If_;
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\DeadCode\NodeManipulator\CountManipulator;
|
|
use Rector\DeadCode\UselessIfCondBeforeForeachDetector;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\RemoveUnusedNonEmptyArrayBeforeForeachRectorTest
|
|
*/
|
|
final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
/**
|
|
* @var \Rector\DeadCode\NodeManipulator\CountManipulator
|
|
*/
|
|
private $countManipulator;
|
|
/**
|
|
* @var \Rector\Core\NodeManipulator\IfManipulator
|
|
*/
|
|
private $ifManipulator;
|
|
/**
|
|
* @var \Rector\DeadCode\UselessIfCondBeforeForeachDetector
|
|
*/
|
|
private $uselessIfCondBeforeForeachDetector;
|
|
public function __construct(\Rector\DeadCode\NodeManipulator\CountManipulator $countManipulator, \Rector\Core\NodeManipulator\IfManipulator $ifManipulator, \Rector\DeadCode\UselessIfCondBeforeForeachDetector $uselessIfCondBeforeForeachDetector)
|
|
{
|
|
$this->countManipulator = $countManipulator;
|
|
$this->ifManipulator = $ifManipulator;
|
|
$this->uselessIfCondBeforeForeachDetector = $uselessIfCondBeforeForeachDetector;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unused if check to non-empty array before foreach of the array', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$values = [];
|
|
if ($values !== []) {
|
|
foreach ($values as $value) {
|
|
echo $value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$values = [];
|
|
foreach ($values as $value) {
|
|
echo $value;
|
|
}
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\If_::class];
|
|
}
|
|
/**
|
|
* @param If_ $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->isUselessBeforeForeachCheck($node)) {
|
|
return null;
|
|
}
|
|
$stmt = $node->stmts[0];
|
|
$ifComments = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS) ?? [];
|
|
$stmtComments = $stmt->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS) ?? [];
|
|
$comments = \array_merge($ifComments, $stmtComments);
|
|
$stmt->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS, $comments);
|
|
return $stmt;
|
|
}
|
|
private function isUselessBeforeForeachCheck(\PhpParser\Node\Stmt\If_ $if) : bool
|
|
{
|
|
if (!$this->ifManipulator->isIfWithOnly($if, \PhpParser\Node\Stmt\Foreach_::class)) {
|
|
return \false;
|
|
}
|
|
/** @var Foreach_ $foreach */
|
|
$foreach = $if->stmts[0];
|
|
$foreachExpr = $foreach->expr;
|
|
if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotIdenticalEmptyArray($if, $foreachExpr)) {
|
|
return \true;
|
|
}
|
|
if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotEmpty($if, $foreachExpr)) {
|
|
return \true;
|
|
}
|
|
return $this->countManipulator->isCounterHigherThanOne($if->cond, $foreachExpr);
|
|
}
|
|
}
|