Skip to content

Commit 4118f46

Browse files
committed
Don't require return tag if returning void
1 parent e332b88 commit 4118f46

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

Chadicus/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,9 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
115115
// If the return type is void, make sure there is
116116
// no return statement in the function.
117117
if ($returnType === 'void') {
118-
if (isset($tokens[$stackPtr]['scope_closer']) === true) {
119-
$endToken = $tokens[$stackPtr]['scope_closer'];
120-
for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
121-
if ($tokens[$returnToken]['code'] === T_CLOSURE
122-
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
123-
) {
124-
$returnToken = $tokens[$returnToken]['scope_closer'];
125-
continue;
126-
}
127-
128-
if ($tokens[$returnToken]['code'] === T_RETURN
129-
|| $tokens[$returnToken]['code'] === T_YIELD
130-
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
131-
) {
132-
break;
133-
}
134-
}
135-
136-
if ($returnToken !== $endToken) {
137-
// If the function is not returning anything, just
138-
// exiting, then there is no problem.
139-
$semicolon = $phpcsFile->findNext(T_WHITESPACE, ($returnToken + 1), null, true);
140-
if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
141-
$error = 'Function return type is void, but function contains return statement';
142-
$phpcsFile->addError($error, $return, 'InvalidReturnVoid');
143-
}
144-
}
118+
if (!$this->isReturnVoid($phpcsFile, $tokens, $stackPtr)) {
119+
$error = 'Function return type is void, but function contains return statement';
120+
$phpcsFile->addError($error, $return, 'InvalidReturnVoid');
145121
}//end if
146122
} else if ($returnType !== 'mixed' && in_array('void', $typeNames, true) === false) {
147123
// If return type is not void, there needs to be a return statement
@@ -163,12 +139,51 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
163139
}//end if
164140
}//end if
165141
} else {
166-
$error = 'Missing @return tag in function comment';
167-
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
142+
if (!$this->isReturnVoid($phpcsFile, $tokens, $stackPtr)) {
143+
$error = 'Missing @return tag in function comment';
144+
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
145+
}
168146
}//end if
169147

170148
}//end processReturn()
171149

150+
private function isReturnVoid($phpcsFile, $tokens, $stackPtr) : bool
151+
{
152+
if (isset($tokens[$stackPtr]['scope_closer']) !== true) {
153+
return true;
154+
}
155+
156+
$endToken = $tokens[$stackPtr]['scope_closer'];
157+
for ($returnToken = $stackPtr; $returnToken < $endToken; $returnToken++) {
158+
if ($tokens[$returnToken]['code'] === T_CLOSURE
159+
|| $tokens[$returnToken]['code'] === T_ANON_CLASS
160+
) {
161+
$returnToken = $tokens[$returnToken]['scope_closer'];
162+
continue;
163+
}
164+
165+
if ($tokens[$returnToken]['code'] === T_RETURN
166+
|| $tokens[$returnToken]['code'] === T_YIELD
167+
|| $tokens[$returnToken]['code'] === T_YIELD_FROM
168+
) {
169+
break;
170+
}
171+
}
172+
173+
if ($returnToken === $endToken) {
174+
return true;
175+
}
176+
177+
// If the function is not returning anything, just
178+
// exiting, then there is no problem.
179+
$semicolon = $phpcsFile->findNext(T_WHITESPACE, ($returnToken + 1), null, true);
180+
if ($tokens[$semicolon]['code'] !== T_SEMICOLON) {
181+
return false;
182+
}
183+
184+
return true;
185+
}
186+
172187
/**
173188
* Process any throw tags that this function comment has.
174189
*

0 commit comments

Comments
 (0)