@@ -239,7 +239,9 @@ public final function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
239
239
/**
240
240
* Processes the pattern and varifies the code at $stackPtr.
241
241
*
242
- * @param array $patternInfo The info for the pattern to process.
242
+ * @param array $patternInfo Information about the pattern used for
243
+ * checking, which includes are parsed
244
+ * otken representation of the pattern.
243
245
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the
244
246
* token occured.
245
247
* @param int $stackPtr The postion in the tokens stack where
@@ -337,23 +339,22 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
337
339
$ stackPtr = $ origStackPtr ;
338
340
339
341
for ($ i = $ patternInfo ['listen_pos ' ]; $ i < count ($ pattern ); $ i ++) {
342
+
340
343
if ($ pattern [$ i ]['type ' ] === 'token ' ) {
341
344
342
345
if ($ pattern [$ i ]['token ' ] === T_WHITESPACE ) {
343
346
344
- // If we are ignoring comments, check to see if this current
345
- // token is a comment. If so skip it.
346
- if ( in_array ( $ tokens [ $ stackPtr ][ ' code ' ], PHP_CodeSniffer_Tokens:: $ commentTokens ) === true ) {
347
- if ($ this -> _ignoreComments === true ) {
347
+ if ( $ this -> _ignoreComments === true ) {
348
+ // If we are ignoring comments, check to see if this current
349
+ // token is a comment. If so skip it.
350
+ if (in_array ( $ tokens [ $ stackPtr ][ ' code ' ], PHP_CodeSniffer_Tokens:: $ commentTokens ) === true ) {
348
351
continue ;
349
352
}
350
- }
351
-
352
- // If the next token is a comment, the we need to skip the
353
- // current token as we should allow a space before a
354
- // comment for readability.
355
- if (in_array ($ tokens [$ stackPtr + 1 ]['code ' ], PHP_CodeSniffer_Tokens::$ commentTokens ) === true ) {
356
- if ($ this ->_ignoreComments === true ) {
353
+
354
+ // If the next token is a comment, the we need to skip the
355
+ // current token as we should allow a space before a
356
+ // comment for readability.
357
+ if (in_array ($ tokens [$ stackPtr + 1 ]['code ' ], PHP_CodeSniffer_Tokens::$ commentTokens ) === true ) {
357
358
continue ;
358
359
}
359
360
}
@@ -374,9 +375,19 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
374
375
$ found .= $ tokenContent ;
375
376
}
376
377
377
- if ($ tokenContent !== $ pattern [$ i ]['value ' ]) {
378
- $ hasError = true ;
378
+ if (isset ($ pattern [$ i + 1 ]) === true && $ pattern [$ i + 1 ]['type ' ] === 'skip ' ) {
379
+ // The next token is a skip token, so we just need to make
380
+ // sure the whitespace we found has *at least* the
381
+ // whitespace required.
382
+ if (strpos ($ tokenContent , $ pattern [$ i ]['value ' ]) !== 0 ) {
383
+ $ hasError = true ;
384
+ }
385
+ } else {
386
+ if ($ tokenContent !== $ pattern [$ i ]['value ' ]) {
387
+ $ hasError = true ;
388
+ }
379
389
}
390
+
380
391
} else {
381
392
382
393
$ next = $ phpcsFile ->findNext ($ ignoreTokens , $ stackPtr , null , true );
@@ -406,7 +417,7 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
406
417
407
418
} else if ($ pattern [$ i ]['type ' ] === 'skip ' ) {
408
419
// Find the previous opener.
409
- $ next = $ phpcsFile ->findNext ( $ ignoreTokens , $ stackPtr , null , true );
420
+ $ next = $ phpcsFile ->findPrevious (PHP_CodeSniffer_Tokens:: $ blockOpeners , $ stackPtr , null );
410
421
if ($ next === false || isset ($ tokens [$ next ][$ pattern [$ i ]['to ' ]]) === false ) {
411
422
// If there was not opener, then we must
412
423
// be using the wrong pattern.
@@ -417,7 +428,7 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
417
428
418
429
// Skip to the closing token.
419
430
$ stackPtr = ($ tokens [$ next ][$ pattern [$ i ]['to ' ]] + 1 );
420
- }
431
+ }//end if
421
432
}//end for
422
433
423
434
if ($ hasError === true ) {
@@ -521,7 +532,7 @@ private function _parse($pattern)
521
532
// It's a skip pattern. The skip pattern requires the
522
533
// content of the token in the "from" position and the token
523
534
// to skip to.
524
- $ skipPattern = $ this ->_createSkipPattern ($ pattern{ $ i - 1 } );
535
+ $ skipPattern = $ this ->_createSkipPattern ($ pattern, ( $ i - 1 ) );
525
536
$ lastToken = ($ i - $ firstToken );
526
537
$ i = ($ i + 4 );
527
538
$ skipPatternCount ++;
@@ -565,30 +576,37 @@ private function _parse($pattern)
565
576
/**
566
577
* Creates a skip pattern.
567
578
*
568
- * @param string $from The token content that the skip pattern starts from.
579
+ * @param string $pattern The pattern being parsed.
580
+ * @param string $from The token content that the skip pattern starts from.
569
581
*
570
582
* @return array The pattern step.
571
583
* @see _createTokenPattern()
572
584
* @see _parse()
573
585
*/
574
- private function _createSkipPattern ($ from )
586
+ private function _createSkipPattern ($ pattern , $ from )
575
587
{
576
588
$ skip = array (
577
589
'type ' => 'skip ' ,
578
590
);
579
591
580
- switch ($ from ) {
581
- case '( ' :
582
- $ skip ['to ' ] = 'parenthesis_closer ' ;
583
- break ;
584
- case '{ ' :
585
- $ skip ['to ' ] = 'scope_closer ' ;
586
- break ;
587
- default :
588
- // Something else.
589
- break ;
592
+ for ($ from ; $ from >=0 ; $ from --) {
593
+ switch ($ pattern [$ from ]) {
594
+ case '( ' :
595
+ $ skip ['to ' ] = 'parenthesis_closer ' ;
596
+ break ;
597
+ case '{ ' :
598
+ $ skip ['to ' ] = 'scope_closer ' ;
599
+ break ;
600
+ }
601
+
602
+ if (isset ($ skip ['to ' ]) === true ) {
603
+ break ;
604
+ }
590
605
}
591
606
607
+ if (isset ($ skip ['to ' ]) === false ) {
608
+ $ skip ['to ' ] = 'unknown ' ;
609
+ }
592
610
return $ skip ;
593
611
594
612
}//end _createSkipPattern()
0 commit comments