Skip to content

Commit

Permalink
Add While mutator (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Nov 2, 2020
1 parent d9f5e86 commit 14f0ee0
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
5 changes: 5 additions & 0 deletions devTools/phpstan-src-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ parameters:
count: 1
path: ../src/Mutator/ZeroIteration/Foreach_.php

-
message: "#^Parameter \\#1 \\$node \\(PhpParser\\\\Node\\\\Stmt\\\\While_\\) of method Infection\\\\Mutator\\\\ZeroIteration\\\\While_\\:\\:mutate\\(\\) should be contravariant with parameter \\$node \\(PhpParser\\\\Node\\) of method Infection\\\\Mutator\\\\Mutator\\:\\:mutate\\(\\)$#"
count: 1
path: ../src/Mutator/ZeroIteration/While_.php

-
message: "#^Call to static method Webmozart\\\\Assert\\\\Mixin\\:\\:allIsInstanceOf\\(\\) with array\\<PhpParser\\\\Node\\> and 'PhpParser\\\\\\\\Node' will always evaluate to true\\.$#"
count: 1
Expand Down
1 change: 1 addition & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
"Spread": { "$ref": "#/definitions/default-mutator-config" },
"Foreach_": { "$ref": "#/definitions/default-mutator-config" },
"For_": { "$ref": "#/definitions/default-mutator-config" },
"While_": { "$ref": "#/definitions/default-mutator-config" },
"CastArray": { "$ref": "#/definitions/default-mutator-config" },
"CastBool": { "$ref": "#/definitions/default-mutator-config" },
"CastFloat": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
2 changes: 2 additions & 0 deletions src/Mutator/ProfileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ final class ProfileList
public const ZERO_ITERATION_PROFILE = [
Mutator\ZeroIteration\For_::class,
Mutator\ZeroIteration\Foreach_::class,
Mutator\ZeroIteration\While_::class,
];

public const CAST_PROFILE = [
Expand Down Expand Up @@ -381,6 +382,7 @@ final class ProfileList
// Zero Iteration
'Foreach_' => Mutator\ZeroIteration\Foreach_::class,
'For_' => Mutator\ZeroIteration\For_::class,
'While_' => Mutator\ZeroIteration\While_::class,

// Cast
'CastArray' => Mutator\Cast\CastArray::class,
Expand Down
102 changes: 102 additions & 0 deletions src/Mutator/ZeroIteration/While_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\ZeroIteration;

use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;

/**
* @internal
*/
final class While_ implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
<<<'TXT'
Replaces the iterable being iterated over with a `while` expression with false, preventing
any iteration within the block to be executed. For example:
```php`
$condition = true;
while ($condition) {
// ...
}
```
Will be mutated to:
```php
$condition = true;
while (false) {
// ...
}
```
TXT
,
MutatorCategory::SEMANTIC_REDUCTION,
null
);
}

/**
* @psalm-mutation-free
*
* @param Node\Stmt\While_ $node
*
* @return iterable<Node\Stmt\While_>
*/
public function mutate(Node $node): iterable
{
yield new Node\Stmt\While_(
new Node\Expr\ConstFetch(new Node\Name('false')),
$node->stmts,
$node->getAttributes()
);
}

public function canMutate(Node $node): bool
{
return $node instanceof Node\Stmt\While_;
}
}
102 changes: 102 additions & 0 deletions tests/phpunit/Mutator/ZeroIteration/While_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\ZeroIteration;

use Infection\Tests\Mutator\BaseMutatorTestCase;

final class While_Test extends BaseMutatorTestCase
{
/**
* @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 'It mutates expression part from variable to false' => [
<<<'PHP'
<?php
$condition = true;
while ($condition) {
}
PHP
,
<<<'PHP'
<?php
$condition = true;
while (false) {
}
PHP
];

yield 'It mutates expression part from boolean true to false' => [
<<<'PHP'
<?php
while (true) {
}
PHP
,
<<<'PHP'
<?php
while (false) {
}
PHP
];

yield 'It does not mutate expression part in do-while loop to false' => [
<<<'PHP'
<?php
do {
} while (true);
PHP
];
}
}

0 comments on commit 14f0ee0

Please sign in to comment.