Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Concat does not generate mutant when both operands are the same #1602

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Mutator/Operator/Concat.php
Expand Up @@ -40,6 +40,7 @@
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;

/**
* @internal
Expand Down Expand Up @@ -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([$node]) !== $printer->prettyPrint([$newNode])) {
yield $newNode;
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/Mutator/Operator/ConcatTest.php
Expand Up @@ -154,5 +154,37 @@ public function mutationsProvider(): iterable
,
],
];

yield 'Does not flip the same variable' => [
<<<'PHP'
<?php

$a = 'foo';
$a . $a;
PHP
,
[],
];

yield 'Does not flip the same variable - multiple concatenation' => [
<<<'PHP'
<?php

$a = 'foo';
$a . $a . $a;
PHP
,
[],
];

yield 'Does not flip the same value' => [
<<<'PHP'
<?php

'foo' . 'foo';
PHP
,
[],
];
}
}