Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplication mutator should not mutate when return value is integer #1515

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Mutator/Arithmetic/Multiplication.php
Expand Up @@ -39,6 +39,9 @@
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ParentConnector;
use Infection\PhpParser\Visitor\ReflectionVisitor;
use function is_string;
use PhpParser\Node;

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should also check that the current node is a return expression as it's done e.g. here: https://github.com/infection/infection/blob/master/src/Mutator/ReturnValue/This.php#L84

Otherwise, the following code will not be mutated, but it should:

public function mul(int $a, int $b): int
{
    $c = $a * $b;

    return 1;
}

btw, could you please add such case to tests so that we are sure it is mutated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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

$parentNode = ParentConnector::getParent($node);

if (!$parentNode instanceof Node\Stmt\Return_) {
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
42 changes: 42 additions & 0 deletions tests/phpunit/Mutator/Arithmetic/MultiplicationTest.php
Expand Up @@ -135,6 +135,48 @@ 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
];

yield 'It mutates when class method returns type is integer' => [
<<<'PHP'
<?php

new class
{
public function mul(int $a, int $b): int
{
$c = $a * $b;

return 1;
}
};
PHP,
<<<'PHP'
<?php

new class
{
public function mul(int $a, int $b) : int
{
$c = $a / $b;
return 1;
}
};
PHP
];
}
Expand Down