From 6dc7c95f86da1138f5b9bb9eb1ae5ea92aed8904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Fri, 29 Oct 2021 11:29:41 +0100 Subject: [PATCH 1/7] feat: Concat does not generate mutant when both operands are the same Closes #1601 --- src/Mutator/Operator/Concat.php | 13 ++++++-- tests/phpunit/Mutator/Operator/ConcatTest.php | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index b080180e5..11911141e 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->prettyPrintExpr($node) !== $printer->prettyPrintExpr($newNode)) { + yield $newNode; } } diff --git a/tests/phpunit/Mutator/Operator/ConcatTest.php b/tests/phpunit/Mutator/Operator/ConcatTest.php index ed4341634..4cd0281f3 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' + Date: Fri, 29 Oct 2021 13:14:10 +0100 Subject: [PATCH 2/7] Update ConcatTest.php --- tests/phpunit/Mutator/Operator/ConcatTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/Mutator/Operator/ConcatTest.php b/tests/phpunit/Mutator/Operator/ConcatTest.php index 4cd0281f3..856497418 100644 --- a/tests/phpunit/Mutator/Operator/ConcatTest.php +++ b/tests/phpunit/Mutator/Operator/ConcatTest.php @@ -163,7 +163,7 @@ public function mutationsProvider(): iterable $a . $a; PHP , - [] + [], ]; yield 'Does not flip the same variable - multiple concatenation' => [ @@ -174,7 +174,7 @@ public function mutationsProvider(): iterable $a . $a . $a; PHP , - [] + [], ]; yield 'Does not flip the same value' => [ @@ -184,7 +184,7 @@ public function mutationsProvider(): iterable 'foo' . 'foo'; PHP , - [] + [], ]; } } From 38c9321d06d15c96840432c8b1470aa0520c7688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Fri, 29 Oct 2021 13:27:58 +0100 Subject: [PATCH 3/7] use prettyPrint instead of prettyPrintExpr --- src/Mutator/Operator/Concat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index 11911141e..4f52d9d45 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -93,7 +93,7 @@ public function mutate(Node $node): iterable $newNode = new Node\Expr\BinaryOp\Concat($left, $right); - if ($printer->prettyPrintExpr($node) !== $printer->prettyPrintExpr($newNode)) { + if ($printer->prettyPrint([$node]) !== $printer->prettyPrint([$newNode])) { yield $newNode; } } From 9dfa63a42543632b7f1ef9833a029bedde934cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Sat, 30 Oct 2021 16:48:29 +0100 Subject: [PATCH 4/7] add clone per review suggestion --- src/Mutator/Operator/Concat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index 4f52d9d45..26ca8fbd9 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -93,7 +93,7 @@ public function mutate(Node $node): iterable $newNode = new Node\Expr\BinaryOp\Concat($left, $right); - if ($printer->prettyPrint([$node]) !== $printer->prettyPrint([$newNode])) { + if ($printer->prettyPrint([clone $node]) !== $printer->prettyPrint([$newNode])) { yield $newNode; } } From 46d74fe1e8dd9577bbdd348362dc19d9eb2dee21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Sun, 31 Oct 2021 10:04:52 +0000 Subject: [PATCH 5/7] suppress psalm issue - ImpureMethodCall - per @maks-rafalko --- src/Mutator/Operator/Concat.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index 26ca8fbd9..055e8dd0a 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -93,6 +93,7 @@ public function mutate(Node $node): iterable $newNode = new Node\Expr\BinaryOp\Concat($left, $right); + /** @psalm-suppress ImpureMethodCall */ if ($printer->prettyPrint([clone $node]) !== $printer->prettyPrint([$newNode])) { yield $newNode; } From 730f3ff28f564cf160359713a6ec3a157a4829cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Sun, 31 Oct 2021 10:09:08 +0000 Subject: [PATCH 6/7] Update Concat.php --- src/Mutator/Operator/Concat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mutator/Operator/Concat.php b/src/Mutator/Operator/Concat.php index 055e8dd0a..af11043c1 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -93,7 +93,7 @@ public function mutate(Node $node): iterable $newNode = new Node\Expr\BinaryOp\Concat($left, $right); - /** @psalm-suppress ImpureMethodCall */ + /* @psalm-suppress ImpureMethodCall */ if ($printer->prettyPrint([clone $node]) !== $printer->prettyPrint([$newNode])) { yield $newNode; } From c1bc8213dc3e5cfac59c76e12d5c2cca47a5f2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Sun, 31 Oct 2021 10:18:02 +0000 Subject: [PATCH 7/7] suppress issue in psalm.xml --- psalm.xml | 8 ++++++++ src/Mutator/Operator/Concat.php | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) 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 af11043c1..26ca8fbd9 100644 --- a/src/Mutator/Operator/Concat.php +++ b/src/Mutator/Operator/Concat.php @@ -93,7 +93,6 @@ public function mutate(Node $node): iterable $newNode = new Node\Expr\BinaryOp\Concat($left, $right); - /* @psalm-suppress ImpureMethodCall */ if ($printer->prettyPrint([clone $node]) !== $printer->prettyPrint([$newNode])) { yield $newNode; }