rector/rules/Php72/Rector/Assign/ListEachRector.php

119 lines
4.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\Assign;
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;
use PhpParser\Node\Stmt\Expression;
use Rector\Exception\ShouldNotHappenException;
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\Assign\ListEachRector\ListEachRectorTest
2018-10-03 13:14:08 +00:00
*/
final class ListEachRector 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 key() and current() instead', [new CodeSample(<<<'CODE_SAMPLE'
2018-10-03 13:14:08 +00:00
list($key, $callback) = each($callbacks);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$key = key($callbacks);
$callback = current($callbacks);
next($callbacks);
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 [Expression::class];
2018-10-03 13:14:08 +00:00
}
/**
* @param Expression $node
* @return null|Expression|Stmt[]
2018-10-03 13:14:08 +00:00
*/
public function refactor(Node $node)
2018-10-03 13:14:08 +00:00
{
if (!$node->expr instanceof Assign) {
return null;
}
$listAndEach = $this->assignManipulator->matchListAndEach($node->expr);
if (!$listAndEach instanceof ListAndEach) {
return null;
2018-10-03 13:14:08 +00:00
}
if ($this->shouldSkipAssign($listAndEach)) {
return null;
}
$list = $listAndEach->getList();
$eachFuncCall = $listAndEach->getEachFuncCall();
2018-10-03 13:14:08 +00:00
// only key: list($key, ) = each($values);
if ($list->items[0] instanceof ArrayItem && !$list->items[1] instanceof ArrayItem) {
2021-01-30 21:41:25 +00:00
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
$keyFuncCallAssign = new Assign($list->items[0]->value, $keyFuncCall);
return new Expression($keyFuncCallAssign);
2018-10-03 13:14:08 +00:00
}
// only value: list(, $value) = each($values);
if ($list->items[1] instanceof ArrayItem && !$list->items[0] instanceof ArrayItem) {
2021-01-30 21:41:25 +00:00
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);
$secondArrayItem = $list->items[1];
$currentAssign = new Assign($secondArrayItem->value, $currentFuncCall);
return [new Expression($currentAssign), new Expression($nextFuncCall)];
2018-10-03 13:14:08 +00:00
}
// both: list($key, $value) = each($values);
2021-01-30 21:41:25 +00:00
$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);
$secondArrayItem = $list->items[1];
if (!$secondArrayItem instanceof ArrayItem) {
throw new ShouldNotHappenException();
}
$currentAssign = new Assign($secondArrayItem->value, $currentFuncCall);
2021-01-30 21:41:25 +00:00
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
$firstArrayItem = $list->items[0];
if (!$firstArrayItem instanceof ArrayItem) {
throw new ShouldNotHappenException();
}
$keyAssign = new Assign($firstArrayItem->value, $keyFuncCall);
return [new Expression($keyAssign), new Expression($currentAssign), new Expression($nextFuncCall)];
2018-10-03 13:14:08 +00:00
}
private function shouldSkipAssign(ListAndEach $listAndEach) : bool
2018-10-03 13:14:08 +00:00
{
$list = $listAndEach->getList();
if (\count($list->items) !== 2) {
return \true;
2018-10-31 15:34:37 +00:00
}
// empty list → cannot handle
if ($list->items[0] instanceof ArrayItem) {
return \false;
}
return !$list->items[1] instanceof ArrayItem;
2018-10-03 13:14:08 +00:00
}
}