8000 Allow missing elements when destructuring arrays in foreach loops by MidnightDesign · Pull Request #1880 · infection/infection · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow missing elements when destructuring arrays in foreach loops #1880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Mutator/Removal/ArrayItemRemoval.php
< 8000 td id="diff-232b7a1bee99438bd50403f92a396311bef54df67300f6839be14b90669cbb58L148" data-line-number="148" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
use function range;
use Webmozart\Assert\Assert;

/**
* @internal
Expand Down Expand Up @@ -116,8 +115,6 @@ public static function getDefinition(): ?Definition
*/
public function mutate(Node $node): iterable
{
Assert::allNotNull($node->items);

foreach ($this->getItemsIndexes($node->items) as $indexToRemove) {
$newArrayNode = clone $node;
unset($newArrayNode->items[$indexToRemove]);
Expand Down Expand Up @@ -147,13 +144,18 @@ public function canMutate(Node $node): bool
return false;
}

// Don't mutate destructured values in foreach loops
if ($parent instanceof Node\Stmt\Foreach_ && $parent->valueVar === $node) {
return false;
}

return true;
}

/**
* @psalm-mutation-free
*
* @param ArrayItem[] $items
* @param array<array-key, ArrayItem|null> $items
*
* @return int[]
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/Mutator/Removal/ArrayItemRemovalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ public function mutationsProvider(): iterable
yield 'It does not mutate arrays as an attribute argument' => [
MutatorFixturesProvider::getFixtureFileContent($this, 'does-not-mutate-array-in-attribute.php'),
];

yield 'It does not mutate destructured array values in foreach loops' => [
'<?php foreach ($items as [, $value]) {}',
];
}
}
0