18
18
use function array_values ;
19
19
use function count ;
20
20
use function explode ;
21
- use function file ;
22
21
use function file_exists ;
23
22
use function get_class ;
24
23
use function is_array ;
25
24
use function sort ;
26
- use function strpos ;
27
- use function trim ;
28
- use OutOfBoundsException ;
29
- use PHP_Token_CLASS ;
30
- use PHP_Token_COMMENT ;
31
- use PHP_Token_DOC_COMMENT ;
32
- use PHP_Token_FUNCTION ;
33
- use PHP_Token_INTERFACE ;
34
25
use PHP_Token_Stream ;
35
26
use PHP_Token_Stream_CachingFactory ;
36
- use PHP_Token_TRAIT ;
37
27
use PHPUnit \Framework \TestCase ;
38
28
use PHPUnit \Runner \PhptTestCase ;
39
29
use PHPUnit \Util \Test ;
@@ -103,14 +93,14 @@ final class CodeCoverage
103
93
private $ data ;
104
94
105
95
/**
106
- * @var array
96
+ * @var IgnoredLinesFinder
107
97
*/
108
- private $ ignoredLines = [] ;
98
+ private $ ignoredLinesFinder ;
109
99
110
100
/**
111
101
* @var bool
112
102
*/
113
- private $ disableIgnoredLines = false ;
103
+ private $ useAnnotationsForIgnoringCode = true ;
114
104
115
105
/**
116
106
* Test data.
@@ -133,10 +123,11 @@ final class CodeCoverage
133
123
134
124
public function __construct (Driver $ driver , Filter $ filter )
135
125
{
136
- $ this ->driver = $ driver ;
137
- $ this ->filter = $ filter ;
138
- $ this ->data = new ProcessedCodeCoverageData ;
139
- $ this ->wizard = new Wizard ;
126
+ $ this ->driver = $ driver ;
127
+ $ this ->filter = $ filter ;
128
+ $ this ->data = new ProcessedCodeCoverageData ;
129
+ $ this ->ignoredLinesFinder = new IgnoredLinesFinder ;
130
+ $ this ->wizard = new Wizard ;
140
131
}
141
132
142
133
/**
@@ -264,7 +255,10 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append =
264
255
}
265
256
266
257
$ this ->applyFilter ($ rawData );
267
- $ this ->applyIgnoredLinesFilter ($ rawData );
258
+
259
+ if ($ this ->useAnnotationsForIgnoringCode ) {
260
+ $ this ->applyIgnoredLinesFilter ($ rawData );
261
+ }
268
262
269
263
$ this ->data ->initializeUnseenData ($ rawData );
270
264
@@ -373,12 +367,12 @@ public function doNotProcessUncoveredFiles(): void
373
367
374
368
public function enableAnnotationsForIgnoringCode (): void
375
369
{
376
- $ this ->disableIgnoredLines = false ;
370
+ $ this ->useAnnotationsForIgnoringCode = true ;
377
371
}
378
372
379
373
public function disableAnnotationsForIgnoringCode (): void
380
374
{
381
- $ this ->disableIgnoredLines = true ;
375
+ $ this ->useAnnotationsForIgnoringCode = false ;
382
376
}
383
377
384
378
public function ignoreDeprecatedCode (): void
@@ -479,7 +473,14 @@ private function applyIgnoredLinesFilter(RawCodeCoverageData $data): void
479
473
continue ;
480
474
}
481
475
482
- $ data ->removeCoverageDataForLines ($ filename , $ this ->getLinesToBeIgnored ($ filename ));
476
+ $ data ->removeCoverageDataForLines (
477
+ $ filename ,
478
+ $ this ->ignoredLinesFinder ->findIgnoredLinesInFile (
479
+ $ filename ,
480
+ $ this ->useAnnotationsForIgnoringCode ,
481
+ $ this ->ignoreDeprecatedCode
482
+ )
483
+ );
483
484
}
484
485
}
485
486
@@ -506,102 +507,6 @@ private function addUncoveredFilesFromFilter(): void
506
507
}
507
508
}
508
509
509
- private function getLinesToBeIgnored (string $ fileName ): array
510
- {
511
- if (isset ($ this ->ignoredLines [$ fileName ])) {
512
- return $ this ->ignoredLines [$ fileName ];
513
- }
514
-
515
- try {
516
- return $ this ->getLinesToBeIgnoredInner ($ fileName );
517
- } catch (OutOfBoundsException $ e ) {
518
- // This can happen with PHP_Token_Stream if the file is syntactically invalid,
519
- // and probably affects a file that wasn't executed.
520
- return [];
521
- }
522
- }
523
-
524
- private function getLinesToBeIgnoredInner (string $ fileName ): array
525
- {
526
- $ this ->ignoredLines [$ fileName ] = [];
527
-
528
- if ($ this ->cacheTokens ) {
529
- $ tokens = PHP_Token_Stream_CachingFactory::get ($ fileName );
530
- } else {
531
- $ tokens = new PHP_Token_Stream ($ fileName );
532
- }
533
-
534
- if ($ this ->disableIgnoredLines ) {
535
- $ this ->ignoredLines [$ fileName ] = array_unique ($ this ->ignoredLines [$ fileName ]);
536
- sort ($ this ->ignoredLines [$ fileName ]);
537
-
538
- return $ this ->ignoredLines [$ fileName ];
539
- }
540
-
541
- $ ignore = false ;
542
- $ stop = false ;
543
-
544
- foreach ($ tokens ->tokens () as $ token ) {
545
- switch (get_class ($ token )) {
546
- case PHP_Token_COMMENT::class:
547
- case PHP_Token_DOC_COMMENT::class:
548
- $ _token = trim ((string ) $ token );
549
-
550
- if ($ _token === '// @codeCoverageIgnore ' ||
551
- $ _token === '//@codeCoverageIgnore ' ) {
552
- $ ignore = true ;
553
- $ stop = true ;
554
- } elseif ($ _token === '// @codeCoverageIgnoreStart ' ||
555
- $ _token === '//@codeCoverageIgnoreStart ' ) {
556
- $ ignore = true ;
557
- } elseif ($ _token === '// @codeCoverageIgnoreEnd ' ||
558
- $ _token === '//@codeCoverageIgnoreEnd ' ) {
559
- $ stop = true ;
560
- }
561
-
562
- break ;
563
-
564
- case PHP_Token_INTERFACE::class:
565
- case PHP_Token_TRAIT::class:
566
- case PHP_Token_CLASS::class:
567
- $ this ->ignoredLines [$ fileName ][] = $ token ->getLine (); //work around https://bugs.xdebug.org/view.php?id=1798
568
- // Intentional fallthrough
569
- case PHP_Token_FUNCTION::class:
570
- /* @var \PHP_Token_Interface $token */
571
-
572
- $ docblock = (string ) $ token ->getDocblock ();
573
-
574
- if (strpos ($ docblock , '@codeCoverageIgnore ' ) || ($ this ->ignoreDeprecatedCode && strpos ($ docblock , '@deprecated ' ))) {
575
- $ endLine = $ token ->getEndLine ();
576
-
577
- for ($ i = $ token ->getLine (); $ i <= $ endLine ; $ i ++) {
578
- $ this ->ignoredLines [$ fileName ][] = $ i ;
579
- }
580
- }
581
-
582
- break ;
583
- }
584
-
585
- if ($ ignore ) {
586
- $ this ->ignoredLines [$ fileName ][] = $ token ->getLine ();
587
-
588
- if ($ stop ) {
589
- $ ignore = false ;
590
- $ stop = false ;
591
- }
592
- }
593
- }
594
-
595
- $ this ->ignoredLines [$ fileName ] = array_unique (
596
- $ this ->ignoredLines [$ fileName ]
597
- );
598
-
599
- $ this ->ignoredLines [$ fileName ] = array_unique ($ this ->ignoredLines [$ fileName ]);
600
- sort ($ this ->ignoredLines [$ fileName ]);
601
-
602
- return $ this ->ignoredLines [$ fileName ];
603
- }
604
-
605
510
/**
606
511
* @throws UnintentionallyCoveredCodeException
607
512
* @throws ReflectionException
0 commit comments