Skip to content

Commit

Permalink
Do not mutate $var instanceof ClassName inside assert() function …
Browse files Browse the repository at this point in the history
…as it's impossible or hard to kill

Fixes #1845
  • Loading branch information
maks-rafalko committed May 11, 2023
1 parent fae8d1f commit 1592133
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Mutator/Boolean/InstanceOf_.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ParentConnector;
use PhpParser\Node;

/**
Expand Down Expand Up @@ -80,6 +81,26 @@ public function mutate(Node $node): iterable

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

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

return true;
}

private function isArgumentOfAssertFunction(Node\Expr\Instanceof_ $node): bool
{
$parentNode = ParentConnector::findParent($node);
$grandParentNode = $parentNode !== null ? ParentConnector::findParent($parentNode) : null;

if (!$grandParentNode instanceof Node\Expr\FuncCall || !$grandParentNode->name instanceof Node\Name) {

Check warning on line 100 in src/Mutator/Boolean/InstanceOf_.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ { $parentNode = ParentConnector::findParent($node); $grandParentNode = $parentNode !== null ? ParentConnector::findParent($parentNode) : null; - if (!$grandParentNode instanceof Node\Expr\FuncCall || !$grandParentNode->name instanceof Node\Name) { + if (!$grandParentNode instanceof Node\Expr\FuncCall || !true) { return false; } return $grandParentNode->name->toLowerString() === 'assert'; } }
return false;
}

return $grandParentNode->name->toLowerString() === 'assert';
}
}
41 changes: 41 additions & 0 deletions tests/phpunit/Mutator/Boolean/InstanceOf_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,46 @@ public function mutationsProvider(): iterable
,
],
];

yield 'It does not mutate an instanceof comparison inside `assert()` function call with a class literal' => [
<<<'PHP'
<?php
return assert($example instanceof Example);
PHP
,
];

yield 'It does not mutate an instanceof comparison inside `assert()` function call with variable' => [
<<<'PHP'
<?php
return assert($example instanceof $foo);
PHP
,
];

yield 'It mutates an instanceof comparison inside other than `assert()` functions' => [
<<<'PHP'
<?php
return someFunc($example instanceof $foo);
PHP
,
[
<<<'PHP'
<?php
return someFunc(true);
PHP
,
<<<'PHP'
<?php
return someFunc(false);
PHP
,
],
];
}
}

0 comments on commit 1592133

Please sign in to comment.