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

109 lines
3.4 KiB
PHP
Raw Permalink 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\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\While_;
use Rector\NodeManipulator\AssignManipulator;
use Rector\Php72\ValueObject\ListAndEach;
use Rector\Rector\AbstractRector;
use Rector\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 AbstractRector implements MinPhpVersionInterface
2018-10-03 13:14:08 +00:00
{
2019-08-18 12:15:39 +00:00
/**
* @readonly
* @var \Rector\NodeManipulator\AssignManipulator
2019-08-18 12:15:39 +00:00
*/
private $assignManipulator;
public function __construct(AssignManipulator $assignManipulator)
2019-08-18 12:15:39 +00:00
{
$this->assignManipulator = $assignManipulator;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::DEPRECATE_EACH;
}
public function getRuleDefinition() : RuleDefinition
2018-10-03 13:14:08 +00:00
{
return new RuleDefinition('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
, <<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
foreach ($callbacks as $key => $callback) {
// ...
}
CODE_SAMPLE
), new 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 [While_::class];
2018-10-03 13:14:08 +00:00
}
/**
* @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
}
$listAndEach = $this->assignManipulator->matchListAndEach($node->cond);
if (!$listAndEach instanceof ListAndEach) {
return null;
2018-10-03 13:14:08 +00:00
}
$eachFuncCall = $listAndEach->getEachFuncCall();
$list = $listAndEach->getList();
if (!isset($eachFuncCall->getArgs()[0])) {
return null;
}
$firstArg = $eachFuncCall->getArgs()[0];
$foreachedExpr = \count($list->items) === 1 ? $this->nodeFactory->createFuncCall('array_keys', [$firstArg]) : $firstArg->value;
$arrayItem = \array_pop($list->items);
$isTrailingCommaLast = \false;
if (!$arrayItem instanceof ArrayItem) {
$foreachedExpr = $this->nodeFactory->createFuncCall('array_keys', [$eachFuncCall->args[0]]);
/** @var ArrayItem $arrayItem */
$arrayItem = \current($list->items);
$isTrailingCommaLast = \true;
}
$foreach = new 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 ($list->items !== []) {
2020-05-31 17:18:57 +00:00
/** @var ArrayItem|null $keyItem */
$keyItem = \array_pop($list->items);
if ($keyItem instanceof ArrayItem && !$isTrailingCommaLast) {
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
}
}