Skip to content

Commit

Permalink
PSR1/SideEffects: improve recognition of disable/enable annotations
Browse files Browse the repository at this point in the history
As it was, the sniff would respect PHPCS disable annotations, but only when either unqualified or qualified up to a sniff name.
It would not respect a disable annotation using a sniffname with errorcode, i.e. `PSR1.Files.SideEffects.FoundWithSymbols`.

While the `FoundWithSymbols` errorcode is the only error code for this sniff and using `// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols` is effectively the same as using `// phpcs:disable PSR1.Files.SideEffects`, I do believe end-users should not need to be aware of whether or not a sniff has multiple error codes when using disable annotations.

This updates the sniff to also respect disable annotations which include the error code.

Includes unit test.

Fixes 3386
  • Loading branch information
jrfnl committed Dec 4, 2023
1 parent 334a64f commit 7552c17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
&& (empty($tokens[$i]['sniffCodes']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1.Files']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true)
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects.FoundWithSymbols']) === true)
) {
do {
$i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
} while ($i !== false
&& empty($tokens[$i]['sniffCodes']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false);
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects.FoundWithSymbols']) === false);

if ($i === false) {
// The entire rest of the file is disabled,
Expand Down
8 changes: 8 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.17.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
define("MAXSIZE", 100);
// phpcs:enable
$defined = true;
if (defined('MINSIZE') === false) {
$defined = false;
}

0 comments on commit 7552c17

Please sign in to comment.