diff --git a/src/Mutator/Arithmetic/Division.php b/src/Mutator/Arithmetic/Division.php index fe77a923a..5c2134553 100644 --- a/src/Mutator/Arithmetic/Division.php +++ b/src/Mutator/Arithmetic/Division.php @@ -56,6 +56,31 @@ public function mutate(Node $node) protected function mutatesNode(Node $node): bool { - return $node instanceof Node\Expr\BinaryOp\Div; + if (!$node instanceof Node\Expr\BinaryOp\Div) { + return false; + } + + if ($this->isNumericOne($node->left) || $this->isNumericOne($node->right)) { + return false; + } + + if ($node->left instanceof Node\Expr\UnaryMinus && $this->isNumericOne($node->left->expr)) { + return false; + } + + if ($node->right instanceof Node\Expr\UnaryMinus && $this->isNumericOne($node->right->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; } } diff --git a/src/Mutator/Arithmetic/Multiplication.php b/src/Mutator/Arithmetic/Multiplication.php index 4557041fe..4969f18e8 100644 --- a/src/Mutator/Arithmetic/Multiplication.php +++ b/src/Mutator/Arithmetic/Multiplication.php @@ -56,6 +56,31 @@ public function mutate(Node $node) protected function mutatesNode(Node $node): bool { - return $node instanceof Node\Expr\BinaryOp\Mul; + if (!$node instanceof Node\Expr\BinaryOp\Mul) { + return false; + } + + if ($this->isNumericOne($node->left) || $this->isNumericOne($node->right)) { + return false; + } + + if ($node->left instanceof Node\Expr\UnaryMinus && $this->isNumericOne($node->left->expr)) { + return false; + } + + if ($node->right instanceof Node\Expr\UnaryMinus && $this->isNumericOne($node->right->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; } } diff --git a/tests/Mutator/Arithmetic/DivisionTest.php b/tests/Mutator/Arithmetic/DivisionTest.php index be7dba653..56c4147d7 100644 --- a/tests/Mutator/Arithmetic/DivisionTest.php +++ b/tests/Mutator/Arithmetic/DivisionTest.php @@ -50,10 +50,9 @@ public function test_mutator($input, $expected = null): void $this->doTest($input, $expected); } - public function provideMutationCases(): array + public function provideMutationCases(): \Generator { - return [ - 'It changes regular divison' => [ + yield 'It changes regular divison' => [ <<<'PHP' [ + ]; + + yield 'It does not change division equals' => [ <<<'PHP' mutate($code); + yield 'It does not mutate when the left side is 1 to avoid an equivalent mutation' => [ + <<<'PHP' + [ + <<<'PHP' [ + <<<'PHP' + [ + <<<'PHP' +assertSame($expectedMutatedCode, $mutations[0]); +$a = $b / -1; +PHP + ]; + + yield 'It does not mutate when the left side is 1.0 to avoid an equivalent mutation' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +doTest($input, $expected); } - public function provideMutationCases(): array + public function provideMutationCases(): \Generator { - return [ - 'It mutates normal multiplication' => [ + yield 'It mutates normal multiplication' => [ <<<'PHP' [ + ]; + + yield 'It does not mutate multiplication equals' => [ <<<'PHP' [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +