Skip to content

Commit bce506d

Browse files
committed
There were all sorts of problems with scope patterns that had skips that were not directly after a block opener. Fixed these errors so the patterns can be like: try {\n...}
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@225353 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 5cd30e0 commit bce506d

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

CodeSniffer/Standards/AbstractPatternSniff.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ public final function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
239239
/**
240240
* Processes the pattern and varifies the code at $stackPtr.
241241
*
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.
243245
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the
244246
* token occured.
245247
* @param int $stackPtr The postion in the tokens stack where
@@ -337,23 +339,22 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
337339
$stackPtr = $origStackPtr;
338340

339341
for ($i = $patternInfo['listen_pos']; $i < count($pattern); $i++) {
342+
340343
if ($pattern[$i]['type'] === 'token') {
341344

342345
if ($pattern[$i]['token'] === T_WHITESPACE) {
343346

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) {
348351
continue;
349352
}
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) {
357358
continue;
358359
}
359360
}
@@ -374,9 +375,19 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
374375
$found .= $tokenContent;
375376
}
376377

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+
}
379389
}
390+
380391
} else {
381392

382393
$next = $phpcsFile->findNext($ignoreTokens, $stackPtr, null, true);
@@ -406,7 +417,7 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
406417

407418
} else if ($pattern[$i]['type'] === 'skip') {
408419
// Find the previous opener.
409-
$next = $phpcsFile->findNext($ignoreTokens, $stackPtr, null, true);
420+
$next = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$blockOpeners, $stackPtr, null);
410421
if ($next === false || isset($tokens[$next][$pattern[$i]['to']]) === false) {
411422
// If there was not opener, then we must
412423
// be using the wrong pattern.
@@ -417,7 +428,7 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile,
417428

418429
// Skip to the closing token.
419430
$stackPtr = ($tokens[$next][$pattern[$i]['to']] + 1);
420-
}
431+
}//end if
421432
}//end for
422433

423434
if ($hasError === true) {
@@ -521,7 +532,7 @@ private function _parse($pattern)
521532
// It's a skip pattern. The skip pattern requires the
522533
// content of the token in the "from" position and the token
523534
// to skip to.
524-
$skipPattern = $this->_createSkipPattern($pattern{$i - 1});
535+
$skipPattern = $this->_createSkipPattern($pattern, ($i - 1));
525536
$lastToken = ($i - $firstToken);
526537
$i = ($i + 4);
527538
$skipPatternCount++;
@@ -565,30 +576,37 @@ private function _parse($pattern)
565576
/**
566577
* Creates a skip pattern.
567578
*
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.
569581
*
570582
* @return array The pattern step.
571583
* @see _createTokenPattern()
572584
* @see _parse()
573585
*/
574-
private function _createSkipPattern($from)
586+
private function _createSkipPattern($pattern, $from)
575587
{
576588
$skip = array(
577589
'type' => 'skip',
578590
);
579591

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+
}
590605
}
591606

607+
if (isset($skip['to']) === false) {
608+
$skip['to'] = 'unknown';
609+
}
592610
return $skip;
593611

594612
}//end _createSkipPattern()

0 commit comments

Comments
 (0)