File tree 2 files changed +209
-0
lines changed
2 files changed +209
-0
lines changed Original file line number Diff line number Diff line change 19
19
use function file_get_contents ;
20
20
use function in_array ;
21
21
use function is_file ;
22
+ use function preg_replace ;
22
23
use function range ;
24
+ use function str_ends_with ;
25
+ use function str_starts_with ;
23
26
use function trim ;
24
27
use SebastianBergmann \CodeCoverage \Driver \Driver ;
25
28
use SebastianBergmann \CodeCoverage \StaticAnalysis \FileAnalyser ;
@@ -65,6 +68,15 @@ public static function fromXdebugWithPathCoverage(array $rawCoverage): self
65
68
$ functionCoverage = [];
66
69
67
70
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
+
68
80
$ lineCoverage [$ file ] = $ fileCoverageData ['lines ' ];
69
81
$ functionCoverage [$ file ] = $ fileCoverageData ['functions ' ];
70
82
}
Original file line number Diff line number Diff line change @@ -403,4 +403,201 @@ public function testCoverageForFileWithInlineAnnotations(): void
403
403
$ coverage ->functionCoverage ()[$ filename ]
404
404
);
405
405
}
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
+ }
406
603
}
You can’t perform that action at this time.
0 commit comments