Skip to content

Commit 8c36c58

Browse files
dvdougsebastianbergmann
authored andcommitted
Fixes #1023 Strip Xdebug annotations from function names in traits
1 parent f430918 commit 8c36c58

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

src/Data/RawCodeCoverageData.php

+12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
use function file_get_contents;
2020
use function in_array;
2121
use function is_file;
22+
use function preg_replace;
2223
use function range;
24+
use function str_ends_with;
25+
use function str_starts_with;
2326
use function trim;
2427
use SebastianBergmann\CodeCoverage\Driver\Driver;
2528
use SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser;
@@ -65,6 +68,15 @@ public static function fromXdebugWithPathCoverage(array $rawCoverage): self
6568
$functionCoverage = [];
6669

6770
foreach ($rawCoverage as $file => $fileCoverageData) {
71+
// Xdebug annotates the function name of traits, strip that off
72+
foreach ($fileCoverageData['functions'] as $existingKey => $data) {
73+
if (str_ends_with($existingKey, '}') && !str_starts_with($existingKey, '{')) { // don't want to catch {main}
74+
$newKey = preg_replace('/\{.*}$/', '', $existingKey);
75+
$fileCoverageData['functions'][$newKey] = $data;
76+
unset($fileCoverageData['functions'][$existingKey]);
77+
}
78+
}
79+
6880
$lineCoverage[$file] = $fileCoverageData['lines'];
6981
$functionCoverage[$file] = $fileCoverageData['functions'];
7082
}

tests/tests/Data/RawCodeCoverageDataTest.php

+197
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,201 @@ public function testCoverageForFileWithInlineAnnotations(): void
403403
$coverage->functionCoverage()[$filename]
404404
);
405405
}
406+
407+
/**
408+
* Xdebug annotates function names inside trait classes.
409+
*/
410+
public function testTraitFunctionNamesDecodedPathCoverageXDebugFormat(): void
411+
{
412+
$rawDataFromDriver = [
413+
'/some/path/FooTrait.php' => [
414+
'lines' => [
415+
11 => 1,
416+
12 => -1,
417+
15 => 1,
418+
16 => -2,
419+
18 => 1,
420+
],
421+
'functions' => [
422+
'App\\FooTrait->returnsTrue{trait-method:/some/path/FooTrait.php:9-16}' => [
423+
'branches' => [
424+
0 => [
425+
'op_start' => 0,
426+
'op_end' => 5,
427+
'line_start' => 11,
428+
'line_end' => 11,
429+
'hit' => 1,
430+
'out' => [
431+
0 => 6,
432+
1 => 8,
433+
],
434+
'out_hit' => [
435+
0 => 0,
436+
1 => 1,
437+
],
438+
],
439+
6 => [
440+
'op_start' => 6,
441+
'op_end' => 7,
442+
'line_start' => 12,
443+
'line_end' => 12,
444+
'hit' => 0,
445+
'out' => [
446+
0 => 2147483645,
447+
],
448+
'out_hit' => [
449+
0 => 0,
450+
],
451+
],
452+
8 => [
453+
'op_start' => 8,
454+
'op_end' => 12,
455+
'line_start' => 15,
456+
'line_end' => 16,
457+
'hit' => 1,
458+
'out' => [
459+
],
460+
'out_hit' => [
461+
],
462+
],
463+
],
464+
'paths' => [
465+
0 => [
466+
'path' => [
467+
0 => 0,
468+
1 => 6,
469+
],
470+
'hit' => 0,
471+
],
472+
1 => [
473+
'path' => [
474+
0 => 0,
475+
1 => 8,
476+
],
477+
'hit' => 1,
478+
],
479+
],
480+
],
481+
'{main}' => [
482+
'branches' => [
483+
0 => [
484+
'op_start' => 0,
485+
'op_end' => 2,
486+
'line_start' => 3,
487+
'line_end' => 18,
488+
'hit' => 1,
489+
'out' => [
490+
0 => 2147483645,
491+
],
492+
'out_hit' => [
493+
0 => 0,
494+
],
495+
],
496+
],
497+
'paths' => [
498+
0 => [
499+
'path' => [
500+
0 => 0,
501+
],
502+
'hit' => 1,
503+
],
504+
],
505+
],
506+
],
507+
],
508+
];
509+
510+
$functionData = [
511+
'/some/path/FooTrait.php' => [
512+
'App\\FooTrait->returnsTrue' => [
513+
'branches' => [
514+
0 => [
515+
'op_start' => 0,
516+
'op_end' => 5,
517+
'line_start' => 11,
518+
'line_end' => 11,
519+
'hit' => 1,
520+
'out' => [
521+
0 => 6,
522+
1 => 8,
523+
],
524+
'out_hit' => [
525+
0 => 0,
526+
1 => 1,
527+
],
528+
],
529+
6 => [
530+
'op_start' => 6,
531+
'op_end' => 7,
532+
'line_start' => 12,
533+
'line_end' => 12,
534+
'hit' => 0,
535+
'out' => [
536+
0 => 2147483645,
537+
],
538+
'out_hit' => [
539+
0 => 0,
540+
],
541+
],
542+
8 => [
543+
'op_start' => 8,
544+
'op_end' => 12,
545+
'line_start' => 15,
546+
'line_end' => 16,
547+
'hit' => 1,
548+
'out' => [
549+
],
550+
'out_hit' => [
551+
],
552+
],
553+
],
554+
'paths' => [
555+
0 => [
556+
'path' => [
557+
0 => 0,
558+
1 => 6,
559+
],
560+
'hit' => 0,
561+
],
562+
1 => [
563+
'path' => [
564+
0 => 0,
565+
1 => 8,
566+
],
567+
'hit' => 1,
568+
],
569+
],
570+
],
571+
'{main}' => [
572+
'branches' => [
573+
0 => [
574+
'op_start' => 0,
575+
'op_end' => 2,
576+
'line_start' => 3,
577+
'line_end' => 18,
578+
'hit' => 1,
579+
'out' => [
580+
0 => 2147483645,
581+
],
582+
'out_hit' => [
583+
0 => 0,
584+
],
585+
],
586+
],
587+
'paths' => [
588+
0 => [
589+
'path' => [
590+
0 => 0,
591+
],
592+
'hit' => 1,
593+
],
594+
],
595+
],
596+
],
597+
];
598+
599+
$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);
600+
601+
$this->assertEquals($functionData, $dataObject->functionCoverage());
602+
}
406603
}

0 commit comments

Comments
 (0)