11
11
12
12
use PHP_CodeSniffer \Exceptions \TokenizerException ;
13
13
use PHP_CodeSniffer \Util ;
14
+ use PHP_CodeSniffer \Util \IgnoreList ;
14
15
15
16
abstract class Tokenizer
16
17
{
@@ -173,6 +174,7 @@ private function createPositionMap()
173
174
$ lineNumber = 1 ;
174
175
$ eolLen = strlen ($ this ->eolChar );
175
176
$ ignoring = null ;
177
+ $ ignoreAll = IgnoreList::ignoringAll ();
176
178
$ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
177
179
178
180
$ checkEncoding = false ;
@@ -277,15 +279,15 @@ private function createPositionMap()
277
279
if ($ ignoring === null
278
280
&& strpos ($ commentText , '@codingStandardsIgnoreStart ' ) !== false
279
281
) {
280
- $ ignoring = [ ' .all ' => true ] ;
282
+ $ ignoring = $ ignoreAll ;
281
283
if ($ ownLine === true ) {
282
284
$ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoring ;
283
285
}
284
286
} else if ($ ignoring !== null
285
287
&& strpos ($ commentText , '@codingStandardsIgnoreEnd ' ) !== false
286
288
) {
287
289
if ($ ownLine === true ) {
288
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
290
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
289
291
} else {
290
292
$ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoring ;
291
293
}
@@ -294,7 +296,7 @@ private function createPositionMap()
294
296
} else if ($ ignoring === null
295
297
&& strpos ($ commentText , '@codingStandardsIgnoreLine ' ) !== false
296
298
) {
297
- $ ignoring = [ ' .all ' => true ] ;
299
+ $ ignoring = $ ignoreAll ;
298
300
if ($ ownLine === true ) {
299
301
$ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoring ;
300
302
$ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ ignoring ;
@@ -393,7 +395,7 @@ private function createPositionMap()
393
395
if (substr ($ commentTextLower , 0 , 9 ) === 'phpcs:set ' ) {
394
396
// Ignore standards for complete lines that change sniff settings.
395
397
if ($ lineHasOtherTokens === false ) {
396
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
398
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
397
399
}
398
400
399
401
// Need to maintain case here, to get the correct sniff code.
@@ -416,42 +418,28 @@ private function createPositionMap()
416
418
} else if (substr ($ commentTextLower , 0 , 13 ) === 'phpcs:disable ' ) {
417
419
if ($ lineHasOtherContent === false ) {
418
420
// Completely ignore the comment line.
419
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = ['.all ' => true ];
420
- }
421
-
422
- if ($ ignoring === null ) {
423
- $ ignoring = [];
421
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
424
422
}
425
423
426
424
$ disabledSniffs = [];
427
425
428
426
$ additionalText = substr ($ commentText , 14 );
429
427
if (empty ($ additionalText ) === true ) {
430
- $ ignoring = [ ' .all ' => true ] ;
428
+ $ ignoring = $ ignoreAll ;
431
429
} else {
430
+ if ($ ignoring === null ) {
431
+ $ ignoring = IgnoreList::ignoringNone ();
432
+ } else {
433
+ $ ignoring = clone $ ignoring ;
434
+ }
435
+
432
436
$ parts = explode (', ' , $ additionalText );
433
437
foreach ($ parts as $ sniffCode ) {
434
438
$ sniffCode = trim ($ sniffCode );
435
439
$ disabledSniffs [$ sniffCode ] = true ;
436
- $ ignoring [$ sniffCode ] = true ;
437
-
438
- // This newly disabled sniff might be disabling an existing
439
- // enabled exception that we are tracking.
440
- if (isset ($ ignoring ['.except ' ]) === true ) {
441
- foreach (array_keys ($ ignoring ['.except ' ]) as $ ignoredSniffCode ) {
442
- if ($ ignoredSniffCode === $ sniffCode
443
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
444
- ) {
445
- unset($ ignoring ['.except ' ][$ ignoredSniffCode ]);
446
- }
447
- }
448
-
449
- if (empty ($ ignoring ['.except ' ]) === true ) {
450
- unset($ ignoring ['.except ' ]);
451
- }
452
- }
453
- }//end foreach
454
- }//end if
440
+ $ ignoring ->set ($ sniffCode , true );
441
+ }
442
+ }
455
443
456
444
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_DISABLE ;
457
445
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_DISABLE ' ;
@@ -464,49 +452,22 @@ private function createPositionMap()
464
452
if (empty ($ additionalText ) === true ) {
465
453
$ ignoring = null ;
466
454
} else {
467
- $ parts = explode (', ' , $ additionalText );
455
+ $ ignoring = clone $ ignoring ;
456
+ $ parts = explode (', ' , $ additionalText );
468
457
foreach ($ parts as $ sniffCode ) {
469
458
$ sniffCode = trim ($ sniffCode );
470
459
$ enabledSniffs [$ sniffCode ] = true ;
460
+ $ ignoring ->set ($ sniffCode , false );
461
+ }
471
462
472
- // This new enabled sniff might remove previously disabled
473
- // sniffs if it is actually a standard or category of sniffs.
474
- foreach (array_keys ($ ignoring ) as $ ignoredSniffCode ) {
475
- if ($ ignoredSniffCode === $ sniffCode
476
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
477
- ) {
478
- unset($ ignoring [$ ignoredSniffCode ]);
479
- }
480
- }
481
-
482
- // This new enabled sniff might be able to clear up
483
- // previously enabled sniffs if it is actually a standard or
484
- // category of sniffs.
485
- if (isset ($ ignoring ['.except ' ]) === true ) {
486
- foreach (array_keys ($ ignoring ['.except ' ]) as $ ignoredSniffCode ) {
487
- if ($ ignoredSniffCode === $ sniffCode
488
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
489
- ) {
490
- unset($ ignoring ['.except ' ][$ ignoredSniffCode ]);
491
- }
492
- }
493
- }
494
- }//end foreach
495
-
496
- if (empty ($ ignoring ) === true ) {
463
+ if ($ ignoring ->isEmpty () === true ) {
497
464
$ ignoring = null ;
498
- } else {
499
- if (isset ($ ignoring ['.except ' ]) === true ) {
500
- $ ignoring ['.except ' ] += $ enabledSniffs ;
501
- } else {
502
- $ ignoring ['.except ' ] = $ enabledSniffs ;
503
- }
504
465
}
505
- }//end if
466
+ }
506
467
507
468
if ($ lineHasOtherContent === false ) {
508
469
// Completely ignore the comment line.
509
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
470
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
510
471
} else {
511
472
// The comment is on the same line as the code it is ignoring,
512
473
// so respect the new ignore rules.
@@ -523,31 +484,35 @@ private function createPositionMap()
523
484
524
485
$ additionalText = substr ($ commentText , 13 );
525
486
if (empty ($ additionalText ) === true ) {
526
- $ ignoreRules = ['.all ' => true ];
487
+ $ ignoreRules = ['.all ' => true ];
488
+ $ lineIgnoring = $ ignoreAll ;
527
489
} else {
528
490
$ parts = explode (', ' , $ additionalText );
491
+ if ($ ignoring === null ) {
492
+ $ lineIgnoring = IgnoreList::ignoringNone ();
493
+ } else {
494
+ $ lineIgnoring = clone $ ignoring ;
495
+ }
496
+
529
497
foreach ($ parts as $ sniffCode ) {
530
498
$ ignoreRules [trim ($ sniffCode )] = true ;
499
+ $ lineIgnoring ->set ($ sniffCode , true );
531
500
}
532
501
}
533
502
534
503
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_IGNORE ;
535
504
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_IGNORE ' ;
536
505
$ this ->tokens [$ i ]['sniffCodes ' ] = $ ignoreRules ;
537
506
538
- if ($ ignoring !== null ) {
539
- $ ignoreRules += $ ignoring ;
540
- }
541
-
542
507
if ($ lineHasOtherContent === false ) {
543
508
// Completely ignore the comment line, and set the following
544
509
// line to include the ignore rules we've set.
545
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
546
- $ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ ignoreRules ;
510
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
511
+ $ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ lineIgnoring ;
547
512
} else {
548
513
// The comment is on the same line as the code it is ignoring,
549
514
// so respect the ignore rules it set.
550
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreRules ;
515
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ lineIgnoring ;
551
516
}
552
517
}//end if
553
518
}//end if
0 commit comments