diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 548565f160..1d0745b98b 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -104,8 +104,9 @@ public function process(File $phpcsFile, $stackPtr) // and the opening parenthesis. // Unfinished closures are tokenized as T_FUNCTION however, and can be excluded // by checking for the scope_opener. + $methodProps = $phpcsFile->getMethodProperties($stackPtr); if ($tokens[$stackPtr]['code'] === T_FUNCTION - && (isset($tokens[$stackPtr]['scope_opener']) === true || $phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) + && (isset($tokens[$stackPtr]['scope_opener']) === true || $methodProps['has_body'] === false) ) { if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { $spaces = 'newline'; @@ -125,25 +126,27 @@ public function process(File $phpcsFile, $stackPtr) } // Must be no space before semicolon in abstract/interface methods. - if ($phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) { + if ($methodProps['has_body'] === false) { $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket); - if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { - $spaces = 'newline'; - } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { - $spaces = $tokens[($end - 1)]['length']; - } else { - $spaces = 0; - } + if ($end !== false) { + if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { + $spaces = 'newline'; + } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { + $spaces = $tokens[($end - 1)]['length']; + } else { + $spaces = 0; + } - if ($spaces !== 0) { - $error = 'Expected 0 spaces before semicolon; %s found'; - $data = [$spaces]; - $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($end - 1), ''); + if ($spaces !== 0) { + $error = 'Expected 0 spaces before semicolon; %s found'; + $data = [$spaces]; + $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($end - 1), ''); + } } } - } + }//end if }//end if // Must be one space before and after USE keyword for closures. diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc rename to src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed rename to src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.2.inc b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..719f703b0d --- /dev/null +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.2.inc @@ -0,0 +1,7 @@ + 1, 4 => 1, 5 => 1, @@ -108,8 +109,9 @@ public function getErrorList($testFile='') 483 => 1, 490 => 2, ]; - } else { - $errors = [ + + case 'FunctionDeclarationUnitTest.js': + return [ 3 => 1, 4 => 1, 5 => 1, @@ -121,9 +123,10 @@ public function getErrorList($testFile='') 41 => 1, 48 => 1, ]; - }//end if - return $errors; + default: + return []; + }//end switch }//end getErrorList()