Skip to content

Commit

Permalink
Family of MethodCall Mutators
Browse files Browse the repository at this point in the history
These mutators replace any method or function call with `null`, `true`, or `false`.
This way even if a method/funcion call is embedded in other construct, be it an `if`
or another method call, there won't be a language error.
  • Loading branch information
sanmai committed Mar 22, 2018
1 parent 0dc437b commit 40b4dcd
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Mutator/Statement/MethodCallFalse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Statement;

class MethodCallFalse extends MethodCallTrue
{
const REPLACEMENT = 'false';
}
15 changes: 15 additions & 0 deletions src/Mutator/Statement/MethodCallNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Statement;

class MethodCallNull extends MethodCallTrue
{
const REPLACEMENT = 'null';
}
28 changes: 28 additions & 0 deletions src/Mutator/Statement/MethodCallTrue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Statement;

use Infection\Mutator\Util\Mutator;
use PhpParser\Node;

class MethodCallTrue extends Mutator
{
const REPLACEMENT = 'true';

public function mutate(Node $node)
{
return new Node\Expr\ConstFetch(new Node\Name(static::REPLACEMENT));
}

public function shouldMutate(Node $node): bool
{
return $node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\FuncCall;
}
}
6 changes: 6 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ final class MutatorProfile

const STATEMENT = [
Mutator\Statement\Assign::class,
Mutator\Statement\MethodCallFalse::class,
Mutator\Statement\MethodCallNull::class,
Mutator\Statement\MethodCallTrue::class,
];

const ZERO_ITERATION = [
Expand Down Expand Up @@ -214,6 +217,9 @@ final class MutatorProfile

//Statement
'Assign' => Mutator\Statement\Assign::class,
'MethodCallFalse' => Mutator\Statement\MethodCallFalse::class,
'MethodCallNull' => Mutator\Statement\MethodCallNull::class,
'MethodCallTrue' => Mutator\Statement\MethodCallTrue::class,

//Zero Iteration
'Foreach_' => Mutator\ZeroIteration\Foreach_::class,
Expand Down
63 changes: 63 additions & 0 deletions tests/Mutator/Statement/MethodCallFalseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Tests\Mutator\Statement;

use Infection\Tests\Mutator\AbstractMutatorTestCase;

class MethodCallFalseTest extends AbstractMutatorTestCase
{
/**
* @dataProvider provideMutationCases
*/
public function test_mutator($input, $expected = null)
{
$this->doTest($input, $expected);
}

public function provideMutationCases(): array
{
return [
'It removes method calls' => [
<<<'PHP'
<?php
bar();
$a = bar();
$var->foo();
$var->foo() == 1;
$var->foo()->bar();
$var->foo()->bar()->baz();
$var->foo()->bar()->baz() == $var;
if ($var->foo()) {
}
if ($var->foo()->bar() == 1) {
}
PHP
,
<<<'PHP'
<?php
false;
$a = false;
false;
false == 1;
false;
false;
false == $var;
if (false) {
}
if (false == 1) {
}
PHP
,
],
];
}
}
63 changes: 63 additions & 0 deletions tests/Mutator/Statement/MethodCallNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Tests\Mutator\Statement;

use Infection\Tests\Mutator\AbstractMutatorTestCase;

class MethodCallNullTest extends AbstractMutatorTestCase
{
/**
* @dataProvider provideMutationCases
*/
public function test_mutator($input, $expected = null)
{
$this->doTest($input, $expected);
}

public function provideMutationCases(): array
{
return [
'It removes method calls' => [
<<<'PHP'
<?php
bar();
$a = bar();
$var->foo();
$var->foo() == 1;
$var->foo()->bar();
$var->foo()->bar()->baz();
$var->foo()->bar()->baz() == $var;
if ($var->foo()) {
}
if ($var->foo()->bar() == 1) {
}
PHP
,
<<<'PHP'
<?php
null;
$a = null;
null;
null == 1;
null;
null;
null == $var;
if (null) {
}
if (null == 1) {
}
PHP
,
],
];
}
}
63 changes: 63 additions & 0 deletions tests/Mutator/Statement/MethodCallTrueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Tests\Mutator\Statement;

use Infection\Tests\Mutator\AbstractMutatorTestCase;

class MethodCallTrueTest extends AbstractMutatorTestCase
{
/**
* @dataProvider provideMutationCases
*/
public function test_mutator($input, $expected = null)
{
$this->doTest($input, $expected);
}

public function provideMutationCases(): array
{
return [
'It removes method calls' => [
<<<'PHP'
<?php
bar();
$a = bar();
$var->foo();
$var->foo() == 1;
$var->foo()->bar();
$var->foo()->bar()->baz();
$var->foo()->bar()->baz() == $var;
if ($var->foo()) {
}
if ($var->foo()->bar() == 1) {
}
PHP
,
<<<'PHP'
<?php
true;
$a = true;
true;
true == 1;
true;
true;
true == $var;
if (true) {
}
if (true == 1) {
}
PHP
,
],
];
}
}

0 comments on commit 40b4dcd

Please sign in to comment.