Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutator to remove shared cases #1306

Merged
merged 4 commits into from Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/schema.json
Expand Up @@ -276,6 +276,7 @@
"FunctionCallRemoval": { "$ref": "#/definitions/default-mutator-config" },
"MethodCallRemoval": { "$ref": "#/definitions/default-mutator-config" },
"CloneRemoval": { "$ref": "#/definitions/default-mutator-config" },
"SharedCaseRemoval": { "$ref": "#/definitions/default-mutator-config" },
"ArrayOneItem": { "$ref": "#/definitions/default-mutator-config" },
"FloatNegation": { "$ref": "#/definitions/default-mutator-config" },
"FunctionCall": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
2 changes: 2 additions & 0 deletions src/Mutator/ProfileList.php
Expand Up @@ -170,6 +170,7 @@ final class ProfileList
Mutator\Removal\CloneRemoval::class,
Mutator\Removal\FunctionCallRemoval::class,
Mutator\Removal\MethodCallRemoval::class,
Mutator\Removal\SharedCaseRemoval::class,
];

public const RETURN_VALUE_PROFILE = [
Expand Down Expand Up @@ -354,6 +355,7 @@ final class ProfileList
'CloneRemoval' => Mutator\Removal\CloneRemoval::class,
'FunctionCallRemoval' => Mutator\Removal\FunctionCallRemoval::class,
'MethodCallRemoval' => Mutator\Removal\MethodCallRemoval::class,
'SharedCaseRemoval' => Mutator\Removal\SharedCaseRemoval::class,

// Return Value
'ArrayOneItem' => Mutator\ReturnValue\ArrayOneItem::class,
Expand Down
114 changes: 114 additions & 0 deletions src/Mutator/Removal/SharedCaseRemoval.php
@@ -0,0 +1,114 @@
<?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\Removal;

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

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

public static function getDefinition(): ?Definition
{
return new Definition(
'Removes `case`s from `switch`.',
MutatorCategory::SEMANTIC_REDUCTION,
null
);
}

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

foreach ($node->cases as $case) {
if ($case->stmts === []) {
return true;
}
}

return false;
}

/**
* @param Node\Stmt\Switch_ $node
*
* @return iterable<Node\Stmt\Switch_>
*/
public function mutate(Node $node): iterable
{
$lastWasEmpty = false;

foreach ($node->cases as $i => $case) {
if ($case->stmts === []) {
$lastWasEmpty = true;
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
$cases = $node->cases;
unset($cases[$i]);

yield new Node\Stmt\Switch_(
$node->cond,
$cases,
$node->getAttributes()
);
} elseif ($lastWasEmpty) {
sanmai marked this conversation as resolved.
Show resolved Hide resolved
$lastWasEmpty = false;
$cases = $node->cases;
unset($cases[$i]);
$lastCase = $cases[$i - 1];
$cases[$i - 1] = new Node\Stmt\Case_(
$lastCase->cond,
$case->stmts,
$lastCase->getAttributes()
);

yield new Node\Stmt\Switch_(
$node->cond,
$cases,
$node->getAttributes()
);
}
}
}
}
111 changes: 111 additions & 0 deletions tests/phpunit/Mutator/Removal/SharedCaseRemovalTest.php
@@ -0,0 +1,111 @@
<?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\Removal;

use Infection\Tests\Mutator\BaseMutatorTestCase;

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

public function mutationsProvider(): iterable
{
yield 'It does not mutate single cases with a body' => [
'<?php switch(true) {case true: $a = [];break; case false: $a = [];break;};',
];

yield 'It removes cases that share a body' => [
'<?php switch(true) {case true: case false: $a = [];break; case null: $a = []; break;};',
sanmai marked this conversation as resolved.
Show resolved Hide resolved
[
'<?php

switch (true) {
case false:
$a = [];
break;
case null:
$a = [];
break;
}',
'<?php

switch (true) {
case true:
$a = [];
break;
case null:
$a = [];
break;
}',
],
];

yield 'It removes default if it shares a body with a case' => [
'<?php switch(true) {case true: $a = [];break; case false: default: $b = []; break;};',
[
'<?php

switch (true) {
case true:
$a = [];
break;
default:
$b = [];
break;
}',
'<?php

switch (true) {
case true:
$a = [];
break;
case false:
$b = [];
break;
}',
],
];
}
}