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 always false if and elseif mutators #1866

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,12 @@
"Break_": { "$ref": "#/definitions/default-mutator-config" },
"Catch_": { "$ref": "#/definitions/default-mutator-config" },
"Continue_": { "$ref": "#/definitions/default-mutator-config" },
"ElseIfAlwaysFalse": { "$ref": "#/definitions/default-mutator-config" },
"ElseIfNegation": { "$ref": "#/definitions/default-mutator-config" },
"Ternary": { "$ref": "#/definitions/default-mutator-config" },
"Throw_": { "$ref": "#/definitions/default-mutator-config" },
"Finally_": { "$ref": "#/definitions/default-mutator-config" },
"IfAlwaysFalse": { "$ref": "#/definitions/default-mutator-config" },
"IfNegation": { "$ref": "#/definitions/default-mutator-config" },
"Coalesce": { "$ref": "#/definitions/default-mutator-config" },
"Concat": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
106 changes: 106 additions & 0 deletions src/Mutator/Operator/ElseIfAlwaysFalse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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 in_array;
use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;

/**
* @internal
*
* @implements Mutator<Node\Stmt\ElseIf_>
*/
final class ElseIfAlwaysFalse implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
'Changes elseif condition to false',
MutatorCategory::ORTHOGONAL_REPLACEMENT,
null,
<<<'DIFF'
if ($this->foo()) {
- } elseif ($this->bar()) {
+ } elseif (false) {
}
DIFF
);
}

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

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

$cond = $node->cond;

if ($cond instanceof Node\Expr\ConstFetch) {
return !$this->isBoolean($cond);
}

return true;
}

private function isBoolean(ConstFetch $cond): bool
{
return in_array($cond->name->toLowerString(), ['true', 'false'], true);
}
}
109 changes: 109 additions & 0 deletions src/Mutator/Operator/IfAlwaysFalse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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 in_array;
use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;

/**
* @internal
*
* @implements Mutator<Node\Stmt\If_>
*/
final class IfAlwaysFalse implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
'Changes if condition to false',
MutatorCategory::ORTHOGONAL_REPLACEMENT,
null,
<<<'DIFF'
- if ($this->foo()) {
+ if (false) {
}
DIFF
);
}

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

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

$cond = $node->cond;

if ($cond instanceof Node\Expr\ConstFetch) {
return !$this->isBoolean($cond);
}

return true;
}

private function isBoolean(ConstFetch $cond): bool
{
return in_array($cond->name->toLowerString(), ['true', 'false'], true);
}
}
4 changes: 4 additions & 0 deletions src/Mutator/ProfileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ final class ProfileList
Mutator\Operator\Coalesce::class,
Mutator\Operator\Concat::class,
Mutator\Operator\Continue_::class,
Mutator\Operator\ElseIfAlwaysFalse::class,
Mutator\Operator\ElseIfNegation::class,
Mutator\Operator\Finally_::class,
Mutator\Operator\IfAlwaysFalse::class,
Mutator\Operator\IfNegation::class,
Mutator\Operator\NullSafeMethodCall::class,
Mutator\Operator\NullSafePropertyCall::class,
Expand Down Expand Up @@ -378,8 +380,10 @@ final class ProfileList
'Coalesce' => Mutator\Operator\Coalesce::class,
'Concat' => Mutator\Operator\Concat::class,
'Continue_' => Mutator\Operator\Continue_::class,
'ElseIfAlwaysFalse' => Mutator\Operator\ElseIfAlwaysFalse::class,
'ElseIfNegation' => Mutator\Operator\ElseIfNegation::class,
'Finally_' => Mutator\Operator\Finally_::class,
'IfAlwaysFalse' => Mutator\Operator\IfAlwaysFalse::class,
'IfNegation' => Mutator\Operator\IfNegation::class,
'NullSafeMethodCall' => Mutator\Operator\NullSafeMethodCall::class,
'NullSafePropertyCall' => Mutator\Operator\NullSafePropertyCall::class,
Expand Down
134 changes: 134 additions & 0 deletions tests/phpunit/Mutator/Operator/ElseIfAlwaysFalseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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 ElseIfAlwaysFalseTest 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 simple elseif condition to false' => [
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif ($this->bar()) {
return 2;
} else {
return 3;
}
PHP
,
[
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif (false) {
return 2;
} else {
return 3;
}
PHP
],
];

yield 'It mutates complex if condition to false' => [
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif ($this->bar() && $var !== false) {
return 2;
} else {
return 3;
}
PHP
,
[
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif (false) {
return 2;
} else {
return 3;
}
PHP
],
];

yield 'It does not mutate false condition' => [
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif (false) {
return 2;
}
PHP
];

yield 'It does not mutate true condition' => [
<<<'PHP'
<?php

if ($this->foo()) {
return 1;
} elseif (true) {
return 2;
}
PHP
];
}
}
Loading
Loading