[Php71] Add ListToArrayDestructRector

This commit is contained in:
Tomas Votruba 2019-09-26 01:52:55 +02:00
parent 4e9aee7891
commit de9baae58e
6 changed files with 161 additions and 1 deletions

View File

@ -10,3 +10,4 @@ services:
Rector\Php71\Rector\FuncCall\CountOnNullRector: ~
Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector: ~
Rector\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector: ~
Rector\Php71\Rector\List_\ListToArrayDestructRector: ~

View File

@ -20,7 +20,15 @@ final class EmptyListRector extends AbstractRector
{
return new RectorDefinition(
'list() cannot be empty',
[new CodeSample('list() = $values;', 'list($generated) = $values;')]
[new CodeSample(
<<<'CODE_SAMPLE'
'list() = $values;'
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
'list($unusedGenerated) = $values;'
CODE_SAMPLE
)]
);
}

View File

@ -0,0 +1,87 @@
<?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\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @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\Php71\Tests\Rector\List_\ListToArrayDestructRector\ListToArrayDestructRectorTest
*/
final class ListToArrayDestructRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove & from new &X', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run()
{
list($id1, $name1) = $data;
foreach ($data as list($id, $name)) {
}
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run()
{
[$id1, $name1] = $data;
foreach ($data as [$id, $name]) {
}
}
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [List_::class];
}
/**
* @param List_ $node
*/
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Assign) {
if ($parentNode->var === $node) {
return new Array_((array) $node->items);
}
}
if ($parentNode instanceof Foreach_) {
if ($parentNode->valueVar === $node) {
return new Array_((array) $node->items);
}
}
return null;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\Php71\Tests\Rector\List_\ListToArrayDestructRector\Fixture;
class SomeClass
{
public function run()
{
list($id1, $name1) = $data;
foreach ($data as list($id, $name)) {
}
}
}
?>
-----
<?php
namespace Rector\Php71\Tests\Rector\List_\ListToArrayDestructRector\Fixture;
class SomeClass
{
public function run()
{
[$id1, $name1] = $data;
foreach ($data as [$id, $name]) {
}
}
}
?>

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\Php71\Tests\Rector\List_\ListToArrayDestructRector;
use Rector\Php71\Rector\List_\ListToArrayDestructRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class ListToArrayDestructRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
/**
* @return string[]
*/
public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
}
protected function getRectorClass(): string
{
return ListToArrayDestructRector::class;
}
}

View File

@ -208,3 +208,4 @@ parameters:
- '#In method "Rector\\FileSystemRector\\Rector\\AbstractFileSystemRector\:\:isDoctrineEntityClass", parameter \$class has no type\-hint and no @param annotation\. More info\: http\://bit\.ly/usetypehint#'
- '#In method "Rector\\Rector\\AbstractRector\:\:isDoctrineEntityClass", parameter \$class has no type\-hint and no @param annotation\. More info\: http\://bit\.ly/usetypehint#'
- '#In method "Rector\\FileSystemRector\\Rector\\AbstractFileSystemRector\:\:moveFile", parameter \$nodes type is "array"\. Please provide a @param annotation to further specify the type of the array\. For instance\: @param int\[\] \$nodes\. More info\: http\://bit\.ly/typehintarray#'
- '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array<PhpParser\\Node\\Expr\\ArrayItem\>, array<PhpParser\\Node\\Expr\\ArrayItem\|null\> given#'