rector/rules/php72/src/Rector/While_/WhileEachToForeachRector.php

126 lines
3.1 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2018-10-03 13:14:08 +00:00
namespace Rector\Php72\Rector\While_;
2018-10-03 13:14:08 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\While_;
use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2018-10-03 13:14:08 +00:00
/**
* @source https://wiki.php.net/rfc/deprecations_php_7_2#each
*
* @see \Rector\Php72\Tests\Rector\While_\WhileEachToForeachRector\WhileEachToForeachRectorTest
2018-10-03 13:14:08 +00:00
*/
final class WhileEachToForeachRector extends AbstractRector
{
2019-08-18 12:15:39 +00:00
/**
* @var AssignManipulator
*/
private $assignManipulator;
public function __construct(AssignManipulator $assignManipulator)
{
$this->assignManipulator = $assignManipulator;
}
public function getRuleDefinition(): RuleDefinition
2018-10-03 13:14:08 +00:00
{
return new RuleDefinition(
2018-10-03 13:14:08 +00:00
'each() function is deprecated, use foreach() instead.',
[
new CodeSample(
<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
while (list($key, $callback) = each($callbacks)) {
// ...
}
CODE_SAMPLE
2018-10-03 13:14:08 +00:00
,
<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
foreach ($callbacks as $key => $callback) {
// ...
}
CODE_SAMPLE
2018-10-03 13:14:08 +00:00
),
new CodeSample(
<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
while (list($key) = each($callbacks)) {
// ...
}
CODE_SAMPLE
2018-10-03 13:14:08 +00:00
,
<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
foreach (array_keys($callbacks) as $key) {
// ...
}
CODE_SAMPLE
2018-10-03 13:14:08 +00:00
),
]
);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [While_::class];
}
/**
* @param While_ $node
2018-10-03 13:14:08 +00:00
*/
public function refactor(Node $node): ?Node
2018-10-03 13:14:08 +00:00
{
if (! $node->cond instanceof Assign) {
return null;
2018-10-03 13:14:08 +00:00
}
/** @var Assign $assignNode */
$assignNode = $node->cond;
2019-08-18 12:15:39 +00:00
if (! $this->assignManipulator->isListToEachAssign($assignNode)) {
return null;
2018-10-03 13:14:08 +00:00
}
/** @var FuncCall $eachFuncCall */
$eachFuncCall = $assignNode->expr;
/** @var List_ $listNode */
$listNode = $assignNode->var;
2021-01-30 21:41:25 +00:00
$foreachedExpr = count($listNode->items) === 1 ? $this->nodeFactory->createFuncCall(
2019-02-27 13:21:11 +00:00
'array_keys',
[$eachFuncCall->args[0]]
) : $eachFuncCall->args[0]->value;
2018-10-03 13:14:08 +00:00
2020-07-19 18:52:42 +00:00
/** @var ArrayItem $arrayItem */
$arrayItem = array_pop($listNode->items);
$foreach = new Foreach_($foreachedExpr, $arrayItem, [
'stmts' => $node->stmts,
2018-10-03 13:14:08 +00:00
]);
$this->mirrorComments($foreach, $node);
2018-10-03 13:14:08 +00:00
// is key included? add it to foreach
if ($listNode->items !== []) {
2020-05-31 17:18:57 +00:00
/** @var ArrayItem|null $keyItem */
2018-10-03 13:14:08 +00:00
$keyItem = array_pop($listNode->items);
2020-05-31 17:18:57 +00:00
if ($keyItem !== null) {
2020-06-29 21:19:37 +00:00
$foreach->keyVar = $keyItem->value;
2020-05-31 17:18:57 +00:00
}
2018-10-03 13:14:08 +00:00
}
2020-06-29 21:19:37 +00:00
return $foreach;
2018-10-03 13:14:08 +00:00
}
}