Skip to content

Commit

Permalink
Multiplication mutator should not mutate when return value is integer
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed May 3, 2021
1 parent c2f04fd commit 233ee09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Mutator/Arithmetic/Multiplication.php
Expand Up @@ -39,6 +39,8 @@
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ReflectionVisitor;
use function is_string;
use PhpParser\Node;

/**
Expand Down Expand Up @@ -94,7 +96,19 @@ public function canMutate(Node $node): bool
return false;
}

return true;
$functionScope = $node->getAttribute(ReflectionVisitor::FUNCTION_SCOPE_KEY);

if (!$functionScope instanceof Node\Stmt\ClassMethod) {
return true;
}

$returnType = $functionScope->getReturnType();

if ($returnType instanceof Node\Identifier) {
$returnType = $returnType->name;
}

return !(is_string($returnType) && $returnType === 'int');
}

private function isNumericOne(Node $node): bool
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/Mutator/Arithmetic/MultiplicationTest.php
Expand Up @@ -135,6 +135,20 @@ public function mutationsProvider(): iterable
<?php
$a = $b * -1.0;
PHP
];

yield 'It does not mutate when class method returns type is integer' => [
<<<'PHP'
<?php
new class
{
public function mul(int $a, int $b): int
{
return $a * $b;
}
};
PHP
];
}
Expand Down

0 comments on commit 233ee09

Please sign in to comment.