Skip to content

Commit 195e941

Browse files
committed
Squiz/OperatorSpacing: bug fix - prevent fixer conflict
Another one in the fixer conflict series. When running the `Squiz` standard over all test case files, a fixer conflict in the `Squiz.WhiteSpace.OperatorSpacing` sniff was discovered via the tests in the `./src/Standards/Generic/Tests/CodeAnalysis/RequireExplicitBooleanOperatorPrecedenceUnitTest.inc` file for a code sample like this: ```php $foo = $var // Comment. ? false // Comment. : true; ``` The conflict essentially comes down to the `Squiz.WhiteSpace.OperatorSpacing` trying to remove the new line before the `?` and the `:` operator, but failing to do so as the new line is included in the comment token on the previous line and the sniff only adjusts/removes whitespace tokens. Original errors for the code sample added in the test case file: ``` 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 493 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 494 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 499 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 504 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) ``` The fix proposed in this PR changes the sniff to make the `SpacingBefore` error code non-fixable if the previous non-whitespace token is a comment token - even when the comment token doesn't contain a new line. While the sniff _could_ auto-fix when the comment token doesn't contain a new line, I have chosen to disable the auto-fixer for those cases anyway as it is not for the sniff to determine whether the comment should be moved, removed or should stay where it is. With these changes, the code sample in the test case file now yields the following errors: ``` 487 | ERROR | [x] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 488 | ERROR | [x] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 493 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 494 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 499 | ERROR | [ ] Expected 1 space before "?"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) 504 | ERROR | [ ] Expected 1 space before ":"; newline found (Squiz.WhiteSpace.OperatorSpacing.SpacingBefore) ``` This effectively fixes the fixer conflict. :point_right: The diff will be easier to review while ignoring whitespace changes. --- <details> <summary>Fixer Conflict details</summary> ``` * fixed 0 violations, starting loop 48 * => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258 Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" => Changeset ended: 1 changes applied => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258 Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 925 (T_WHITESPACE on line 73) " :" => " :" A: Squiz.WhiteSpace.OperatorSpacing:268 replaced token 925 (T_WHITESPACE on line 73) " :" => " :" => Changeset ended: 1 changes applied => Fixing file: 2/2 violations remaining [made 48 passes]... * fixed 2 violations, starting loop 49 * => Changeset started by Squiz.WhiteSpace.OperatorSpacing:258 Q: Squiz.WhiteSpace.OperatorSpacing:267 replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" **** Squiz.WhiteSpace.OperatorSpacing:268 has possible conflict with another sniff on loop 47; caused by the following change **** **** replaced token 911 (T_WHITESPACE on line 72) " ?" => " ?" **** **** ignoring all changes until next loop **** => Changeset failed to apply => Fixing file: 0/2 violations remaining [made 49 passes]... ``` </details> --- Related to 152
1 parent 779bbe1 commit 195e941

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php

+23-13
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ public function process(File $phpcsFile, $stackPtr)
238238
) {
239239
// Throw an error for assignments only if enabled using the sniff property
240240
// because other standards allow multiple spaces to align assignments.
241-
if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) {
241+
$prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
242+
if ($tokens[$prevNonWhitespace]['line'] !== $tokens[$stackPtr]['line']) {
242243
$found = 'newline';
243244
} else {
244245
$found = $tokens[($stackPtr - 1)]['length'];
@@ -253,20 +254,29 @@ public function process(File $phpcsFile, $stackPtr)
253254
$operator,
254255
$found,
255256
];
256-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data);
257-
if ($fix === true) {
258-
$phpcsFile->fixer->beginChangeset();
259-
if ($found === 'newline') {
260-
$i = ($stackPtr - 2);
261-
while ($tokens[$i]['code'] === T_WHITESPACE) {
262-
$phpcsFile->fixer->replaceToken($i, '');
263-
$i--;
257+
258+
if (isset(Tokens::$commentTokens[$tokens[$prevNonWhitespace]['code']]) === true) {
259+
// Throw a non-fixable error if the token on the previous line is a comment token,
260+
// as in that case it's not for the sniff to decide where the comment should be moved to
261+
// and it would get us into unfixable situations as the new line char is included
262+
// in the contents of the comment token.
263+
$phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
264+
} else {
265+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data);
266+
if ($fix === true) {
267+
$phpcsFile->fixer->beginChangeset();
268+
if ($found === 'newline') {
269+
$i = ($stackPtr - 2);
270+
while ($tokens[$i]['code'] === T_WHITESPACE) {
271+
$phpcsFile->fixer->replaceToken($i, '');
272+
$i--;
273+
}
264274
}
265-
}
266275

267-
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
268-
$phpcsFile->fixer->endChangeset();
269-
}
276+
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
277+
$phpcsFile->fixer->endChangeset();
278+
}
279+
}//end if
270280
}//end if
271281
}//end if
272282

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc

+26
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,29 @@ match ($a) {
482482
'b', 'c', 'd' => -2,
483483
default => -3,
484484
};
485+
486+
$foo = $var
487+
? 10
488+
: true;
489+
490+
// Safeguard that a non-fixable error is thrown when there is a new line before the operator,
491+
// but the last non-whitespace token before the operator is a comment token.
492+
$foo = $var // Comment
493+
? false /* Comment */
494+
: true;
495+
496+
$foo = $var // phpcs: ignore Stnd.Cat.Sniff -- for reasons.
497+
498+
499+
? $something /**
500+
* Don't ask, but someone might have a docblock between the lines. It's valid PHP after all.
501+
*/
502+
503+
504+
: true;
505+
506+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing ignoreNewlines true
507+
$foo = $var // Comment
508+
? false // Comment
509+
: true;
510+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing ignoreNewlines false

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed

+24
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,27 @@ match ($a) {
476476
'b', 'c', 'd' => -2,
477477
default => -3,
478478
};
479+
480+
$foo = $var ? 10 : true;
481+
482+
// Safeguard that a non-fixable error is thrown when there is a new line before the operator,
483+
// but the last non-whitespace token before the operator is a comment token.
484+
$foo = $var // Comment
485+
? false /* Comment */
486+
: true;
487+
488+
$foo = $var // phpcs: ignore Stnd.Cat.Sniff -- for reasons.
489+
490+
491+
? $something /**
492+
* Don't ask, but someone might have a docblock between the lines. It's valid PHP after all.
493+
*/
494+
495+
496+
: true;
497+
498+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing ignoreNewlines true
499+
$foo = $var // Comment
500+
? false // Comment
501+
: true;
502+
// phpcs:set Squiz.WhiteSpace.OperatorSpacing ignoreNewlines false

src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public function getErrorList($testFile='')
104104
265 => 2,
105105
266 => 2,
106106
271 => 2,
107+
487 => 1,
108+
488 => 1,
109+
493 => 1,
110+
494 => 1,
111+
499 => 1,
112+
504 => 1,
107113
];
108114

109115
case 'OperatorSpacingUnitTest.js':

0 commit comments

Comments
 (0)