-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb49c6a
commit c1639d9
Showing
23 changed files
with
332 additions
and
119 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
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pest\Mutate\Mutators\Return; | ||
|
||
use Pest\Mutate\Mutators\Abstract\AbstractMutator; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Return_; | ||
|
||
class AlwaysReturnEmptyArray extends AbstractMutator | ||
{ | ||
public const SET = 'Return'; | ||
|
||
public const DESCRIPTION = 'Mutates a return statement to an empty array'; | ||
|
||
public const DIFF = <<<'DIFF' | ||
return [1]; // [tl! remove] | ||
return []; // [tl! add] | ||
DIFF; | ||
|
||
public static function nodesToHandle(): array | ||
{ | ||
return [Return_::class]; | ||
} | ||
|
||
public static function can(Node $node): bool | ||
{ | ||
if (! $node instanceof Return_) { | ||
return false; | ||
} | ||
|
||
$parent = $node->getAttribute('parent'); | ||
|
||
if (! $parent instanceof Function_) { | ||
return false; | ||
} | ||
|
||
if ($node->expr instanceof Array_ && $node->expr->items === []) { | ||
return false; | ||
} | ||
|
||
return $parent->returnType instanceof Identifier && | ||
$parent->returnType->name === 'array'; | ||
} | ||
|
||
public static function mutate(Node $node): Node | ||
{ | ||
/** @var Return_ $node */ | ||
$node->expr->items = []; // @phpstan-ignore-line | ||
|
||
return $node; | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pest\Mutate\Mutators\Return; | ||
|
||
use Pest\Mutate\Mutators\Abstract\AbstractMutator; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Return_; | ||
|
||
class AlwaysReturnNull extends AbstractMutator | ||
{ | ||
public const SET = 'Return'; | ||
|
||
public const DESCRIPTION = 'Mutates a return statement to null if it is not null'; | ||
|
||
public const DIFF = <<<'DIFF' | ||
return $a; // [tl! remove] | ||
return null; // [tl! add] | ||
DIFF; | ||
|
||
public static function nodesToHandle(): array | ||
{ | ||
return [Return_::class]; | ||
} | ||
|
||
public static function can(Node $node): bool | ||
{ | ||
if (! $node instanceof Return_) { | ||
return false; | ||
} | ||
|
||
$parent = $node->getAttribute('parent'); | ||
|
||
if (! $parent instanceof Function_) { | ||
return false; | ||
} | ||
|
||
if ($node->expr instanceof ConstFetch && $node->expr->name->parts[0] === 'null') { | ||
return false; | ||
} | ||
|
||
return $parent->returnType === null || | ||
$parent->returnType->getType() === 'NullableType'; | ||
} | ||
|
||
public static function mutate(Node $node): Node | ||
{ | ||
/** @var Return_ $node */ | ||
$node->expr = new ConstFetch(new Name('null')); | ||
|
||
return $node; | ||
} | ||
} |
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,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\Return\AlwaysReturnEmptyArray; | ||
use Pest\Mutate\Mutators\Return\AlwaysReturnNull; | ||
|
||
class ReturnSet implements MutatorSet | ||
{ | ||
use HasName; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function mutators(): array | ||
{ | ||
return [ | ||
AlwaysReturnNull::class, | ||
AlwaysReturnEmptyArray::class, | ||
]; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.