Skip to content

Commit

Permalink
Fixes #1023 Strip Xdebug annotations from function names in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug authored and sebastianbergmann committed Dec 11, 2023
1 parent f430918 commit 8c36c58
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Data/RawCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use function file_get_contents;
use function in_array;
use function is_file;
use function preg_replace;
use function range;
use function str_ends_with;
use function str_starts_with;
use function trim;
use SebastianBergmann\CodeCoverage\Driver\Driver;
use SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser;
Expand Down Expand Up @@ -65,6 +68,15 @@ public static function fromXdebugWithPathCoverage(array $rawCoverage): self
$functionCoverage = [];

foreach ($rawCoverage as $file => $fileCoverageData) {
// Xdebug annotates the function name of traits, strip that off
foreach ($fileCoverageData['functions'] as $existingKey => $data) {
if (str_ends_with($existingKey, '}') && !str_starts_with($existingKey, '{')) { // don't want to catch {main}
$newKey = preg_replace('/\{.*}$/', '', $existingKey);
$fileCoverageData['functions'][$newKey] = $data;
unset($fileCoverageData['functions'][$existingKey]);
}
}

$lineCoverage[$file] = $fileCoverageData['lines'];
$functionCoverage[$file] = $fileCoverageData['functions'];
}
Expand Down
197 changes: 197 additions & 0 deletions tests/tests/Data/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,201 @@ public function testCoverageForFileWithInlineAnnotations(): void
$coverage->functionCoverage()[$filename]
);
}

/**
* Xdebug annotates function names inside trait classes.
*/
public function testTraitFunctionNamesDecodedPathCoverageXDebugFormat(): void
{
$rawDataFromDriver = [
'/some/path/FooTrait.php' => [
'lines' => [
11 => 1,
12 => -1,
15 => 1,
16 => -2,
18 => 1,
],
'functions' => [
'App\\FooTrait->returnsTrue{trait-method:/some/path/FooTrait.php:9-16}' => [
'branches' => [
0 => [
'op_start' => 0,
'op_end' => 5,
'line_start' => 11,
'line_end' => 11,
'hit' => 1,
'out' => [
0 => 6,
1 => 8,
],
'out_hit' => [
0 => 0,
1 => 1,
],
],
6 => [
'op_start' => 6,
'op_end' => 7,
'line_start' => 12,
'line_end' => 12,
'hit' => 0,
'out' => [
0 => 2147483645,
],
'out_hit' => [
0 => 0,
],
],
8 => [
'op_start' => 8,
'op_end' => 12,
'line_start' => 15,
'line_end' => 16,
'hit' => 1,
'out' => [
],
'out_hit' => [
],
],
],
'paths' => [
0 => [
'path' => [
0 => 0,
1 => 6,
],
'hit' => 0,
],
1 => [
'path' => [
0 => 0,
1 => 8,
],
'hit' => 1,
],
],
],
'{main}' => [
'branches' => [
0 => [
'op_start' => 0,
'op_end' => 2,
'line_start' => 3,
'line_end' => 18,
'hit' => 1,
'out' => [
0 => 2147483645,
],
'out_hit' => [
0 => 0,
],
],
],
'paths' => [
0 => [
'path' => [
0 => 0,
],
'hit' => 1,
],
],
],
],
],
];

$functionData = [
'/some/path/FooTrait.php' => [
'App\\FooTrait->returnsTrue' => [
'branches' => [
0 => [
'op_start' => 0,
'op_end' => 5,
'line_start' => 11,
'line_end' => 11,
'hit' => 1,
'out' => [
0 => 6,
1 => 8,
],
'out_hit' => [
0 => 0,
1 => 1,
],
],
6 => [
'op_start' => 6,
'op_end' => 7,
'line_start' => 12,
'line_end' => 12,
'hit' => 0,
'out' => [
0 => 2147483645,
],
'out_hit' => [
0 => 0,
],
],
8 => [
'op_start' => 8,
'op_end' => 12,
'line_start' => 15,
'line_end' => 16,
'hit' => 1,
'out' => [
],
'out_hit' => [
],
],
],
'paths' => [
0 => [
'path' => [
0 => 0,
1 => 6,
],
'hit' => 0,
],
1 => [
'path' => [
0 => 0,
1 => 8,
],
'hit' => 1,
],
],
],
'{main}' => [
'branches' => [
0 => [
'op_start' => 0,
'op_end' => 2,
'line_start' => 3,
'line_end' => 18,
'hit' => 1,
'out' => [
0 => 2147483645,
],
'out_hit' => [
0 => 0,
],
],
],
'paths' => [
0 => [
'path' => [
0 => 0,
],
'hit' => 1,
],
],
],
],
];

$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);

$this->assertEquals($functionData, $dataObject->functionCoverage());
}
}

0 comments on commit 8c36c58

Please sign in to comment.