Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/set/php/php71.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
10 changes: 9 additions & 1 deletion packages/Php70/src/Rector/List_/EmptyListRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ public function getDefinition(): RectorDefinition
{
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
)]
);
}

Expand Down
87 changes: 87 additions & 0 deletions packages/Php71/src/Rector/List_/ListToArrayDestructRector.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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]) {
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'