diff --git a/src/Files/File.php b/src/Files/File.php index eb56ef64b4..67dc75f540 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1463,6 +1463,7 @@ public function getMethodParameters($stackPtr) * 'is_abstract' => false, // true if the abstract keyword was found. * 'is_final' => false, // true if the final keyword was found. * 'is_static' => false, // true if the static keyword was found. + * 'has_body' => false, // true if the method has a body * ); * * @@ -1541,6 +1542,7 @@ public function getMethodProperties($stackPtr) $returnType = ''; $returnTypeToken = false; $nullableReturnType = false; + $hasBody = true; if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) { $scopeOpener = null; @@ -1576,6 +1578,9 @@ public function getMethodProperties($stackPtr) $returnType .= $this->tokens[$i]['content']; } } + + $end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']); + $hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET; }//end if if ($returnType !== '' && $nullableReturnType === true) { @@ -1591,6 +1596,7 @@ public function getMethodProperties($stackPtr) 'is_abstract' => $isAbstract, 'is_final' => $isFinal, 'is_static' => $isStatic, + 'has_body' => $hasBody, ]; }//end getMethodProperties() diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 2607422837..8e7c30ed86 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -105,7 +105,7 @@ public function process(File $phpcsFile, $stackPtr) // Unfinished closures are tokenized as T_FUNCTION however, and can be excluded // by checking for the scope_opener. if ($tokens[$stackPtr]['code'] === T_FUNCTION - && (isset($tokens[$stackPtr]['scope_opener']) === true || $tokens[$stackPtr]['level'] === 1) + && (isset($tokens[$stackPtr]['scope_opener']) === true || $phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) ) { if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { $spaces = 'newline'; @@ -125,8 +125,8 @@ public function process(File $phpcsFile, $stackPtr) } // Must be no space before semicolon in abstract/interface methods. - $end = $phpcsFile->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $closeBracket); - if ($tokens[$end]['code'] === T_SEMICOLON) { + if ($phpcsFile->getMethodProperties($stackPtr)['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) { diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed index fd2763a56a..b993e3f079 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed @@ -311,4 +311,4 @@ if(true) { abstract function baz(); } -} \ No newline at end of file +}