2020-01-04 12:51:09 +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\For_;
|
2020-01-04 12:51:09 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-05-22 13:31:04 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2023-05-22 13:31:04 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Smaller;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\For_;
|
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-04 12:51:09 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest
|
2020-01-04 12:51:09 +00:00
|
|
|
*/
|
2023-08-04 17:15:45 +00:00
|
|
|
final class ForRepeatedCountToOwnVariableRector extends AbstractRector
|
2020-01-04 12:51:09 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-08-04 17:15:45 +00:00
|
|
|
* @var string
|
2020-01-04 12:51:09 +00:00
|
|
|
*/
|
2023-08-04 17:15:45 +00:00
|
|
|
private const COUNTER_NAME = 'counter';
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-04 12:51:09 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change count() in for function to own variable', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-01-04 12:51:09 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
|
|
|
for ($i = 5; $i <= count($items); $i++) {
|
|
|
|
echo $items[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-01-04 12:51:09 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
|
|
|
$itemsCount = count($items);
|
|
|
|
for ($i = 5; $i <= $itemsCount; $i++) {
|
|
|
|
echo $items[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-01-04 12:51:09 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-01-04 12:51:09 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-01-04 12:51:09 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [For_::class];
|
2020-01-04 12:51:09 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param For_ $node
|
2022-05-04 15:20:50 +00:00
|
|
|
* @return Stmt[]|null
|
2020-01-04 12:51:09 +00:00
|
|
|
*/
|
2023-08-04 17:15:45 +00:00
|
|
|
public function refactor(Node $node) : ?array
|
2020-01-04 12:51:09 +00:00
|
|
|
{
|
2023-05-22 13:31:04 +00:00
|
|
|
$countInCond = null;
|
2023-08-04 17:15:45 +00:00
|
|
|
$counterVariable = new Variable(self::COUNTER_NAME);
|
2023-05-22 13:31:04 +00:00
|
|
|
foreach ($node->cond as $condExpr) {
|
|
|
|
if (!$condExpr instanceof Smaller && !$condExpr instanceof SmallerOrEqual) {
|
|
|
|
continue;
|
2020-01-04 12:51:09 +00:00
|
|
|
}
|
2023-05-22 13:31:04 +00:00
|
|
|
if (!$condExpr->right instanceof FuncCall) {
|
|
|
|
continue;
|
2020-01-04 12:51:09 +00:00
|
|
|
}
|
2023-05-22 13:31:04 +00:00
|
|
|
$funcCall = $condExpr->right;
|
|
|
|
if (!$this->isName($funcCall, 'count')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$countInCond = $condExpr->right;
|
2023-08-04 17:15:45 +00:00
|
|
|
$condExpr->right = $counterVariable;
|
2020-12-24 16:28:56 +00:00
|
|
|
}
|
2023-08-04 17:15:45 +00:00
|
|
|
if (!$countInCond instanceof Expr) {
|
2020-01-04 12:51:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-08-04 17:15:45 +00:00
|
|
|
$countAssign = new Assign($counterVariable, $countInCond);
|
2022-06-07 08:22:29 +00:00
|
|
|
return [new Expression($countAssign), $node];
|
2020-01-04 12:51:09 +00:00
|
|
|
}
|
|
|
|
}
|