Skip to content

Commit

Permalink
TernaryNegated mutator: do properly mutate shorthand ternaries
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 22, 2023
1 parent 7d33e0c commit 8755451
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Mutators/ControlStructures/TernaryNegated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Pest\Mutate\Mutators\Abstract\AbstractMutator;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Ternary;

Expand All @@ -28,6 +29,10 @@ public static function nodesToHandle(): array
public static function mutate(Node $node): Node
{
/** @var Ternary $node */
if (! $node->if instanceof Expr) {
$node->if = $node->cond;
}

$node->cond = new BooleanNot($node->cond);

return $node;
Expand Down
12 changes: 12 additions & 0 deletions tests/Mutators/ControlStructures/TernaryNegatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@
return !($a > $b) ? 'a' : 'b';
CODE);
});

it('mutates a shorthand ternary condition to be negated', function (): void {
expect(mutateCode(TernaryNegated::class, <<<'CODE'
<?php
return $a ?: $b;
CODE))->toBe(<<<'CODE'
<?php
return !$a ? $a : $b;
CODE);
});

0 comments on commit 8755451

Please sign in to comment.