Skip to content

Commit

Permalink
infection#597 Array item removal mutator
Browse files Browse the repository at this point in the history
  • Loading branch information
majkel89 committed Mar 3, 2019
1 parent b956858 commit fe53b75
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 0 deletions.
29 changes: 29 additions & 0 deletions resources/schema.json
Expand Up @@ -115,6 +115,35 @@
}
]
}
},
"ArrayItemRemoval": {
"type": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"settings": {
"type": "object",
"additionalProperties": false,
"properties": {
"remove": {
"type": "string",
"enum": ["first", "last", "all"]
},
"limit": {
"type": "integer",
"minimum": 1
}
}
}
}
}
]
}
}
}
},
Expand Down
129 changes: 129 additions & 0 deletions src/Mutator/Removal/ArrayItemRemoval.php
@@ -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)) . '>'
));
}
}
2 changes: 2 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Expand Up @@ -158,6 +158,7 @@ final class MutatorProfile
];

public const REMOVAL = [
Mutator\Removal\ArrayItemRemoval::class,
Mutator\Removal\FunctionCallRemoval::class,
Mutator\Removal\MethodCallRemoval::class,
];
Expand Down Expand Up @@ -327,6 +328,7 @@ final class MutatorProfile
'PregMatchMatches' => Mutator\Regex\PregMatchMatches::class,

//Removal
'ArrayItemRemoval' => Mutator\Removal\ArrayItemRemoval::class,
'FunctionCallRemoval' => Mutator\Removal\FunctionCallRemoval::class,
'MethodCallRemoval' => Mutator\Removal\MethodCallRemoval::class,

Expand Down
156 changes: 156 additions & 0 deletions tests/Mutator/Removal/ArrayItemRemovalTest.php
@@ -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',
];
}
}

0 comments on commit fe53b75

Please sign in to comment.