From de9baae58e40ceab04aaf587b4cd0587b863fd91 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 26 Sep 2019 01:52:55 +0200 Subject: [PATCH] [Php71] Add ListToArrayDestructRector --- config/set/php/php71.yaml | 1 + .../src/Rector/List_/EmptyListRector.php | 10 ++- .../List_/ListToArrayDestructRector.php | 87 +++++++++++++++++++ .../Fixture/fixture.php.inc | 33 +++++++ .../ListToArrayDestructRectorTest.php | 30 +++++++ phpstan.neon | 1 + 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 packages/Php71/src/Rector/List_/ListToArrayDestructRector.php create mode 100644 packages/Php71/tests/Rector/List_/ListToArrayDestructRector/Fixture/fixture.php.inc create mode 100644 packages/Php71/tests/Rector/List_/ListToArrayDestructRector/ListToArrayDestructRectorTest.php diff --git a/config/set/php/php71.yaml b/config/set/php/php71.yaml index 6cbc69e5f2f..4163f9e956e 100644 --- a/config/set/php/php71.yaml +++ b/config/set/php/php71.yaml @@ -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: ~ diff --git a/packages/Php70/src/Rector/List_/EmptyListRector.php b/packages/Php70/src/Rector/List_/EmptyListRector.php index 5d42f0b8ea3..769ad22a6c3 100644 --- a/packages/Php70/src/Rector/List_/EmptyListRector.php +++ b/packages/Php70/src/Rector/List_/EmptyListRector.php @@ -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 + )] ); } diff --git a/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php new file mode 100644 index 00000000000..da6d936e89d --- /dev/null +++ b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php @@ -0,0 +1,87 @@ +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; + } +} diff --git a/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/Fixture/fixture.php.inc b/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..a7f4a218221 --- /dev/null +++ b/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/ListToArrayDestructRectorTest.php b/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/ListToArrayDestructRectorTest.php new file mode 100644 index 00000000000..4519307880a --- /dev/null +++ b/packages/Php71/tests/Rector/List_/ListToArrayDestructRector/ListToArrayDestructRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + /** + * @return string[] + */ + public function provideDataForTest(): iterable + { + yield [__DIR__ . '/Fixture/fixture.php.inc']; + } + + protected function getRectorClass(): string + { + return ListToArrayDestructRector::class; + } +} diff --git a/phpstan.neon b/phpstan.neon index 6e466c6861a..0408353bb95 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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, array given#'