From 87554518b1ea28248828a0e859a51c1f62ad847f Mon Sep 17 00:00:00 2001 From: Sandro Gehri Date: Wed, 22 Nov 2023 13:53:32 +0100 Subject: [PATCH] TernaryNegated mutator: do properly mutate shorthand ternaries --- src/Mutators/ControlStructures/TernaryNegated.php | 5 +++++ .../ControlStructures/TernaryNegatedTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Mutators/ControlStructures/TernaryNegated.php b/src/Mutators/ControlStructures/TernaryNegated.php index 16bc972..205c0c0 100644 --- a/src/Mutators/ControlStructures/TernaryNegated.php +++ b/src/Mutators/ControlStructures/TernaryNegated.php @@ -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; @@ -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; diff --git a/tests/Mutators/ControlStructures/TernaryNegatedTest.php b/tests/Mutators/ControlStructures/TernaryNegatedTest.php index 6e62907..8bd1241 100644 --- a/tests/Mutators/ControlStructures/TernaryNegatedTest.php +++ b/tests/Mutators/ControlStructures/TernaryNegatedTest.php @@ -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' + toBe(<<<'CODE' +