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::getInstanceIgnoringAll ();
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,24 @@ 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
+ $ ignoring = IgnoreList::getNewInstanceFrom ($ ignoring );
387
+
388
388
$ parts = explode (', ' , $ additionalText );
389
389
foreach ($ parts as $ sniffCode ) {
390
390
$ sniffCode = trim ($ sniffCode );
391
391
$ 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
392
+ $ ignoring ->set ($ sniffCode , true );
393
+ }
394
+ }
411
395
412
396
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_DISABLE ;
413
397
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_DISABLE ' ;
@@ -420,49 +404,22 @@ private function createPositionMap()
420
404
if (empty ($ additionalText ) === true ) {
421
405
$ ignoring = null ;
422
406
} else {
423
- $ parts = explode (', ' , $ additionalText );
407
+ $ ignoring = IgnoreList::getNewInstanceFrom ($ ignoring );
408
+ $ parts = explode (', ' , $ additionalText );
424
409
foreach ($ parts as $ sniffCode ) {
425
410
$ sniffCode = trim ($ sniffCode );
426
411
$ enabledSniffs [$ sniffCode ] = true ;
412
+ $ ignoring ->set ($ sniffCode , false );
413
+ }
427
414
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 ) {
415
+ if ($ ignoring ->ignoresNothing () === true ) {
453
416
$ ignoring = null ;
454
- } else {
455
- if (isset ($ ignoring ['.except ' ]) === true ) {
456
- $ ignoring ['.except ' ] += $ enabledSniffs ;
457
- } else {
458
- $ ignoring ['.except ' ] = $ enabledSniffs ;
459
- }
460
417
}
461
- }//end if
418
+ }
462
419
463
420
if ($ lineHasOtherContent === false ) {
464
421
// Completely ignore the comment line.
465
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = [ ' .all ' => true ] ;
422
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
466
423
} else {
467
424
// The comment is on the same line as the code it is ignoring,
468
425
// so respect the new ignore rules.
@@ -479,31 +436,31 @@ private function createPositionMap()
479
436
480
437
$ additionalText = substr ($ commentText , 13 );
481
438
if (empty ($ additionalText ) === true ) {
482
- $ ignoreRules = ['.all ' => true ];
439
+ $ ignoreRules = ['.all ' => true ];
440
+ $ lineIgnoring = $ ignoreAll ;
483
441
} else {
484
- $ parts = explode (', ' , $ additionalText );
442
+ $ parts = explode (', ' , $ additionalText );
443
+ $ lineIgnoring = IgnoreList::getNewInstanceFrom ($ ignoring );
444
+
485
445
foreach ($ parts as $ sniffCode ) {
486
446
$ ignoreRules [trim ($ sniffCode )] = true ;
447
+ $ lineIgnoring ->set ($ sniffCode , true );
487
448
}
488
449
}
489
450
490
451
$ this ->tokens [$ i ]['code ' ] = T_PHPCS_IGNORE ;
491
452
$ this ->tokens [$ i ]['type ' ] = 'T_PHPCS_IGNORE ' ;
492
453
$ this ->tokens [$ i ]['sniffCodes ' ] = $ ignoreRules ;
493
454
494
- if ($ ignoring !== null ) {
495
- $ ignoreRules += $ ignoring ;
496
- }
497
-
498
455
if ($ lineHasOtherContent === false ) {
499
456
// Completely ignore the comment line, and set the following
500
457
// 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 ;
458
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreAll ;
459
+ $ this ->ignoredLines [($ this ->tokens [$ i ]['line ' ] + 1 )] = $ lineIgnoring ;
503
460
} else {
504
461
// The comment is on the same line as the code it is ignoring,
505
462
// so respect the ignore rules it set.
506
- $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ ignoreRules ;
463
+ $ this ->ignoredLines [$ this ->tokens [$ i ]['line ' ]] = $ lineIgnoring ;
507
464
}
508
465
}//end if
509
466
}//end if
0 commit comments