-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
282ef00
commit 003f842
Showing
4 changed files
with
282 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,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; | ||
} | ||
} |
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
172 changes: 172 additions & 0 deletions
172
tests/phpunit/Mutator/Operator/SpreadAssignmentTest.php
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,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 | ||
, | ||
]; | ||
} | ||
} |