Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Infection/TrinaryLogicMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use Infection\Mutator\Definition;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ParentConnector;
use LogicException;
use PhpParser\Node;
use function in_array;

/**
* @implements Mutator<Node\Expr\MethodCall>
* @implements Mutator<Node\Expr\MethodCall|Node\Expr\BooleanNot>
*/
final class TrinaryLogicMutator implements Mutator
{
Expand Down Expand Up @@ -38,6 +39,18 @@ public function getName(): string

public function canMutate(Node $node): bool
{
if ($node instanceof Node\Expr\MethodCall) {
$parentNode = ParentConnector::getParent($node);

if ($parentNode instanceof Node\Expr\BooleanNot) {
return false;
}
}

if ($node instanceof Node\Expr\BooleanNot) {
$node = $node->expr;
}

if (!$node instanceof Node\Expr\MethodCall) {
return false;
}
Expand All @@ -55,6 +68,25 @@ public function canMutate(Node $node): bool

public function mutate(Node $node): iterable
{
if ($node instanceof Node\Expr\BooleanNot) {
$node = $node->expr;
if (!$node instanceof Node\Expr\MethodCall) {
throw new LogicException();
}

if (!$node->name instanceof Node\Identifier) {
throw new LogicException();
}

if ($node->name->name === 'yes') {
yield new Node\Expr\MethodCall($node->var, 'no');
} else {
yield new Node\Expr\MethodCall($node->var, 'yes');
}

return;
}

if (!$node->name instanceof Node\Identifier) {
throw new LogicException();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Infection/TrinaryLogicMutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public static function mutationsProvider(): iterable
$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
!$trinary->no();
PHP
,
];

yield 'It does not double negate' => [
<<<'PHP'
<?php
$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
!$trinary->yes();
PHP
,
<<<'PHP'
<?php

$trinary = \PHPStan\Type\IsSuperTypeOfResult::createYes();
$trinary->no();
PHP
,
];
}
Expand Down