@@ -216,7 +216,7 @@ class PHP_CodeSniffer_File
216216 *
217217 * @var array()
218218 */
219- private $ _ignoredLines = array ();
219+ private static $ _ignoredLines = array ();
220220
221221 /**
222222 * An array of sniffs that are being ignored.
@@ -447,7 +447,7 @@ public function start($contents=null)
447447
448448 // Reset the ignored lines because lines numbers may have changed
449449 // if we are fixing this file.
450- $ this -> _ignoredLines = array ();
450+ self :: $ _ignoredLines = array ();
451451
452452 try {
453453 $ this ->eolChar = self ::detectLineEndings ($ this ->_file , $ contents );
@@ -476,7 +476,6 @@ public function start($contents=null)
476476 }
477477
478478 $ foundCode = false ;
479- $ ignoring = false ;
480479 $ listeners = $ this ->phpcs ->getSniffs ();
481480 $ listenerIgnoreTo = array ();
482481 $ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
@@ -490,23 +489,7 @@ public function start($contents=null)
490489 || ($ inTests === true && $ token ['code ' ] === T_INLINE_HTML )
491490 ) {
492491 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 ) {
510493 // Ignoring the whole file, just a little late.
511494 $ this ->_errors = array ();
512495 $ this ->_warnings = array ();
@@ -525,10 +508,6 @@ public function start($contents=null)
525508 }//end if
526509 }//end if
527510
528- if ($ ignoring === true ) {
529- $ this ->_ignoredLines [$ token ['line ' ]] = true ;
530- }
531-
532511 if (PHP_CODESNIFFER_VERBOSITY > 2 ) {
533512 $ type = $ token ['type ' ];
534513 $ content = PHP_CodeSniffer::prepareForOutput ($ token ['content ' ]);
@@ -606,33 +585,6 @@ public function start($contents=null)
606585 }//end foreach
607586 }//end foreach
608587
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-
636588 if ($ this ->_recordErrors === false ) {
637589 $ this ->_errors = array ();
638590 $ this ->_warnings = array ();
@@ -1028,6 +980,10 @@ public function addFixableWarning(
1028980 */
1029981 private function _addError ($ error , $ line , $ column , $ code , $ data , $ severity , $ fixable )
1030982 {
983+ if (isset (self ::$ _ignoredLines [$ line ]) === true ) {
984+ return false ;
985+ }
986+
1031987 // Work out which sniff generated the error.
1032988 if (substr ($ code , 0 , 9 ) === 'Internal. ' ) {
1033989 // Any internal message.
@@ -1172,6 +1128,10 @@ private function _addError($error, $line, $column, $code, $data, $severity, $fix
11721128 */
11731129 private function _addWarning ($ warning , $ line , $ column , $ code , $ data , $ severity , $ fixable )
11741130 {
1131+ if (isset (self ::$ _ignoredLines [$ line ]) === true ) {
1132+ return false ;
1133+ }
1134+
11751135 // Work out which sniff generated the warning.
11761136 if (substr ($ code , 0 , 9 ) === 'Internal. ' ) {
11771137 // Any internal message.
@@ -1385,7 +1345,7 @@ public function getFixableCount()
13851345 */
13861346 public function getIgnoredLines ()
13871347 {
1388- return $ this -> _ignoredLines ;
1348+ return self :: $ _ignoredLines ;
13891349
13901350 }//end getIgnoredLines()
13911351
@@ -1512,6 +1472,8 @@ private static function _createPositionMap(&$tokens, $tokenizer, $eolChar, $enco
15121472 $ lineNumber = 1 ;
15131473 $ eolLen = (strlen ($ eolChar ) * -1 );
15141474 $ tokenizerType = get_class ($ tokenizer );
1475+ $ ignoring = false ;
1476+ $ inTests = defined ('PHP_CODESNIFFER_IN_TESTS ' );
15151477
15161478 $ checkEncoding = false ;
15171479 if ($ encoding !== 'iso-8859-1 ' && function_exists ('iconv_strlen ' ) === true ) {
@@ -1639,6 +1601,35 @@ private static function _createPositionMap(&$tokens, $tokenizer, $eolChar, $enco
16391601 // Newline chars are not counted in the token length.
16401602 $ tokens [$ i ]['length ' ] += $ eolLen ;
16411603 }
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+ }
16421633 }//end for
16431634
16441635 }//end _createPositionMap()
0 commit comments