Skip to content

Commit

Permalink
Don't require return tag if returning void
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Nov 10, 2018
1 parent e332b88 commit 4118f46
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions Chadicus/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,9 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
// If the return type is void, make sure there is
// no return statement in the function.
if ($returnType === 'void') {
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
$endToken = $tokens[$stackPtr]['scope_closer'];
for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
if ($tokens[$returnToken]['code'] === T_CLOSURE
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
) {
$returnToken = $tokens[$returnToken]['scope_closer'];
continue;
}

if ($tokens[$returnToken]['code'] === T_RETURN
|| $tokens[$returnToken]['code'] === T_YIELD
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
) {
break;
}
}

if ($returnToken !== $endToken) {
// If the function is not returning anything, just
// exiting, then there is no problem.
$semicolon = $phpcsFile->findNext(T_WHITESPACE, ($returnToken + 1), null, true);
if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
$error = 'Function return type is void, but function contains return statement';
$phpcsFile->addError($error, $return, 'InvalidReturnVoid');
}
}
if (!$this->isReturnVoid($phpcsFile, $tokens, $stackPtr)) {
$error = 'Function return type is void, but function contains return statement';
$phpcsFile->addError($error, $return, 'InvalidReturnVoid');
}//end if
} else if ($returnType !== 'mixed' && in_array('void', $typeNames, true) === false) {
// If return type is not void, there needs to be a return statement
Expand All @@ -163,12 +139,51 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
}//end if
}//end if
} else {
$error = 'Missing @return tag in function comment';
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
if (!$this->isReturnVoid($phpcsFile, $tokens, $stackPtr)) {
$error = 'Missing @return tag in function comment';
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
}
}//end if

}//end processReturn()

private function isReturnVoid($phpcsFile, $tokens, $stackPtr) : bool
{
if (isset($tokens[$stackPtr]['scope_closer']) !== true) {
return true;
}

$endToken = $tokens[$stackPtr]['scope_closer'];
for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
if ($tokens[$returnToken]['code'] === T_CLOSURE
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
) {
$returnToken = $tokens[$returnToken]['scope_closer'];
continue;
}

if ($tokens[$returnToken]['code'] === T_RETURN
|| $tokens[$returnToken]['code'] === T_YIELD
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
) {
break;
}
}

if ($returnToken === $endToken) {
return true;
}

// If the function is not returning anything, just
// exiting, then there is no problem.
$semicolon = $phpcsFile->findNext(T_WHITESPACE, ($returnToken + 1), null, true);
if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
return false;
}

return true;
}

/**
* Process any throw tags that this function comment has.
*
Expand Down

0 comments on commit 4118f46

Please sign in to comment.