You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: void functions being wrongly flagged for missing @return tags
If a void function has a nested anonymous function or closure which returns a value, the `FunctionComment` sniff was wrongly flagging the void function has missing a `@return` tag.
Fixed by adding logic to check if the `return` statement is within a nested function/closure, and skipping it if it does.
Added:
- Added `isReturnInNestedFunction` method to `FunctionCommentSniff` class to check if `return` is in a nested function/closure.
- Added new test functions in both `TestFile`s.
Changed:
- Various `TestFile`s comments.
- Readme.
Removed:
- Auto formatting removals, superfluous whitespace and `@return void`.
Copy file name to clipboardExpand all lines: yCodeTech/Sniffs/Commenting/FunctionCommentSniff.php
+55-16Lines changed: 55 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -39,8 +39,6 @@ public function register()
39
39
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
40
40
* @param int $stackPtr The position of the current token in the
41
41
* stack passed in $tokens.
42
-
*
43
-
* @return void
44
42
*/
45
43
publicfunctionprocess(File$phpcsFile, $stackPtr)
46
44
{
@@ -73,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr)
73
71
$stackPtr,
74
72
true
75
73
);
76
-
74
+
77
75
if ($tokenAfterComment !== false && $tokenAfterComment !== $stackPtr) {
78
76
// There are other tokens between the docblock and the function,
79
77
// so this docblock doesn't belong to this function. So the function doesn't have a docblock, and found a previous function's docblock instead. Skipping.
@@ -116,7 +114,7 @@ public function process(File $phpcsFile, $stackPtr)
116
114
break;
117
115
}
118
116
}
119
-
117
+
120
118
if ($returnTagPtr !== false) {
121
119
$error = 'Void function should not have @return tag';
0 commit comments