Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #597 Array item removal mutator * #597 fixes * #597 add missing declere strict types * #597 fix limit setting * #597 add type hints * #597 some tweaks * #597 code style fix * #597 phpdoc fix
- Loading branch information
1 parent
b956858
commit cf8c3c5
Showing
4 changed files
with
326 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,130 @@ | ||
<?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 Generator; | ||
use Infection\Config\Exception\InvalidConfigException; | ||
use Infection\Mutator\Util\Mutator; | ||
use Infection\Mutator\Util\MutatorConfig; | ||
use PhpParser\Node; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ArrayItemRemoval extends Mutator | ||
{ | ||
private const DEFAULT_SETTINGS = [ | ||
'remove' => 'first', | ||
'limit' => PHP_INT_MAX, | ||
]; | ||
|
||
/** @var string first|last|all */ | ||
private $remove; | ||
/** @var int */ | ||
private $limit; | ||
|
||
public function __construct(MutatorConfig $config) | ||
{ | ||
parent::__construct($config); | ||
|
||
$settings = $this->getResultSettings(); | ||
|
||
$this->remove = $settings['remove']; | ||
$this->limit = $settings['limit']; | ||
} | ||
|
||
/** | ||
* @param Node|Node\Expr\Array_ $arrayNode | ||
*/ | ||
public function mutate(Node $arrayNode): Generator | ||
{ | ||
foreach ($this->getItemsIndexes($arrayNode->items) as $indexToRemove) { | ||
$newArrayNode = clone $arrayNode; | ||
unset($newArrayNode->items[$indexToRemove]); | ||
|
||
yield $newArrayNode; | ||
} | ||
} | ||
|
||
protected function mutatesNode(Node $node): bool | ||
{ | ||
return $node instanceof Node\Expr\Array_ && \count($node->items); | ||
} | ||
|
||
private function getItemsIndexes(array $items): array | ||
{ | ||
switch ($this->remove) { | ||
case 'first': | ||
return [0]; | ||
case 'last': | ||
return [\count($items) - 1]; | ||
default: | ||
return \range(0, \min(\count($items), $this->limit) - 1); | ||
} | ||
} | ||
|
||
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,165 @@ | ||
<?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\Tests\Mutator\Removal; | ||
|
||
use Generator; | ||
use Infection\Config\Exception\InvalidConfigException; | ||
use Infection\Tests\Mutator\AbstractMutatorTestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ArrayItemRemovalTest extends AbstractMutatorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideMutationCases | ||
*/ | ||
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 = [];', | ||
]; | ||
|
||
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 obeys limit when mutating arrays in `all` mode' => [ | ||
'<?php $a = [1, 2, 3];', | ||
[ | ||
"<?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 = [];", | ||
], | ||
['settings' => ['remove' => 'all', 'limit' => 1]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvalidConfigurationCases | ||
*/ | ||
public function test_settings_validation(string $setting, $value, string $valueInError): void | ||
{ | ||
$this->expectException(InvalidConfigException::class); | ||
$this->expectExceptionMessage(sprintf( | ||
'Invalid configuration of ArrayItemRemoval mutator. Setting `%s` is invalid (%s)', | ||
$setting, | ||
$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', | ||
]; | ||
} | ||
} |