2019-10-13 05:59:52 +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\Php72\Rector\Assign;
|
2018-10-03 13:14:08 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2023-08-05 10:29:33 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\NodeManipulator\AssignManipulator;
|
2024-04-24 08:56:31 +00:00
|
|
|
use Rector\Php72\ValueObject\ListAndEach;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
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
|
2020-05-03 14:07:56 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php72\Rector\Assign\ListEachRector\ListEachRectorTest
|
2018-10-03 13:14:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +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
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\AssignManipulator
|
2019-08-18 12:15:39 +00:00
|
|
|
*/
|
|
|
|
private $assignManipulator;
|
2023-05-24 10:04:51 +00:00
|
|
|
public function __construct(AssignManipulator $assignManipulator)
|
2019-08-18 12:15:39 +00:00
|
|
|
{
|
|
|
|
$this->assignManipulator = $assignManipulator;
|
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::DEPRECATE_EACH;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-03 13:14:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +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);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-05-31 10:56:56 +00:00
|
|
|
$key = key($callbacks);
|
|
|
|
$callback = current($callbacks);
|
|
|
|
next($callbacks);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-03 13:14:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-03 13:14:08 +00:00
|
|
|
{
|
2023-05-24 10:04:51 +00:00
|
|
|
return [Expression::class];
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-24 10:04:51 +00:00
|
|
|
* @param Expression $node
|
2023-08-05 10:29:33 +00:00
|
|
|
* @return null|Expression|Stmt[]
|
2018-10-03 13:14:08 +00:00
|
|
|
*/
|
2023-05-24 10:04:51 +00:00
|
|
|
public function refactor(Node $node)
|
2018-10-03 13:14:08 +00:00
|
|
|
{
|
2023-05-24 10:04:51 +00:00
|
|
|
if (!$node->expr instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-04-24 08:56:31 +00:00
|
|
|
$listAndEach = $this->assignManipulator->matchListAndEach($node->expr);
|
|
|
|
if (!$listAndEach instanceof ListAndEach) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
2024-04-24 08:56:31 +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);
|
2024-04-24 08:56:31 +00:00
|
|
|
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);
|
2024-04-24 08:56:31 +00:00
|
|
|
$keyFuncCallAssign = new Assign($list->items[0]->value, $keyFuncCall);
|
2023-07-09 17:30:30 +00:00
|
|
|
return new Expression($keyFuncCallAssign);
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
|
|
|
// only value: list(, $value) = each($values);
|
2024-04-24 08:56:31 +00:00
|
|
|
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);
|
2024-04-24 08:56:31 +00:00
|
|
|
$secondArrayItem = $list->items[1];
|
2023-05-24 10:04:51 +00:00
|
|
|
$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);
|
2024-04-24 08:56:31 +00:00
|
|
|
$secondArrayItem = $list->items[1];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$secondArrayItem instanceof ArrayItem) {
|
|
|
|
throw new ShouldNotHappenException();
|
2020-12-20 21:05:48 +00:00
|
|
|
}
|
2023-05-24 10:04:51 +00:00
|
|
|
$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);
|
2024-04-24 08:56:31 +00:00
|
|
|
$firstArrayItem = $list->items[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstArrayItem instanceof ArrayItem) {
|
|
|
|
throw new ShouldNotHappenException();
|
2020-12-20 21:05:48 +00:00
|
|
|
}
|
2023-05-24 10:04:51 +00:00
|
|
|
$keyAssign = new Assign($firstArrayItem->value, $keyFuncCall);
|
|
|
|
return [new Expression($keyAssign), new Expression($currentAssign), new Expression($nextFuncCall)];
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
2024-04-24 08:56:31 +00:00
|
|
|
private function shouldSkipAssign(ListAndEach $listAndEach) : bool
|
2018-10-03 13:14:08 +00:00
|
|
|
{
|
2024-04-24 08:56:31 +00:00
|
|
|
$list = $listAndEach->getList();
|
|
|
|
if (\count($list->items) !== 2) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-31 15:34:37 +00:00
|
|
|
}
|
|
|
|
// empty list → cannot handle
|
2024-04-24 08:56:31 +00:00
|
|
|
if ($list->items[0] instanceof ArrayItem) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
2024-04-24 08:56:31 +00:00
|
|
|
return !$list->items[1] instanceof ArrayItem;
|
2018-10-03 13:14:08 +00:00
|
|
|
}
|
|
|
|
}
|