Skip to content

Commit

Permalink
Ignore 1 in Div assignment operator (DivEqual). (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
uncaught committed Oct 16, 2023
1 parent 59211d2 commit 3a39f6c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Mutator/Arithmetic/DivEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ public function mutate(Node $node): iterable

public function canMutate(Node $node): bool
{
return $node instanceof Node\Expr\AssignOp\Div;
if (!$node instanceof Node\Expr\AssignOp\Div) {
return false;
}

if ($this->isNumericOne($node->expr)) {
return false;
}

if ($node->expr instanceof Node\Expr\UnaryMinus && $this->isNumericOne($node->expr->expr)) {
return false;
}

return true;
}

private function isNumericOne(Node $node): bool
{
if ($node instanceof Node\Scalar\LNumber && $node->value === 1) {
return true;
}

return $node instanceof Node\Scalar\DNumber && $node->value === 1.0;
}
}
40 changes: 40 additions & 0 deletions tests/phpunit/Mutator/Arithmetic/DivEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@ public function mutationsProvider(): iterable
<?php
$a = 10 / 2;
PHP
,
];

yield 'It does not mutate division by 1 to avoid an equivalent mutation' => [
<<<'PHP'
<?php
$a = 1;
$a /= 1;
PHP
,
];

yield 'It does not mutate division by -1 to avoid an equivalent mutation' => [
<<<'PHP'
<?php
$a = 1;
$a /= -1;
PHP
,
];

yield 'It does not mutate division by 1.0 to avoid an equivalent mutation' => [
<<<'PHP'
<?php
$a = 1;
$a /= 1.0;
PHP
,
];

yield 'It does not mutate division by -1.0 to avoid an equivalent mutation' => [
<<<'PHP'
<?php
$a = 1;
$a /= -1.0;
PHP
,
];
Expand Down

0 comments on commit 3a39f6c

Please sign in to comment.