11
11
12
12
use PHP_CodeSniffer \Exceptions \TokenizerException ;
13
13
use PHP_CodeSniffer \Util \Common ;
14
+ use PHP_CodeSniffer \Util \IgnoreList ;
14
15
use PHP_CodeSniffer \Util \Tokens ;
15
16
use PHP_CodeSniffer \Util \Writers \StatusWriter ;
16
17
@@ -175,6 +176,7 @@ private function createPositionMap()
175
176
$ lineNumber = 1 ;
176
177
$ eolLen = strlen ($ this ->eolChar );
177
178
$ ignoring = null ;
179
+ $ ignoreAll = IgnoreList::ignoringAll ();
178
180
$ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
179
181
180
182
$ checkEncoding = false ;
@@ -349,7 +351,7 @@ private function createPositionMap()
349
351
if (substr ($ commentTextLower , 0 , 9 ) === 'phpcs:set ' ) {
350
352
// Ignore standards for complete lines that change sniff settings.
351
353
if ($ lineHasOtherTokens === false ) {
352
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
354
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
353
355
}
354
356
355
357
// Need to maintain case here, to get the correct sniff code.
@@ -372,42 +374,28 @@ private function createPositionMap()
372
374
} else if (substr ($ commentTextLower , 0 , 13 ) === 'phpcs:disable ' ) {
373
375
if ($ lineHasOtherContent === false ) {
374
376
// Completely ignore the comment line.
375
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = ['.all ' => true ];
376
- }
377
-
378
- if ($ ignoring === null ) {
379
- $ ignoring = [];
377
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
380
378
}
381
379
382
380
$ disabledSniffs = [];
383
381
384
382
$ additionalText = substr ($ commentText , 14 );
385
383
if (empty ($ additionalText ) === true ) {
386
- $ ignoring = [ ' .all ' => true ] ;
384
+ $ ignoring = $ ignoreAll ;
387
385
} else {
386
+ if ($ ignoring === null ) {
387
+ $ ignoring = IgnoreList::ignoringNone ();
388
+ } else {
389
+ $ ignoring = clone $ ignoring ;
390
+ }
391
+
388
392
$ parts = explode (', ' , $ additionalText );
389
393
foreach ($ parts as $ sniffCode ) {
390
394
$ sniffCode = trim ($ sniffCode );
391
395
$ disabledSniffs [$ sniffCode ] = true ;
392
- $ ignoring [$ sniffCode ] = true ;
393
-
394
- // This newly disabled sniff might be disabling an existing
395
- // enabled exception that we are tracking.
396
- if (isset ($ ignoring ['.except ' ]) === true ) {
397
- foreach (array_keys ($ ignoring ['.except ' ]) as $ ignoredSniffCode ) {
398
- if ($ ignoredSniffCode === $ sniffCode
399
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
400
- ) {
401
- unset($ ignoring ['.except ' ][$ ignoredSniffCode ]);
402
- }
403
- }
404
-
405
- if (empty ($ ignoring ['.except ' ]) === true ) {
406
- unset($ ignoring ['.except ' ]);
407
- }
408
- }
409
- }//end foreach
410
- }//end if
396
+ $ ignoring ->set ($ sniffCode , true );
397
+ }
398
+ }
411
399
412
400
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_DISABLE ;
413
401
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_DISABLE ' ;
@@ -420,49 +408,22 @@ private function createPositionMap()
420
408
if (empty ($ additionalText ) === true ) {
421
409
$ ignoring = null ;
422
410
} else {
423
- $ parts = explode (', ' , $ additionalText );
411
+ $ ignoring = clone $ ignoring ;
412
+ $ parts = explode (', ' , $ additionalText );
424
413
foreach ($ parts as $ sniffCode ) {
425
414
$ sniffCode = trim ($ sniffCode );
426
415
$ enabledSniffs [$ sniffCode ] = true ;
416
+ $ ignoring ->set ($ sniffCode , false );
417
+ }
427
418
428
- // This new enabled sniff might remove previously disabled
429
- // sniffs if it is actually a standard or category of sniffs.
430
- foreach (array_keys ($ ignoring ) as $ ignoredSniffCode ) {
431
- if ($ ignoredSniffCode === $ sniffCode
432
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
433
- ) {
434
- unset($ ignoring [$ ignoredSniffCode ]);
435
- }
436
- }
437
-
438
- // This new enabled sniff might be able to clear up
439
- // previously enabled sniffs if it is actually a standard or
440
- // category of sniffs.
441
- if (isset ($ ignoring ['.except ' ]) === true ) {
442
- foreach (array_keys ($ ignoring ['.except ' ]) as $ ignoredSniffCode ) {
443
- if ($ ignoredSniffCode === $ sniffCode
444
- || strpos ($ ignoredSniffCode , $ sniffCode .'. ' ) === 0
445
- ) {
446
- unset($ ignoring ['.except ' ][$ ignoredSniffCode ]);
447
- }
448
- }
449
- }
450
- }//end foreach
451
-
452
- if (empty ($ ignoring ) === true ) {
419
+ if ($ ignoring ->isEmpty () === true ) {
453
420
$ ignoring = null ;
454
- } else {
455
- if (isset ($ ignoring ['.except ' ]) === true ) {
456
- $ ignoring ['.except ' ] += $ enabledSniffs ;
457
- } else {
458
- $ ignoring ['.except ' ] = $ enabledSniffs ;
459
- }
460
421
}
461
- }//end if
422
+ }
462
423
463
424
if ($ lineHasOtherContent === false ) {
464
425
// Completely ignore the comment line.
465
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
426
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
466
427
} else {
467
428
// The comment is on the same line as the code it is ignoring,
468
429
// so respect the new ignore rules.
@@ -479,31 +440,35 @@ private function createPositionMap()
479
440
480
441
$ additionalText = substr ($ commentText , 13 );
481
442
if (empty ($ additionalText ) === true ) {
482
- $ ignoreRules = ['.all ' => true ];
443
+ $ ignoreRules = ['.all ' => true ];
444
+ $ lineIgnoring = $ ignoreAll ;
483
445
} else {
484
446
$ parts = explode (', ' , $ additionalText );
447
+ if ($ ignoring === null ) {
448
+ $ lineIgnoring = IgnoreList::ignoringNone ();
449
+ } else {
450
+ $ lineIgnoring = clone $ ignoring ;
451
+ }
452
+
485
453
foreach ($ parts as $ sniffCode ) {
486
454
$ ignoreRules [trim ($ sniffCode )] = true ;
455
+ $ lineIgnoring ->set ($ sniffCode , true );
487
456
}
488
457
}
489
458
490
459
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_IGNORE ;
491
460
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_IGNORE ' ;
492
461
$ this ->tokens [$ i ]['sniffCodes ' ] = $ ignoreRules ;
493
462
494
- if ($ ignoring !== null ) {
495
- $ ignoreRules += $ ignoring ;
496
- }
497
-
498
463
if ($ lineHasOtherContent === false ) {
499
464
// Completely ignore the comment line, and set the following
500
465
// line to include the ignore rules we've set.
501
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
502
- $ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ ignoreRules ;
466
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
467
+ $ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ lineIgnoring ;
503
468
} else {
504
469
// The comment is on the same line as the code it is ignoring,
505
470
// so respect the ignore rules it set.
506
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreRules ;
471
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ lineIgnoring ;
507
472
}
508
473
}//end if
509
474
}//end if
0 commit comments