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
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhpParser\\Node\\ArrayItem>, array<PhpParser\\Node\\ArrayItem\|null> given#'
path: rules/Php71/Rector/List_/ListToArrayDestructRector.php

# scope vs mutating scope
-
identifier: argument.type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;

final class PartialDestruct
{
public function modify_shipping_rate_cost($cost, $rate) {
list(, $instance_id) = explode(':', $rate->id);
}
}

?>
-----
<?php

namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;

final class PartialDestruct
{
public function modify_shipping_rate_cost($cost, $rate) {
[, $instance_id] = explode(':', $rate->id);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;

final class PartialDestructInForeach
{
public function run(array $rows)
{
foreach ($rows as list(, $second)) {
echo $second;
}

list($first, , $third) = $rows;
}
}

?>
-----
<?php

namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;

final class PartialDestructInForeach
{
public function run(array $rows)
{
foreach ($rows as [, $second]) {
echo $second;
}

[$first, , $third] = $rows;
}
}

?>

This file was deleted.

26 changes: 6 additions & 20 deletions rules/Php71/Rector/List_/ListToArrayDestructRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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,
]);
}
}
Loading