Skip to content

Commit

Permalink
#597 Array item removal mutator (#649)
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
majkel89 authored and maks-rafalko committed Mar 9, 2019
1 parent b956858 commit cf8c3c5
Show file tree
Hide file tree
Showing 4 changed files with 326 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
130 changes: 130 additions & 0 deletions src/Mutator/Removal/ArrayItemRemoval.php
@@ -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)) . '>'
));
}
}
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
165 changes: 165 additions & 0 deletions tests/Mutator/Removal/ArrayItemRemovalTest.php
@@ -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',
];
}
}

0 comments on commit cf8c3c5

Please sign in to comment.