-
-
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.
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
Showing
7 changed files
with
253 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
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'; | ||
} |
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,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'; | ||
} |
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,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; | ||
} | ||
} |
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,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 ($bar || $var->foo()) { | ||
} | ||
if ($var->foo()->bar() == 1) { | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
false; | ||
$a = false; | ||
false; | ||
false == 1; | ||
false; | ||
false; | ||
false == $var; | ||
if ($bar || false) { | ||
} | ||
if (false == 1) { | ||
} | ||
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,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 ($bar || $var->foo()) { | ||
} | ||
if ($var->foo()->bar() == 1) { | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
null; | ||
$a = null; | ||
null; | ||
null == 1; | ||
null; | ||
null; | ||
null == $var; | ||
if ($bar || null) { | ||
} | ||
if (null == 1) { | ||
} | ||
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,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 ($bar || $var->foo()) { | ||
} | ||
if ($var->foo()->bar() == 1) { | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
true; | ||
$a = true; | ||
true; | ||
true == 1; | ||
true; | ||
true; | ||
true == $var; | ||
if ($bar || true) { | ||
} | ||
if (true == 1) { | ||
} | ||
PHP | ||
, | ||
], | ||
]; | ||
} | ||
} |