rector/rules/Php72/Rector/While_/WhileEachToForeachRector.php

110 lines
3.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php72\Rector\While_;
2018-10-03 13:14:08 +00:00
use PhpParser\Node;
use PhpParser\Node\Arg;
2018-10-03 13:14:08 +00:00
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 Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2018-10-03 13:14:08 +00:00
/**
2021-04-19 16:15:52 +00:00
* @changelog https://wiki.php.net/rfc/deprecations_php_7_2#each
*
* @see \Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\WhileEachToForeachRectorTest
2018-10-03 13:14:08 +00:00
*/
final class WhileEachToForeachRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
2018-10-03 13:14:08 +00:00
{
2019-08-18 12:15:39 +00:00
/**
* @readonly
* @var \Rector\Core\NodeManipulator\AssignManipulator
2019-08-18 12:15:39 +00:00
*/
private $assignManipulator;
public function __construct(\Rector\Core\NodeManipulator\AssignManipulator $assignManipulator)
2019-08-18 12:15:39 +00:00
{
$this->assignManipulator = $assignManipulator;
}
public function provideMinPhpVersion() : int
{
return \Rector\Core\ValueObject\PhpVersionFeature::DEPRECATE_EACH;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-03 13:14:08 +00:00
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('each() function is deprecated, use foreach() instead.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
while (list($key, $callback) = each($callbacks)) {
// ...
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
foreach ($callbacks as $key => $callback) {
// ...
}
CODE_SAMPLE
), new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
while (list($key) = each($callbacks)) {
// ...
}
CODE_SAMPLE
, <<<'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 array<class-string<Node>>
2018-10-03 13:14:08 +00:00
*/
public function getNodeTypes() : array
2018-10-03 13:14:08 +00:00
{
return [\PhpParser\Node\Stmt\While_::class];
2018-10-03 13:14:08 +00:00
}
/**
* @param \PhpParser\Node $node
2018-10-03 13:14:08 +00:00
*/
public function refactor($node) : ?\PhpParser\Node
2018-10-03 13:14:08 +00:00
{
if (!$node->cond instanceof \PhpParser\Node\Expr\Assign) {
return null;
2018-10-03 13:14:08 +00:00
}
/** @var Assign $assignNode */
$assignNode = $node->cond;
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;
if (!isset($eachFuncCall->args[0])) {
return null;
}
if (!$eachFuncCall->args[0] instanceof \PhpParser\Node\Arg) {
return null;
}
$foreachedExpr = \count($listNode->items) === 1 ? $this->nodeFactory->createFuncCall('array_keys', [$eachFuncCall->args[0]]) : $eachFuncCall->args[0]->value;
2020-07-19 18:52:42 +00:00
/** @var ArrayItem $arrayItem */
$arrayItem = \array_pop($listNode->items);
$foreach = new \PhpParser\Node\Stmt\Foreach_($foreachedExpr, $arrayItem, ['stmts' => $node->stmts]);
$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 */
$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
}
}