From aa6ef2a5571de41d8fee46a04433b6928d30c62c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kowalik?= Date: Sun, 3 Mar 2019 01:29:35 +0100 Subject: [PATCH] #597 fixes --- src/Mutator/Removal/ArrayItemRemoval.php | 18 +++++++++++------- tests/Mutator/Removal/ArrayItemRemovalTest.php | 10 +--------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Mutator/Removal/ArrayItemRemoval.php b/src/Mutator/Removal/ArrayItemRemoval.php index fd3f88852..cec204728 100644 --- a/src/Mutator/Removal/ArrayItemRemoval.php +++ b/src/Mutator/Removal/ArrayItemRemoval.php @@ -35,11 +35,11 @@ namespace Infection\Mutator\Removal; +use Generator; use Infection\Config\Exception\InvalidConfigException; use Infection\Mutator\Util\Mutator; use Infection\Mutator\Util\MutatorConfig; use PhpParser\Node; -use Generator; /** * @internal @@ -66,12 +66,13 @@ public function __construct(MutatorConfig $config) public function mutate(Node $arrayNode): Generator { - /** @var Node\Expr\Array_ $arrayNode */ - foreach ($this->getItemsIndexes($arrayNode->items) as $indexToRemove) { - /** @var Node\Expr\Array_ $newArrayNode */ - $newArrayNode = clone $arrayNode; - unset($newArrayNode->items[$indexToRemove]); - yield $newArrayNode; + if ($arrayNode instanceof Node\Expr\Array_) { + foreach ($this->getItemsIndexes($arrayNode->items) as $indexToRemove) { + $newArrayNode = clone $arrayNode; + unset($newArrayNode->items[$indexToRemove]); + + yield $newArrayNode; + } } } @@ -80,7 +81,9 @@ 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); } @@ -120,6 +123,7 @@ private function getResultSettings(): array private function throwConfigException(string $property): void { $value = $this->getSettings()[$property]; + throw new InvalidConfigException(sprintf( 'Invalid configuration of ArrayItemRemoval mutator. Setting `%s` is invalid (%s)', $property, diff --git a/tests/Mutator/Removal/ArrayItemRemovalTest.php b/tests/Mutator/Removal/ArrayItemRemovalTest.php index c9f506849..32d4d0d67 100644 --- a/tests/Mutator/Removal/ArrayItemRemovalTest.php +++ b/tests/Mutator/Removal/ArrayItemRemovalTest.php @@ -33,9 +33,9 @@ namespace Infection\Tests\Mutator\Removal; +use Generator; use Infection\Config\Exception\InvalidConfigException; use Infection\Tests\Mutator\AbstractMutatorTestCase; -use Generator; /** * @internal @@ -44,10 +44,6 @@ final class ArrayItemRemovalTest extends AbstractMutatorTestCase { /** * @dataProvider provideMutationCases - * @param string $input - * @param string[]|string $expected - * @param array $settings - * @throws \Exception */ public function test_mutator(string $input, $expected = null, array $settings = []): void { @@ -98,10 +94,6 @@ public function provideMutationCases(): Generator } /** - * @param string $setting - * @param mixed $value - * @param string $valueInError - * @throws \Exception * @dataProvider provideInvalidConfigurationCases */ public function test_settings_validation(string $setting, $value, string $valueInError): void