mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-30 22:53:31 +00:00
92 lines
2.1 KiB
PHP
92 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\Php71\Rector\List_;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Array_;
|
|
use PhpParser\Node\Expr\Assign;
|
|
use PhpParser\Node\Expr\List_;
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
|
* @see https://wiki.php.net/rfc/short_list_syntax
|
|
* @see https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring
|
|
*/
|
|
|
|
/**
|
|
* @see \Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\ListToArrayDestructRectorTest
|
|
*/
|
|
final class ListToArrayDestructRector extends AbstractRector
|
|
{
|
|
public function getRuleDefinition(): RuleDefinition
|
|
{
|
|
return new RuleDefinition('Remove & from new &X', [
|
|
new CodeSample(
|
|
<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
list($id1, $name1) = $data;
|
|
|
|
foreach ($data as list($id, $name)) {
|
|
}
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
,
|
|
<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
[$id1, $name1] = $data;
|
|
|
|
foreach ($data as [$id, $name]) {
|
|
}
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes(): array
|
|
{
|
|
return [List_::class];
|
|
}
|
|
|
|
/**
|
|
* @param List_ $node
|
|
*/
|
|
public function refactor(Node $node): ?Node
|
|
{
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_DESTRUCT)) {
|
|
return null;
|
|
}
|
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof Assign && $parentNode->var === $node) {
|
|
return new Array_($node->items);
|
|
}
|
|
if (! $parentNode instanceof Foreach_) {
|
|
return null;
|
|
}
|
|
if ($parentNode->valueVar !== $node) {
|
|
return null;
|
|
}
|
|
return new Array_($node->items);
|
|
}
|
|
}
|