@@ -216,7 +216,7 @@ class PHP_CodeSniffer_File
216
216
*
217
217
* @var array()
218
218
*/
219
- private $ _ignoredLines = array ();
219
+ private static $ _ignoredLines = array ();
220
220
221
221
/**
222
222
* An array of sniffs that are being ignored.
@@ -447,7 +447,7 @@ public function start($contents=null)
447
447
448
448
// Reset the ignored lines because lines numbers may have changed
449
449
// if we are fixing this file.
450
- $ this -> _ignoredLines = array ();
450
+ self :: $ _ignoredLines = array ();
451
451
452
452
try {
453
453
$ this ->eolChar = self ::detectLineEndings ($ this ->_file , $ contents );
@@ -476,7 +476,6 @@ public function start($contents=null)
476
476
}
477
477
478
478
$ foundCode = false ;
479
- $ ignoring = false ;
480
479
$ listeners = $ this ->phpcs ->getSniffs ();
481
480
$ listenerIgnoreTo = array ();
482
481
$ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
@@ -490,23 +489,7 @@ public function start($contents=null)
490
489
|| ($ inTests === true && $ token ['code ' ] === T_INLINE_HTML )
491
490
) {
492
491
if (strpos ($ token ['content ' ], '@codingStandards ' ) !== false ) {
493
- if ($ ignoring === false
494
- && strpos ($ token ['content ' ], '@codingStandardsIgnoreStart ' ) !== false
495
- ) {
496
- $ ignoring = true ;
497
- } else if ($ ignoring === true
498
- && strpos ($ token ['content ' ], '@codingStandardsIgnoreEnd ' ) !== false
499
- ) {
500
- $ ignoring = false ;
501
- // Ignore this comment too.
502
- $ this ->_ignoredLines [$ token ['line ' ]] = true ;
503
- } else if ($ ignoring === false
504
- && strpos ($ token ['content ' ], '@codingStandardsIgnoreLine ' ) !== false
505
- ) {
506
- $ this ->_ignoredLines [($ token ['line ' ] + 1 )] = true ;
507
- // Ignore this comment too.
508
- $ this ->_ignoredLines [$ token ['line ' ]] = true ;
509
- } else if (strpos ($ token ['content ' ], '@codingStandardsIgnoreFile ' ) !== false ) {
492
+ if (strpos ($ token ['content ' ], '@codingStandardsIgnoreFile ' ) !== false ) {
510
493
// Ignoring the whole file, just a little late.
511
494
$ this ->_errors = array ();
512
495
$ this ->_warnings = array ();
@@ -525,10 +508,6 @@ public function start($contents=null)
525
508
}//end if
526
509
}//end if
527
510
528
- if ($ ignoring === true ) {
529
- $ this ->_ignoredLines [$ token ['line ' ]] = true ;
530
- }
531
-
532
511
if (PHP_CODESNIFFER_VERBOSITY > 2 ) {
533
512
$ type = $ token ['type ' ];
534
513
$ content = PHP_CodeSniffer::prepareForOutput ($ token ['content ' ]);
@@ -606,33 +585,6 @@ public function start($contents=null)
606
585
}//end foreach
607
586
}//end foreach
608
587
609
- // Remove errors and warnings for ignored lines.
610
- foreach ($ this ->_ignoredLines as $ line => $ ignore ) {
611
- if (isset ($ this ->_errors [$ line ]) === true ) {
612
- if ($ this ->_recordErrors === false ) {
613
- $ this ->_errorCount -= $ this ->_errors [$ line ];
614
- } else {
615
- foreach ($ this ->_errors [$ line ] as $ col => $ errors ) {
616
- $ this ->_errorCount -= count ($ errors );
617
- }
618
- }
619
-
620
- unset($ this ->_errors [$ line ]);
621
- }
622
-
623
- if (isset ($ this ->_warnings [$ line ]) === true ) {
624
- if ($ this ->_recordErrors === false ) {
625
- $ this ->_errorCount -= $ this ->_warnings [$ line ];
626
- } else {
627
- foreach ($ this ->_warnings [$ line ] as $ col => $ warnings ) {
628
- $ this ->_warningCount -= count ($ warnings );
629
- }
630
- }
631
-
632
- unset($ this ->_warnings [$ line ]);
633
- }
634
- }//end foreach
635
-
636
588
if ($ this ->_recordErrors === false ) {
637
589
$ this ->_errors = array ();
638
590
$ this ->_warnings = array ();
@@ -1028,6 +980,10 @@ public function addFixableWarning(
1028
980
*/
1029
981
private function _addError ($ error , $ line , $ column , $ code , $ data , $ severity , $ fixable )
1030
982
{
983
+ if (isset (self ::$ _ignoredLines [$ line ]) === true ) {
984
+ return false ;
985
+ }
986
+
1031
987
// Work out which sniff generated the error.
1032
988
if (substr ($ code , 0 , 9 ) === 'Internal. ' ) {
1033
989
// Any internal message.
@@ -1172,6 +1128,10 @@ private function _addError($error, $line, $column, $code, $data, $severity, $fix
1172
1128
*/
1173
1129
private function _addWarning ($ warning , $ line , $ column , $ code , $ data , $ severity , $ fixable )
1174
1130
{
1131
+ if (isset (self ::$ _ignoredLines [$ line ]) === true ) {
1132
+ return false ;
1133
+ }
1134
+
1175
1135
// Work out which sniff generated the warning.
1176
1136
if (substr ($ code , 0 , 9 ) === 'Internal. ' ) {
1177
1137
// Any internal message.
@@ -1385,7 +1345,7 @@ public function getFixableCount()
1385
1345
*/
1386
1346
public function getIgnoredLines ()
1387
1347
{
1388
- return $ this -> _ignoredLines ;
1348
+ return self :: $ _ignoredLines ;
1389
1349
1390
1350
}//end getIgnoredLines()
1391
1351
@@ -1512,6 +1472,8 @@ private static function _createPositionMap(&$tokens, $tokenizer, $eolChar, $enco
1512
1472
$ lineNumber = 1 ;
1513
1473
$ eolLen = (strlen ($ eolChar ) * -1 );
1514
1474
$ tokenizerType = get_class ($ tokenizer );
1475
+ $ ignoring = false ;
1476
+ $ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
1515
1477
1516
1478
$ checkEncoding = false ;
1517
1479
if ($ encoding !== 'iso-8859-1 ' && function_exists ('iconv_strlen ' ) === true ) {
@@ -1639,6 +1601,35 @@ private static function _createPositionMap(&$tokens, $tokenizer, $eolChar, $enco
1639
1601
// Newline chars are not counted in the token length.
1640
1602
$ tokens [$ i ]['length ' ] += $ eolLen ;
1641
1603
}
1604
+
1605
+ if ($ tokens [$ i ]['code ' ] === T_COMMENT
1606
+ || $ tokens [$ i ]['code ' ] === T_DOC_COMMENT
1607
+ || ($ inTests === true && $ tokens [$ i ]['code ' ] === T_INLINE_HTML )
1608
+ ) {
1609
+ if (strpos ($ tokens [$ i ]['content ' ], '@codingStandards ' ) !== false ) {
1610
+ if ($ ignoring === false
1611
+ && strpos ($ tokens [$ i ]['content ' ], '@codingStandardsIgnoreStart ' ) !== false
1612
+ ) {
1613
+ $ ignoring = true ;
1614
+ } else if ($ ignoring === true
1615
+ && strpos ($ tokens [$ i ]['content ' ], '@codingStandardsIgnoreEnd ' ) !== false
1616
+ ) {
1617
+ $ ignoring = false ;
1618
+ // Ignore this comment too.
1619
+ self ::$ _ignoredLines [$ tokens [$ i ]['line ' ]] = true ;
1620
+ } else if ($ ignoring === false
1621
+ && strpos ($ tokens [$ i ]['content ' ], '@codingStandardsIgnoreLine ' ) !== false
1622
+ ) {
1623
+ self ::$ _ignoredLines [($ tokens [$ i ]['line ' ] + 1 )] = true ;
1624
+ // Ignore this comment too.
1625
+ self ::$ _ignoredLines [$ tokens [$ i ]['line ' ]] = true ;
1626
+ }
1627
+ }
1628
+ }//end if
1629
+
1630
+ if ($ ignoring === true ) {
1631
+ self ::$ _ignoredLines [$ tokens [$ i ]['line ' ]] = true ;
1632
+ }
1642
1633
}//end for
1643
1634
1644
1635
}//end _createPositionMap()
0 commit comments