From fe4adfdf4558e9cfa8821a3eef8e286f127d7be3 Mon Sep 17 00:00:00 2001 From: Oleg Zhulnev Date: Sun, 18 Oct 2020 22:24:20 +0300 Subject: [PATCH 1/3] Don't mutant $limit 0, -1 in preg_split --- src/Mutator/Number/AbstractNumberMutator.php | 30 ++++++++ src/Mutator/Number/DecrementInteger.php | 68 ++++++++++++------- src/Mutator/Number/IncrementInteger.php | 49 ++++++++++++- src/Mutator/Number/OneZeroInteger.php | 47 ++++++++++++- .../Mutator/Number/DecrementIntegerTest.php | 36 ++++++++++ .../Mutator/Number/IncrementIntegerTest.php | 35 ++++++++++ .../Mutator/Number/OneZeroIntegerTest.php | 22 ++++++ 7 files changed, 258 insertions(+), 29 deletions(-) diff --git a/src/Mutator/Number/AbstractNumberMutator.php b/src/Mutator/Number/AbstractNumberMutator.php index 458425c35..b27817f74 100644 --- a/src/Mutator/Number/AbstractNumberMutator.php +++ b/src/Mutator/Number/AbstractNumberMutator.php @@ -51,6 +51,13 @@ protected function isPartOfSizeComparison(Node $node): bool return $this->isSizeComparison($parent); } + protected function isPartOfComparison(Node $node): bool + { + $parent = ParentConnector::getParent($node); + + return $this->isComparison($parent); + } + private function isSizeComparison(?Node $node): bool { if ($node === null) { @@ -61,10 +68,33 @@ private function isSizeComparison(?Node $node): bool return $this->isSizeComparison(ParentConnector::findParent($node)); } + return $this->isSizeNode($node); + } + + private function isSizeNode(Node $node): bool + { return $node instanceof Node\Expr\BinaryOp\Greater || $node instanceof Node\Expr\BinaryOp\GreaterOrEqual || $node instanceof Node\Expr\BinaryOp\Smaller || $node instanceof Node\Expr\BinaryOp\SmallerOrEqual ; } + + private function isComparison(?Node $node): bool + { + if ($node === null) { + return false; + } + + if ($node instanceof Node\Expr\UnaryMinus) { + return $this->isComparison(ParentConnector::findParent($node)); + } + + return $node instanceof Node\Expr\BinaryOp\Identical + || $node instanceof Node\Expr\BinaryOp\NotIdentical + || $node instanceof Node\Expr\BinaryOp\Equal + || $node instanceof Node\Expr\BinaryOp\NotEqual + || $this->isSizeNode($node) + ; + } } diff --git a/src/Mutator/Number/DecrementInteger.php b/src/Mutator/Number/DecrementInteger.php index ba20ac9c5..08260b703 100644 --- a/src/Mutator/Number/DecrementInteger.php +++ b/src/Mutator/Number/DecrementInteger.php @@ -87,7 +87,14 @@ public function mutate(Node $node): iterable public function canMutate(Node $node): bool { - if (!$node instanceof Node\Scalar\LNumber || $node->value === 1) { + if (!$node instanceof Node\Scalar\LNumber) { + return false; + } + + if ( + $node->value === 1 + && ($this->isPartOfComparison($node) || ParentConnector::getParent($node) instanceof Node\Expr\Assign) + ) { return false; } @@ -99,6 +106,10 @@ public function canMutate(Node $node): bool return false; } + if ($this->isPregSplitLimitZeroOrMinusOneArgument($node)) { + return false; + } + return $this->isAllowedComparison($node); } @@ -108,11 +119,12 @@ private function isAllowedComparison(Node\Scalar\LNumber $node): bool return true; } - $parentNode = ParentConnector::getParent($node); - - if (!$this->isComparison($parentNode)) { + if (!$this->isPartOfComparison($node)) { return true; } + + $parentNode = ParentConnector::getParent($node); + /** @var Node\Expr\BinaryOp $parentNode */ if ($parentNode->left instanceof Node\Expr\FuncCall && $parentNode->left->name instanceof Node\Name && in_array( @@ -137,26 +149,8 @@ private function isAllowedComparison(Node\Scalar\LNumber $node): bool return true; } - private function isComparison(Node $parentNode): bool + private function isArrayZeroIndexAccess(Node\Scalar\LNumber $node): bool { - return $parentNode instanceof Node\Expr\BinaryOp\Identical - || $parentNode instanceof Node\Expr\BinaryOp\NotIdentical - || $parentNode instanceof Node\Expr\BinaryOp\Equal - || $parentNode instanceof Node\Expr\BinaryOp\NotEqual - || $parentNode instanceof Node\Expr\BinaryOp\Greater - || $parentNode instanceof Node\Expr\BinaryOp\GreaterOrEqual - || $parentNode instanceof Node\Expr\BinaryOp\Smaller - || $parentNode instanceof Node\Expr\BinaryOp\SmallerOrEqual - ; - } - - private function isArrayZeroIndexAccess(Node $node): bool - { - if (!$node instanceof Node\Scalar\LNumber) { - return false; - } - - /** @var Node\Scalar\LNumber $node */ if ($node->value !== 0) { return false; } @@ -167,4 +161,32 @@ private function isArrayZeroIndexAccess(Node $node): bool return false; } + + private function isPregSplitLimitZeroOrMinusOneArgument(Node\Scalar\LNumber $node): bool + { + if ($node->value !== 0) { + return false; + } + + $parentNode = ParentConnector::getParent($node); + + if (!$parentNode instanceof Node\Arg) { + if (!$parentNode instanceof Node\Expr\UnaryMinus) { + return false; + } + + $parentNode = ParentConnector::getParent($node); + + if (!$parentNode instanceof Node\Arg) { + return false; + } + } + + $parentNode = ParentConnector::getParent($parentNode); + + return $parentNode instanceof Node\Expr\FuncCall + && $parentNode->name instanceof Node\Name + && $parentNode->name->toLowerString() === 'preg_split' + ; + } } diff --git a/src/Mutator/Number/IncrementInteger.php b/src/Mutator/Number/IncrementInteger.php index 3ee19d63a..ea164a421 100644 --- a/src/Mutator/Number/IncrementInteger.php +++ b/src/Mutator/Number/IncrementInteger.php @@ -77,8 +77,51 @@ public function mutate(Node $node): iterable public function canMutate(Node $node): bool { - return $node instanceof Node\Scalar\LNumber - && $node->value !== 0 - && !$this->isPartOfSizeComparison($node); + if (!$node instanceof Node\Scalar\LNumber) { + return false; + } + + if ( + $node->value === 0 + && ($this->isPartOfComparison($node) || ParentConnector::getParent($node) instanceof Node\Expr\Assign) + ) { + return false; + } + + if ($this->isPartOfSizeComparison($node)) { + return false; + } + + if ($this->isPregSplitLimitZeroOrMinusOneArgument($node)) { + return false; + } + + return true; + } + + private function isPregSplitLimitZeroOrMinusOneArgument(Node\Scalar\LNumber $node): bool + { + if ($node->value !== 1) { + return false; + } + + $parentNode = ParentConnector::getParent($node); + + if (!$parentNode instanceof Node\Expr\UnaryMinus) { + return false; + } + + $parentNode = ParentConnector::getParent($parentNode); + + if (!$parentNode instanceof Node\Arg) { + return false; + } + + $parentNode = ParentConnector::getParent($parentNode); + + return $parentNode instanceof Node\Expr\FuncCall + && $parentNode->name instanceof Node\Name + && $parentNode->name->toLowerString() === 'preg_split' + ; } } diff --git a/src/Mutator/Number/OneZeroInteger.php b/src/Mutator/Number/OneZeroInteger.php index bc4b1cc75..1fe24445c 100644 --- a/src/Mutator/Number/OneZeroInteger.php +++ b/src/Mutator/Number/OneZeroInteger.php @@ -38,6 +38,7 @@ use Infection\Mutator\Definition; use Infection\Mutator\GetMutatorName; use Infection\Mutator\MutatorCategory; +use Infection\PhpParser\Visitor\ParentConnector; use PhpParser\Node; /** @@ -77,8 +78,48 @@ public function mutate(Node $node): iterable public function canMutate(Node $node): bool { - return $node instanceof Node\Scalar\LNumber - && ($node->value === 0 || $node->value === 1) - && !$this->isPartOfSizeComparison($node); + if (!$node instanceof Node\Scalar\LNumber) { + return false; + } + + if ($this->isPartOfSizeComparison($node)) { + return false; + } + + if ($node->value !== 0 && $node->value !== 1) { + return false; + } + + if ($this->isPregSplitLimitZeroOrMinusOneArgument($node)) { + return false; + } + + return true; + } + + private function isPregSplitLimitZeroOrMinusOneArgument(Node\Scalar\LNumber $node): bool + { + if ($node->value !== 1) { + return false; + } + + $parentNode = ParentConnector::getParent($node); + + if (!$parentNode instanceof Node\Expr\UnaryMinus) { + return false; + } + + $parentNode = ParentConnector::getParent($parentNode); + + if (!$parentNode instanceof Node\Arg) { + return false; + } + + $parentNode = ParentConnector::getParent($parentNode); + + return $parentNode instanceof Node\Expr\FuncCall + && $parentNode->name instanceof Node\Name + && $parentNode->name->toLowerString() === 'preg_split' + ; } } diff --git a/tests/phpunit/Mutator/Number/DecrementIntegerTest.php b/tests/phpunit/Mutator/Number/DecrementIntegerTest.php index 61876579c..b4b00a079 100644 --- a/tests/phpunit/Mutator/Number/DecrementIntegerTest.php +++ b/tests/phpunit/Mutator/Number/DecrementIntegerTest.php @@ -468,6 +468,42 @@ public function mutationsProvider(): iterable <<<'PHP' [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + Date: Tue, 20 Oct 2020 23:23:31 +0300 Subject: [PATCH 2/3] Amend TimeoutSkipped e2e test --- tests/e2e/TimeoutSkipped/expected-output.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/TimeoutSkipped/expected-output.txt b/tests/e2e/TimeoutSkipped/expected-output.txt index 4c78b02b4..8f330f497 100644 --- a/tests/e2e/TimeoutSkipped/expected-output.txt +++ b/tests/e2e/TimeoutSkipped/expected-output.txt @@ -1,8 +1,8 @@ -Total: 5 +Total: 6 Killed: 2 Errored: 0 -Escaped: 2 +Escaped: 3 Timed Out: 1 Skipped: 0 Not Covered: 0 From 35ab73bb284b98ed5faee4798def3582ed8fa1b2 Mon Sep 17 00:00:00 2001 From: Oleg Zhulnev Date: Tue, 20 Oct 2020 23:37:41 +0300 Subject: [PATCH 3/3] Amend ExecPath e2e test --- tests/e2e/Exec_Path/expected-output.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Exec_Path/expected-output.txt b/tests/e2e/Exec_Path/expected-output.txt index afaccc04a..019ca0a91 100644 --- a/tests/e2e/Exec_Path/expected-output.txt +++ b/tests/e2e/Exec_Path/expected-output.txt @@ -1,6 +1,6 @@ -Total: 6 +Total: 7 -Killed: 5 +Killed: 6 Errored: 0 Escaped: 0 Timed Out: 0