Skip to content

Commit

Permalink
infection#597 fix limit setting
Browse files Browse the repository at this point in the history
  • Loading branch information
majkel89 committed Mar 3, 2019
1 parent 23e2110 commit 22f6563
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 2 additions & 8 deletions src/Mutator/Removal/ArrayItemRemoval.php
Expand Up @@ -78,13 +78,7 @@ public function mutate(Node $arrayNode): Generator

protected function mutatesNode(Node $node): bool
{
if (!$node instanceof Node\Expr\Array_) {
return false;
}

$itemsCount = \count($node->items);

return $itemsCount && ($this->remove !== 'all' || $itemsCount <= $this->limit);
return $node instanceof Node\Expr\Array_ && \count($node->items);
}

private function getItemsIndexes(array $items): array
Expand All @@ -95,7 +89,7 @@ private function getItemsIndexes(array $items): array
case 'last':
return [\count($items) - 1];
default:
return \array_keys($items);
return \range(0, \min(\count($items), $this->limit) - 1);
}
}

Expand Down
16 changes: 14 additions & 2 deletions tests/Mutator/Removal/ArrayItemRemovalTest.php
Expand Up @@ -80,13 +80,25 @@ public function provideMutationCases(): Generator
['settings' => ['remove' => 'all']],
];

yield 'It skips arrays longer than specified limit when removing `all` items' => [
yield 'It obeys limit when mutating arrays in `all` mode' => [
'<?php $a = [1, 2, 3];',
null,
[
"<?php\n\n\$a = [2, 3];",
"<?php\n\n\$a = [1, 3];",
],
['settings' => ['remove' => 'all', 'limit' => 2]],
];

yield 'It mutates arrays having required items count when removing `all` items' => [
'<?php $a = [1, 2];',
[
"<?php\n\n\$a = [2];",
"<?php\n\n\$a = [1];",
],
['settings' => ['remove' => 'all', 'limit' => 2]],
];

yield 'It mutates correctly for limit value (1)' => [
'<?php $a = [1];',
[
"<?php\n\n\$a = [];",
Expand Down

0 comments on commit 22f6563

Please sign in to comment.