Skip to content

Commit 479d914

Browse files
committed
fix: prevent docblocks incorrectly being treated as one docblock.
If a docblock contains only a tag with no content, the `DocblockFormat` sniff will incorrectly uses the next function's docblock as part of the current docblock, essentially treating 2 docblocks as one. This then falsely flags the tag as having a `TagSpacing` violation. To fix, we just need to implement boundary limits to ensure the content is only parsed in the current docblock before the closing comment tag. - Fixed `checkTagSpacing` method in `DocblockFormatSniff` class to properly define and check docblock boundaries. - Added new test functions in both `TestFile`s to ensure this doesn't get flagged.
1 parent f1fe7c1 commit 479d914

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

test_utils/TestFile.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ public function testReturnEmptyLine()
217217
return "Hello World";
218218
}
219219

220+
/**
221+
* Function with an @ tag that has no content, with another function's docblock below it.
222+
*
223+
* The `testTagWithNoContentContinued` function's docblock below should
224+
* not be considered as part of the `@param` in the docblock below
225+
* (should NOT be flagged for incorrect `TagSpacing`).
226+
*
227+
* @param
228+
*/
229+
public function testTagWithNoContent() {
230+
}
231+
232+
/**
233+
* Test function continued from above (should NOT be flagged for incorrect `TagSpacing`).
234+
*/
235+
public function testTagWithNoContentContinued() {
236+
}
237+
220238

221239
/*************************
222240
* MISSING @RETURN TESTS *

test_utils/TestFile_WithErrors_DoNotFix.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ public function testReturnEmptyLine()
217217
return "Hello World";
218218
}
219219

220+
/**
221+
* Function with an @ tag that has no content, with another function's docblock below it.
222+
*
223+
* The `testTagWithNoContentContinued` function's docblock below should
224+
* not be considered as part of the `@param` in the docblock below
225+
* (should NOT be flagged for incorrect `TagSpacing`).
226+
*
227+
* @param
228+
*/
229+
public function testTagWithNoContent() {
230+
}
231+
232+
/**
233+
* Test function continued from above (should NOT be flagged for incorrect `TagSpacing`).
234+
*/
235+
public function testTagWithNoContentContinued() {
236+
}
237+
220238

221239
/*************************
222240
* MISSING @RETURN TESTS *

yCodeTech/Sniffs/Commenting/DocblockFormatSniff.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,14 @@ private function checkTagSpacing(File $phpcsFile, $stackPtr)
155155
{
156156
$tokens = $phpcsFile->getTokens();
157157

158-
// Check spacing between @ tag and its content
159-
$next = $phpcsFile->findNext([T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STRING], ($stackPtr + 1));
158+
// Find the docblock boundaries to ensure we don't go beyond the current docblock
159+
$docBlockEnd = $phpcsFile->findNext([T_DOC_COMMENT_CLOSE_TAG], ($stackPtr + 1));
160+
if ($docBlockEnd === false) {
161+
return;
162+
}
163+
164+
// Check spacing between @ tag and its content (within same docblock only)
165+
$next = $phpcsFile->findNext([T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STRING], ($stackPtr + 1), $docBlockEnd);
160166
if ($next === false) {
161167
return;
162168
}
@@ -173,9 +179,9 @@ private function checkTagSpacing(File $phpcsFile, $stackPtr)
173179
if ($whitespaceContent !== ' ') {
174180
$needsFixing = true;
175181
}
176-
177-
// Move to next token which should be the string
178-
$contentToken = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($next + 1));
182+
183+
// Move to next token which should be the string (within same docblock)
184+
$contentToken = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($next + 1), $docBlockEnd);
179185
if ($contentToken === false) {
180186
return;
181187
}

0 commit comments

Comments
 (0)