Skip to content

Commit

Permalink
Fix code coverage when mutating a method parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 22, 2023
1 parent d2c3cf0 commit 9e34d63
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Pest\Exceptions\ShouldNotHappen;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\PrettyPrinter\Standard;
use Symfony\Component\Finder\SplFileInfo;

Expand Down Expand Up @@ -46,11 +48,21 @@ public static function create(
$modifiedSourcePath = self::TMP_FOLDER.DIRECTORY_SEPARATOR.hash('xxh3', $modifiedSource).'.php';
file_put_contents($modifiedSourcePath, $modifiedSource);

$endLine = $originalNode->getEndLine();

if (
$originalNode->getAttribute('parent') instanceof Param &&
$originalNode->getAttribute('parent')->getAttribute('parent') instanceof ClassMethod
) {
// use the end line of the method instead if a parameter is mutated, otherwise it is not considered as covered
$endLine = $originalNode->getAttribute('parent')->getAttribute('parent')->getEndLine();
}

return new self(
$file,
$mutator,
$originalNode->getStartLine(),
$originalNode->getEndLine(),
$endLine,
self::diff($originalNode, $modifiedNode),
$modifiedSourcePath,
);
Expand Down

0 comments on commit 9e34d63

Please sign in to comment.