Skip to content
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

ArrayItemRemoval should not mutate lists #1358

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Mutator/Removal/ArrayItemRemoval.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Infection\Mutator\GetConfigClassName;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ParentConnector;
use function min;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
Expand Down Expand Up @@ -119,7 +120,22 @@ public function mutate(Node $arrayNode): iterable

public function canMutate(Node $node): bool
{
return $node instanceof Node\Expr\Array_ && count($node->items);
if (!$node instanceof Node\Expr\Array_) {
return false;
}

if (count($node->items) === 0) {
sanmai marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

$parent = ParentConnector::findParent($node);

// Arrays to the left of an assignments are not arrays but lists.
if ($parent instanceof Node\Expr\Assign && $parent->var === $node) {
return false;
}

return true;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/Mutator/Removal/ArrayItemRemovalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,17 @@ public function mutationsProvider(): iterable
],
['remove' => 'all', 'limit' => 1],
];

yield 'It does not mutate lists with missing elements' => [
'<?php [, $a] = [];',
];

yield 'It does not mutate lists with one element' => [
'<?php [$a] = [];',
];

yield 'It does not mutate lists with any number of elements' => [
'<?php [$a, $b] = [];',
];
}
}