diff --git a/phpstan.neon b/phpstan.neon index 885d5d09ce2..25ce690b3d5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -130,12 +130,6 @@ parameters: identifier: argument.type message: '#Parameter \#1 \$name of method PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass\:\:getAttributes\(\) expects class\-string\|null, string given#' - # false positive, checked above - - - identifier: argument.type - message: '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array, array given#' - path: rules/Php71/Rector/List_/ListToArrayDestructRector.php - # scope vs mutating scope - identifier: argument.type diff --git a/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct.php.inc b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct.php.inc new file mode 100644 index 00000000000..3293e32fd24 --- /dev/null +++ b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct.php.inc @@ -0,0 +1,25 @@ +id); + } +} + +?> +----- +id); + } +} + +?> diff --git a/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct_in_foreach.php.inc b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct_in_foreach.php.inc new file mode 100644 index 00000000000..1ff4b031845 --- /dev/null +++ b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/partial_destruct_in_foreach.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc deleted file mode 100644 index 2a0790987ab..00000000000 --- a/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc +++ /dev/null @@ -1,10 +0,0 @@ -id); - } -} diff --git a/rules/Php71/Rector/List_/ListToArrayDestructRector.php b/rules/Php71/Rector/List_/ListToArrayDestructRector.php index fccd6947217..75f3225472b 100644 --- a/rules/Php71/Rector/List_/ListToArrayDestructRector.php +++ b/rules/Php71/Rector/List_/ListToArrayDestructRector.php @@ -5,8 +5,6 @@ namespace Rector\Php71\Rector\List_; use PhpParser\Node; -use PhpParser\Node\ArrayItem; -use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\List_; use PhpParser\Node\Stmt\Foreach_; @@ -77,14 +75,7 @@ public function refactor(Node $node): ?Node return null; } - $list = $node->var; - - // all list items must be set - if ($this->hasPartialDestruct($list)) { - return null; - } - - $node->var = new Array_($list->items); + $node->var = $this->createShortList($node->var); return $node; } @@ -96,14 +87,7 @@ public function refactor(Node $node): ?Node return null; } - $list = $node->valueVar; - - // all list items must be set - if ($this->hasPartialDestruct($list)) { - return null; - } - - $node->valueVar = new Array_($list->items); + $node->valueVar = $this->createShortList($node->valueVar); return $node; } @@ -113,8 +97,10 @@ public function provideMinPhpVersion(): int return PhpVersion::PHP_71; } - private function hasPartialDestruct(List_ $list): bool + private function createShortList(List_ $list): List_ { - return array_any($list->items, fn (?ArrayItem $arrayItem): bool => ! $arrayItem instanceof ArrayItem); + return new List_($list->items, [ + AttributeKey::KIND => List_::KIND_ARRAY, + ]); } }