Skip to content

Commit dcd89b7

Browse files
committed
Squiz/InlineComment: fix fixer conflict when comment found at end of function
When an inline comment is found at the end of a function, removing a blank line after it conflicts with the `Squiz.WhiteSpace.FunctionClosingBraceSpace` sniff which demands a blank line at the end of a function. I have chosen to fix this by adding a specific error code for that situation and excluding that error code from the `Squiz` ruleset. This way, the backward-compatibility break for other standards using the `Squiz.Commenting.InlineComment` sniff will be smallest. Only if a standard explicitly in/excluded the `Squiz.Commenting.InlineComment.SpacingAfter` errorcode will this have any effect on them. The unit tests already contain a test covering this. As the errorcode is excluded via the ruleset, the results of the unit tests do not change. Fixes 1709
1 parent 6b4d9e5 commit dcd89b7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ public function process(File $phpcsFile, $stackPtr)
292292
return;
293293
}
294294

295+
$errorCode = 'SpacingAfter';
296+
297+
if (isset($tokens[$stackPtr]['conditions']) === true) {
298+
$type = end($tokens[$stackPtr]['conditions']);
299+
$conditionPtr = key($tokens[$stackPtr]['conditions']);
300+
301+
if (in_array($type, [T_FUNCTION, T_CLOSURE], true) === true
302+
&& $tokens[$conditionPtr]['scope_closer'] === $next
303+
) {
304+
$errorCode = 'SpacingAfterAtFunctionEnd';
305+
}
306+
}
307+
295308
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
296309
if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) {
297310
if ($tokens[$i]['code'] !== T_WHITESPACE) {
@@ -303,7 +316,7 @@ public function process(File $phpcsFile, $stackPtr)
303316
}
304317

305318
$error = 'There must be no blank line following an inline comment';
306-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfter');
319+
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode);
307320
if ($fix === true) {
308321
$phpcsFile->fixer->beginChangeset();
309322
for ($i = ($stackPtr + 1); $i < $next; $i++) {

src/Standards/Squiz/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,9 @@
8989
<severity>0</severity>
9090
</rule>
9191

92+
<!-- Prevent fixer conflict for conflicting rules. -->
93+
<rule ref="Squiz.Commenting.InlineComment">
94+
<exclude name="Squiz.Commenting.InlineComment.SpacingAfterAtFunctionEnd"/>
95+
</rule>
96+
9297
</ruleset>

0 commit comments

Comments
 (0)