Skip to content

Commit 4a7e3e8

Browse files
committed
fix: wrongly adding @return to the wrong docblock.
When a function doesn't have a docblock that is found to need a `@return`, and the function or class property above it does have a docblock, the `@return` is added to that docblock. Ergo: the wrong docblock. The problem was that the code was finding the previous docblock in the file and treating it as if it belonged to the current function, even when there were other elements (like properties or other functions) between the docblock and the function being processed. To fix, we validate whether the found docblock actually belongs to the function we were looking for. This is done by looking for significant tokens between the found docblock and the function. If the tokens contain anything other than function modifiers like `public`, `protected`, `abstract`, etc. then we ignore the docblock entirely and just stop processing the issue. - Added logic in the `FunctionCommentSniff` class to check if the docblock belongs to the function and skip if it doesn't. - Added new test functions in the test file.
1 parent 457137c commit 4a7e3e8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

test_utils/TestFile.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ class TestClass
7575
*/
7676
private boolean|string $bar;
7777

78+
private function testFunctionWithoutDocblock(): string {
79+
// Function without a docblock, a required `@return` tag should NOT be added to
80+
// the previous docblock ($bar) (should NOT be flagged).
81+
return 'Hello World';
82+
}
83+
84+
private function anotherFunctionWithoutDocblock(): int {
85+
// Function without a docblock, a required `@return` tag should NOT be added to
86+
// the previous docblock ($bar) (should NOT be flagged).
87+
return 42;
88+
}
89+
90+
7891

7992
/***************
8093
* TYPES TESTS *

test_utils/TestFile_WithErrors_DoNotFix.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ class TestClass
7575
*/
7676
private boolean|string $bar;
7777

78+
private function testFunctionWithoutDocblock(): string {
79+
// Function without a docblock, a required `@return` tag should NOT be added to
80+
// the previous docblock ($bar) (should NOT be flagged).
81+
return 'Hello World';
82+
}
83+
84+
private function anotherFunctionWithoutDocblock(): int {
85+
// Function without a docblock, a required `@return` tag should NOT be added to
86+
// the previous docblock ($bar) (should NOT be flagged).
87+
return 42;
88+
}
89+
90+
7891

7992
/***************
8093
* TYPES TESTS *

yCodeTech/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ public function process(File $phpcsFile, $stackPtr)
6565
return;
6666
}
6767

68+
// Check if this docblock actually belongs to this function
69+
// There should be no other significant tokens between the docblock and the function
70+
$tokenAfterComment = $phpcsFile->findNext(
71+
[T_WHITESPACE, T_COMMENT, T_PUBLIC, T_PRIVATE, T_PROTECTED, T_STATIC, T_FINAL, T_ABSTRACT],
72+
($commentEnd + 1),
73+
$stackPtr,
74+
true
75+
);
76+
77+
if ($tokenAfterComment !== false && $tokenAfterComment !== $stackPtr) {
78+
// There are other tokens between the docblock and the function,
79+
// 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.
80+
return;
81+
}
82+
6883
$commentStart = $tokens[$commentEnd]['comment_opener'];
6984
// Check if function returns void
7085
$hasVoidReturn = $this->hasVoidReturn($phpcsFile, $stackPtr);

0 commit comments

Comments
 (0)