From 1892158e83ce708f267779a287938cd1b8806557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Sun, 31 Oct 2021 11:02:30 +0000 Subject: [PATCH] feat: Concat does not generate mutant when both operands are the same (#1602) * feat: Concat does not generate mutant when both operands are the same Closes #1601 * Update ConcatTest.php * use prettyPrint instead of prettyPrintExpr * add clone per review suggestion * suppress psalm issue - ImpureMethodCall - per @maks-rafalko * Update Concat.php * suppress issue in psalm.xml --- psalm.xml | 8 +++++ src/Mutator/Operator/Concat.php | 13 ++++++-- tests/phpunit/Mutator/Operator/ConcatTest.php | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/psalm.xml b/psalm.xml index 7c69aab1c..493815a9f 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,4 +14,12 @@ + + + + + + + + diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index b080180e5..26ca8fbd9 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -40,6 +40,7 @@ use Infection\Mutator\Mutator; use Infection\Mutator\MutatorCategory; use PhpParser\Node; +use PhpParser\PrettyPrinter\Standard; /** * @internal @@ -81,13 +82,19 @@ public static function getDefinition(): ?Definition */ public function mutate(Node $node): iterable { + $printer = new Standard(); + if ($node->left instanceof Node\Expr\BinaryOp\Concat) { $left = new Node\Expr\BinaryOp\Concat($node->left->left, $node->right); $right = $node->left->right; - - yield new Node\Expr\BinaryOp\Concat($left, $right); } else { - yield new Node\Expr\BinaryOp\Concat($node->right, $node->left); + [$left, $right] = [$node->right, $node->left]; + } + + $newNode = new Node\Expr\BinaryOp\Concat($left, $right); + + if ($printer->prettyPrint([clone $node]) !== $printer->prettyPrint([$newNode])) { + yield $newNode; } } diff --git a/tests/phpunit/Mutator/Operator/ConcatTest.php b/tests/phpunit/Mutator/Operator/ConcatTest.php index ed4341634..856497418 100644 --- a/tests/phpunit/Mutator/Operator/ConcatTest.php +++ b/tests/phpunit/Mutator/Operator/ConcatTest.php @@ -154,5 +154,37 @@ public function mutationsProvider(): iterable , ], ]; + + yield 'Does not flip the same variable' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +