Skip to content

Commit

Permalink
Determine if the method has a body
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Oct 13, 2018
1 parent eeb758a commit 2410463
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* );
* </code>
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -1591,6 +1596,7 @@ public function getMethodProperties($stackPtr)
'is_abstract' => $isAbstract,
'is_final' => $isFinal,
'is_static' => $isStatic,
'has_body' => $hasBody,
];

}//end getMethodProperties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,4 @@ if(true) {

abstract function baz();
}
}
}

0 comments on commit 2410463

Please sign in to comment.