Skip to content

Commit

Permalink
Add SpreadAssignment Mutator
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruGG authored and maks-rafalko committed Jul 4, 2021
1 parent 282ef00 commit 003f842
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
"This": { "$ref": "#/definitions/default-mutator-config" },
"Spaceship": { "$ref": "#/definitions/default-mutator-config" },
"Spread": { "$ref": "#/definitions/default-mutator-config" },
"SpreadAssignment": { "$ref": "#/definitions/default-mutator-config" },
"SpreadRemoval": { "$ref": "#/definitions/default-mutator-config" },
"Foreach_": { "$ref": "#/definitions/default-mutator-config" },
"For_": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
107 changes: 107 additions & 0 deletions src/Mutator/Operator/SpreadAssignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, 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\Operator;

use function count;
use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;
use Webmozart\Assert\Assert;

/**
* @internal
*
* @implements Mutator<Node\Expr\Array_>
*/
final class SpreadAssignment implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
<<<'TXT'
Removes a spread operator in an array expression and turns it into an assignment. For example:
```php
$x = [...$collection];
```
Will be mutated to:
```php
$x = $collection;
```
TXT
,
MutatorCategory::SEMANTIC_REDUCTION,
null,
<<<'DIFF'
- $x = [...$collection];
+ $x = $collection;
DIFF
);
}

/**
* @psalm-mutation-free
*
* @return iterable<Node\Expr>
*/
public function mutate(Node $node): iterable
{
Assert::allNotNull($node->items);

yield $node->items[0]->value;
}

public function canMutate(Node $node): bool
{
if (!$node instanceof Node\Expr\Array_) {
return false;
}

if (count($node->items) !== 1) {
return false;
}

Assert::allNotNull($node->items);

return $node->items[0]->unpack;
}
}
2 changes: 2 additions & 0 deletions src/Mutator/ProfileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ final class ProfileList
Mutator\Operator\NullSafeMethodCall::class,
Mutator\Operator\NullSafePropertyCall::class,
Mutator\Operator\Spread::class,
Mutator\Operator\SpreadAssignment::class,
Mutator\Operator\SpreadRemoval::class,
Mutator\Operator\Ternary::class,
Mutator\Operator\Throw_::class,
Expand Down Expand Up @@ -363,6 +364,7 @@ final class ProfileList
'NullSafeMethodCall' => Mutator\Operator\NullSafeMethodCall::class,
'NullSafePropertyCall' => Mutator\Operator\NullSafePropertyCall::class,
'Spread' => Mutator\Operator\Spread::class,
'SpreadAssignment' => Mutator\Operator\SpreadAssignment::class,
'SpreadRemoval' => Mutator\Operator\SpreadRemoval::class,
'Ternary' => Mutator\Operator\Ternary::class,
'Throw_' => Mutator\Operator\Throw_::class,
Expand Down
172 changes: 172 additions & 0 deletions tests/phpunit/Mutator/Operator/SpreadAssignmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, 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\Operator;

use Infection\Tests\Mutator\BaseMutatorTestCase;

final class SpreadAssignmentTest extends BaseMutatorTestCase
{
/**
* @requires PHP >= 7.4
* @dataProvider mutationsProvider
*
* @param string|string[] $expected
*/
public function test_it_can_mutate(string $input, $expected = []): void
{
$this->doTest($input, $expected);
}

public function mutationsProvider(): iterable
{
yield 'Spread assignment for a raw array' => [
<<<'PHP'
<?php
$a = [...[1, 2, 3]];
PHP
,
<<<'PHP'
<?php
$a = [1, 2, 3];
PHP
,
];

yield 'Spread assignment for a raw empty array' => [
<<<'PHP'
<?php
$a = [...[]];
PHP
,
<<<'PHP'
<?php
$a = [];
PHP
,
];

yield 'Spread assignment for a variable' => [
<<<'PHP'
<?php
$a = [...$collection];
PHP
,
<<<'PHP'
<?php
$a = $collection;
PHP
,
];

yield 'Spread assignment for a function call' => [
<<<'PHP'
<?php
$a = [...getCollection()];
PHP
,
<<<'PHP'
<?php
$a = getCollection();
PHP
,
];

yield 'Spread assignment for a method call' => [
<<<'PHP'
<?php
$a = [...$object->getCollection()];
PHP
,
<<<'PHP'
<?php
$a = $object->getCollection();
PHP
,
];

yield 'Spread assignment for a new iterator object' => [
<<<'PHP'
<?php
$a = [...new ArrayIterator(['a', 'b', 'c'])];
PHP
,
<<<'PHP'
<?php
$a = new ArrayIterator(['a', 'b', 'c']);
PHP
,
];

yield 'It does not mutate argument unpacking' => [
<<<'PHP'
<?php
function foo(...$array) {}
PHP
,
];

yield 'It does not mutate list construct' => [
<<<'PHP'
<?php
[$left, $right] = ['left', 'right'];
PHP
,
];

yield 'It does not mutate spread expression in array with more than one element' => [
<<<'PHP'
<?php
$a = [...[1, 2, 3], 4];
PHP
,
];
}
}

0 comments on commit 003f842

Please sign in to comment.