forked from infection/infection
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infection#597 Array item removal mutator
- Loading branch information
Showing
4 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* This code is licensed under the BSD 3-Clause License. | ||
* | ||
* Copyright (c) 2017-2019, Maks Rafalko | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Infection\Mutator\Removal; | ||
|
||
use Infection\Config\Exception\InvalidConfigException; | ||
use Infection\Mutator\Util\Mutator; | ||
use Infection\Mutator\Util\MutatorConfig; | ||
use PhpParser\Node; | ||
use Generator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ArrayItemRemoval extends Mutator | ||
{ | ||
private const DEFAULT_SETTINGS = [ | ||
'remove' => 'first', | ||
'limit' => PHP_INT_MAX, | ||
]; | ||
|
||
private $remove; | ||
private $limit; | ||
|
||
public function __construct(MutatorConfig $config) | ||
{ | ||
parent::__construct($config); | ||
|
||
$settings = $this->getResultSettings(); | ||
|
||
$this->remove = $settings['remove']; | ||
$this->limit = $settings['limit']; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
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); | ||
} | ||
|
||
private function getItemsIndexes(array $items): array | ||
{ | ||
switch ($this->remove) { | ||
case 'first': | ||
return [0]; | ||
case 'last': | ||
return [\count($items) - 1]; | ||
default: | ||
return \array_keys($items); | ||
} | ||
} | ||
|
||
private function getResultSettings(): array | ||
{ | ||
$settings = \array_merge(self::DEFAULT_SETTINGS, $this->getSettings()); | ||
|
||
if (!\is_string($settings['remove'])) { | ||
$this->throwConfigException('remove'); | ||
} | ||
|
||
$settings['remove'] = \strtolower($settings['remove']); | ||
|
||
if (!\in_array($settings['remove'], ['first', 'last', 'all'])) { | ||
$this->throwConfigException('remove'); | ||
} | ||
|
||
if (!\is_numeric($settings['limit']) || $settings['limit'] < 1) { | ||
$this->throwConfigException('limit'); | ||
} | ||
|
||
return $settings; | ||
} | ||
|
||
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, | ||
\is_scalar($value) ? $value : '<' . \strtoupper(\gettype($value)) . '>' | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
/** | ||
* This code is licensed under the BSD 3-Clause License. | ||
* | ||
* Copyright (c) 2017-2019, Maks Rafalko | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
namespace Infection\Tests\Mutator\Removal; | ||
|
||
use Infection\Config\Exception\InvalidConfigException; | ||
use Infection\Tests\Mutator\AbstractMutatorTestCase; | ||
use Generator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
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 | ||
{ | ||
$this->doTest($input, $expected, $settings); | ||
} | ||
|
||
public function provideMutationCases(): Generator | ||
{ | ||
yield 'It does not mutate empty arrays' => [ | ||
'<?php $a = [];', | ||
null, | ||
]; | ||
|
||
yield 'It removes only first item by default' => [ | ||
'<?php $a = [1, 2, 3];', | ||
"<?php\n\n\$a = [2, 3];", | ||
]; | ||
|
||
yield 'It removes only last item when set to do so' => [ | ||
'<?php $a = [1, 2, 3];', | ||
"<?php\n\n\$a = [1, 2];", | ||
['settings' => ['remove' => 'last']], | ||
]; | ||
|
||
yield 'It removes every item on by one when set to `all`' => [ | ||
'<?php $a = [1, 2, 3];', | ||
[ | ||
"<?php\n\n\$a = [2, 3];", | ||
"<?php\n\n\$a = [1, 3];", | ||
"<?php\n\n\$a = [1, 2];", | ||
], | ||
['settings' => ['remove' => 'all']], | ||
]; | ||
|
||
yield 'It skips arrays longer than specified limit when removing `all` items' => [ | ||
'<?php $a = [1, 2, 3];', | ||
null, | ||
['settings' => ['remove' => 'all', 'limit' => 2]], | ||
]; | ||
|
||
yield 'It mutates arrays having required items count when removing `all` items' => [ | ||
'<?php $a = [1];', | ||
[ | ||
"<?php\n\n\$a = [];", | ||
], | ||
['settings' => ['remove' => 'all', 'limit' => 1]], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $setting | ||
* @param mixed $value | ||
* @param string $valueInError | ||
* @throws \Exception | ||
* @dataProvider provideInvalidConfigurationCases | ||
*/ | ||
public function test_settings_validation(string $setting, $value, string $valueInError): void | ||
{ | ||
$this->expectException(InvalidConfigException::class); | ||
$this->expectExceptionMessage("Invalid configuration of ArrayItemRemoval mutator. Setting `$setting` is invalid ($valueInError)"); | ||
$this->doTest( | ||
'<?php $a = [1, 2, 3];', | ||
"<?php\n\n\$a = [2, 3];", | ||
['settings' => [$setting => $value]] | ||
); | ||
} | ||
|
||
public function provideInvalidConfigurationCases(): Generator | ||
{ | ||
yield 'remove is null' => [ | ||
'remove', null, '<NULL>', | ||
]; | ||
|
||
yield 'remove is array' => [ | ||
'remove', [], '<ARRAY>', | ||
]; | ||
|
||
yield 'remove is not valid' => [ | ||
'remove', 'INVALID', 'INVALID', | ||
]; | ||
|
||
yield 'remove is not string' => [ | ||
'remove', 1, '1', | ||
]; | ||
|
||
yield 'limit is null' => [ | ||
'limit', null, '<NULL>', | ||
]; | ||
|
||
yield 'limit is array' => [ | ||
'limit', [], '<ARRAY>', | ||
]; | ||
|
||
yield 'limit is zero' => [ | ||
'limit', 0, '0', | ||
]; | ||
|
||
yield 'limit is negative' => [ | ||
'limit', -1, '-1', | ||
]; | ||
|
||
yield 'limit is not numeric' => [ | ||
'limit', 'INVALID', 'INVALID', | ||
]; | ||
} | ||
} |