Skip to content

Commit

Permalink
Add Removal mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 17, 2023
1 parent 8b6dabb commit 9908744
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 1 deletion.
1 change: 0 additions & 1 deletion WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
- [ ] Awesome docs

# Backlog Prio 1
- [ ] Add return mutators
- [ ] Add remove function call mutator
- [ ] Add null safe mutators
- [ ] Add array declaration mutators: https://stryker-mutator.io/docs/mutation-testing-elements/supported-mutators/#array-declaration
Expand Down
10 changes: 10 additions & 0 deletions src/Mutators.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
use Pest\Mutate\Mutators\Number\DecrementInteger;
use Pest\Mutate\Mutators\Number\IncrementFloat;
use Pest\Mutate\Mutators\Number\IncrementInteger;
use Pest\Mutate\Mutators\Removal\RemoveFunctionCall;
use Pest\Mutate\Mutators\Removal\RemoveMethodCall;
use Pest\Mutate\Mutators\Return\AlwaysReturnEmptyArray;
use Pest\Mutate\Mutators\Return\AlwaysReturnNull;
use Pest\Mutate\Mutators\Sets\ArithmeticSet;
Expand All @@ -139,6 +141,7 @@
use Pest\Mutate\Mutators\Sets\LogicalSet;
use Pest\Mutate\Mutators\Sets\MathSet;
use Pest\Mutate\Mutators\Sets\NumberSet;
use Pest\Mutate\Mutators\Sets\RemovalSet;
use Pest\Mutate\Mutators\Sets\ReturnSet;
use Pest\Mutate\Mutators\Sets\StringSet;
use Pest\Mutate\Mutators\Sets\VisibilitySet;
Expand Down Expand Up @@ -204,6 +207,8 @@ class Mutators

final public const SET_NUMBER = NumberSet::class;

final public const SET_REMOVAL = RemovalSet::class;

final public const SET_RETURN = ReturnSet::class;

final public const SET_STRING = StringSet::class;
Expand Down Expand Up @@ -471,6 +476,11 @@ class Mutators

final public const NUMBER_INCREMENT_INTEGER = IncrementInteger::class;

/** Removal */
final public const REMOVAL_REMOVE_FUNCTION_CALL = RemoveFunctionCall::class;

final public const REMOVAL_REMOVE_METHOD_CALL = RemoveMethodCall::class;

/** Return */
final public const RETURN_ALWAYS_RETURN_EMPTY_ARRAY = AlwaysReturnEmptyArray::class;

Expand Down
41 changes: 41 additions & 0 deletions src/Mutators/Removal/RemoveFunctionCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Mutators\Removal;

use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;

class RemoveFunctionCall extends AbstractMutator
{
public const SET = 'Removal';

public const DESCRIPTION = 'Removes a function call';

public const DIFF = <<<'DIFF'
foo(); // [tl! remove]
DIFF;

public static function nodesToHandle(): array
{
return [Expression::class];
}

public static function can(Node $node): bool
{
if (! $node instanceof Expression) {
return false;
}

return $node->expr instanceof FuncCall;
}

public static function mutate(Node $node): Node
{
return new Nop();
}
}
46 changes: 46 additions & 0 deletions src/Mutators/Removal/RemoveMethodCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Mutators\Removal;

use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;

class RemoveMethodCall extends AbstractMutator
{
public const SET = 'Removal';

public const DESCRIPTION = 'Removes a method call';

public const DIFF = <<<'DIFF'
$this->foo(); // [tl! remove]
DIFF;

public static function nodesToHandle(): array
{
return [Expression::class];
}

public static function can(Node $node): bool
{
if (! $node instanceof Expression) {
return false;
}

if ($node->expr instanceof MethodCall) {
return true;
}

return $node->expr instanceof StaticCall;
}

public static function mutate(Node $node): Node
{
return new Nop();
}
}
1 change: 1 addition & 0 deletions src/Mutators/Sets/DefaultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function mutators(): array
...LogicalSet::mutators(),
...MathSet::mutators(),
...NumberSet::mutators(),
...RemovalSet::mutators(),
...ReturnSet::mutators(),
...StringSet::mutators(),
...VisibilitySet::defaultMutators(),
Expand Down
26 changes: 26 additions & 0 deletions src/Mutators/Sets/RemovalSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Mutators\Sets;

use Pest\Mutate\Contracts\MutatorSet;
use Pest\Mutate\Mutators\Concerns\HasName;
use Pest\Mutate\Mutators\Removal\RemoveFunctionCall;
use Pest\Mutate\Mutators\Removal\RemoveMethodCall;

class RemovalSet implements MutatorSet
{
use HasName;

/**
* {@inheritDoc}
*/
public static function mutators(): array
{
return [
RemoveFunctionCall::class,
RemoveMethodCall::class,
];
}
}
2 changes: 2 additions & 0 deletions src/Support/MutatorMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Pest\Mutate\Mutators\Sets\LogicalSet;
use Pest\Mutate\Mutators\Sets\MathSet;
use Pest\Mutate\Mutators\Sets\NumberSet;
use Pest\Mutate\Mutators\Sets\RemovalSet;
use Pest\Mutate\Mutators\Sets\ReturnSet;
use Pest\Mutate\Mutators\Sets\StringSet;
use Pest\Mutate\Mutators\Sets\VisibilitySet;
Expand Down Expand Up @@ -46,6 +47,7 @@ public static function get(): array
...LaravelSet::mutators(),
...MathSet::mutators(),
...NumberSet::mutators(),
...RemovalSet::mutators(),
...ReturnSet::mutators(),
...StringSet::mutators(),
...VisibilitySet::mutators(),
Expand Down
2 changes: 2 additions & 0 deletions src/Support/Printers/DefaultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ private function writeMutationTestSummary(MutationTest $test): void

$diff = '';
foreach ($test->mutation->diff['modified'] as $line) {
$line = htmlentities($line); // TODO: this is not good, but currently required other printer breaks on `$this->foo()` because of the >
$diff .= "<div class='text-green'>+ {$line}</div>";
}
foreach ($test->mutation->diff['original'] as $line) {
$line = htmlentities($line); // TODO: this is not good, but currently required other printer breaks on `$this->foo()` because of the >
$diff .= "<div class='text-red'>- {$line}</div>";
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Mutators/Removal/RemoveFunctionCallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Pest\Mutate\Mutators\Removal\RemoveFunctionCall;

it('removes a function call', function (): void {
expect(mutateCode(RemoveFunctionCall::class, <<<'CODE'
<?php
foo();
CODE))->toBe(<<<'CODE'
<?php


CODE);
});
53 changes: 53 additions & 0 deletions tests/Mutators/Removal/RemoveMethodCallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

use Pest\Mutate\Mutators\Removal\RemoveMethodCall;

it('removes a method call', function (): void {
expect(mutateCode(RemoveMethodCall::class, <<<'CODE'
<?php
class Foo
{
function foo()
{
$this->bar();
}
}
CODE))->toBe(<<<'CODE'
<?php
class Foo
{
function foo()
{
}
}
CODE);
});

it('removes a static method call', function (): void {
expect(mutateCode(RemoveMethodCall::class, <<<'CODE'
<?php
class Foo
{
function foo()
{
self::bar();
}
}
CODE))->toBe(<<<'CODE'
<?php
class Foo
{
function foo()
{
}
}
CODE);
});

0 comments on commit 9908744

Please sign in to comment.