rector/rules/Php70/Rector/List_/EmptyListRector.php

56 lines
1.6 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\List_;
2018-10-07 12:12:17 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
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-07 12:12:17 +00:00
/**
2021-04-19 16:15:52 +00:00
* @changelog http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.list
* @see \Rector\Tests\Php70\Rector\List_\EmptyListRector\EmptyListRectorTest
2018-10-07 12:12:17 +00:00
*/
final class EmptyListRector extends AbstractRector implements MinPhpVersionInterface
2018-10-07 12:12:17 +00:00
{
public function getRuleDefinition() : RuleDefinition
2018-10-07 12:12:17 +00:00
{
return new RuleDefinition('list() cannot be empty', [new CodeSample(<<<'CODE_SAMPLE'
'list() = $values;'
2019-09-25 23:52:55 +00:00
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2019-09-25 23:52:55 +00:00
'list($unusedGenerated) = $values;'
CODE_SAMPLE
)]);
2018-10-07 12:12:17 +00:00
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NO_EMPTY_LIST;
}
2018-10-07 12:12:17 +00:00
/**
* @return array<class-string<Node>>
2018-10-07 12:12:17 +00:00
*/
public function getNodeTypes() : array
2018-10-07 12:12:17 +00:00
{
return [List_::class];
2018-10-07 12:12:17 +00:00
}
/**
* @param List_ $node
2018-10-07 12:12:17 +00:00
*/
public function refactor(Node $node) : ?Node
2018-10-07 12:12:17 +00:00
{
foreach ($node->items as $item) {
if ($item instanceof ArrayItem) {
return null;
2018-10-07 12:12:17 +00:00
}
}
$node->items[0] = new ArrayItem(new Variable('unusedGenerated'));
return $node;
2018-10-07 12:12:17 +00:00
}
}