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\Php71\Rector\List_;
|
2019-09-25 23:52:55 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\List_;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
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;
|
2019-09-25 23:52:55 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/short_list_syntax https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\ListToArrayDestructRectorTest
|
2019-09-25 23:52:55 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ListToArrayDestructRector extends AbstractRector implements MinPhpVersionInterface
|
2019-09-25 23:52:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-09-25 23:52:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change list() to array destruct', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-09-25 23:52:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
list($id1, $name1) = $data;
|
|
|
|
|
|
|
|
foreach ($data as list($id, $name)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-09-25 23:52:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
[$id1, $name1] = $data;
|
|
|
|
|
|
|
|
foreach ($data as [$id, $name]) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-25 23:52:55 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-09-25 23:52:55 +00:00
|
|
|
{
|
2023-05-27 08:27:32 +00:00
|
|
|
return [Assign::class, Foreach_::class];
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-27 08:27:32 +00:00
|
|
|
* @param Assign|Foreach_ $node
|
2019-09-25 23:52:55 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-09-25 23:52:55 +00:00
|
|
|
{
|
2023-05-27 08:27:32 +00:00
|
|
|
if ($node instanceof Assign) {
|
|
|
|
if (!$node->var instanceof List_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$list = $node->var;
|
|
|
|
$node->var = new Array_($list->items);
|
|
|
|
return $node;
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|
2023-05-27 08:27:32 +00:00
|
|
|
if (!$node->valueVar instanceof List_) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|
2023-05-27 08:27:32 +00:00
|
|
|
$list = $node->valueVar;
|
|
|
|
$node->valueVar = new Array_($list->items);
|
|
|
|
return $node;
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ARRAY_DESTRUCT;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2019-09-25 23:52:55 +00:00
|
|
|
}
|