Skip to content

Commit de33b9c

Browse files
authored
Don't double negate (#13)
1 parent 6b7223a commit de33b9c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Infection/TrinaryLogicMutator.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use Infection\Mutator\Definition;
66
use Infection\Mutator\Mutator;
77
use Infection\Mutator\MutatorCategory;
8+
use Infection\PhpParser\Visitor\ParentConnector;
89
use LogicException;
910
use PhpParser\Node;
1011
use function in_array;
1112

1213
/**
13-
* @implements Mutator<Node\Expr\MethodCall>
14+
* @implements Mutator<Node\Expr\MethodCall|Node\Expr\BooleanNot>
1415
*/
1516
final class TrinaryLogicMutator implements Mutator
1617
{
@@ -38,6 +39,18 @@ public function getName(): string
3839

3940
public function canMutate(Node $node): bool
4041
{
42+
if ($node instanceof Node\Expr\MethodCall) {
43+
$parentNode = ParentConnector::getParent($node);
44+
45+
if ($parentNode instanceof Node\Expr\BooleanNot) {
46+
return false;
47+
}
48+
}
49+
50+
if ($node instanceof Node\Expr\BooleanNot) {
51+
$node = $node->expr;
52+
}
53+
4154
if (!$node instanceof Node\Expr\MethodCall) {
4255
return false;
4356
}
@@ -55,6 +68,25 @@ public function canMutate(Node $node): bool
5568

5669
public function mutate(Node $node): iterable
5770
{
71+
if ($node instanceof Node\Expr\BooleanNot) {
72+
$node = $node->expr;
73+
if (!$node instanceof Node\Expr\MethodCall) {
74+
throw new LogicException();
75+
}
76+
77+
if (!$node->name instanceof Node\Identifier) {
78+
throw new LogicException();
79+
}
80+
81+
if ($node->name->name === 'yes') {
82+
yield new Node\Expr\MethodCall($node->var, 'no');
83+
} else {
84+
yield new Node\Expr\MethodCall($node->var, 'yes');
85+
}
86+
87+
return;
88+
}
89+
5890
if (!$node->name instanceof Node\Identifier) {
5991
throw new LogicException();
6092
}

tests/Infection/TrinaryLogicMutatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ public static function mutationsProvider(): iterable
7878
$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
7979
!$trinary->no();
8080
PHP
81+
,
82+
];
83+
84+
yield 'It does not double negate' => [
85+
<<<'PHP'
86+
<?php
87+
$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
88+
!$trinary->yes();
89+
PHP
90+
,
91+
<<<'PHP'
92+
<?php
93+
94+
$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
95+
$trinary->no();
96+
PHP
8197
,
8298
];
8399
}

0 commit comments

Comments
 (0)