Skip to content

Commit e8cda77

Browse files
committed
PSR1/SideEffects: improve recognition of disable/enable annotations
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
1 parent b0d171b commit e8cda77

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,17 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
103103
&& (empty($tokens[$i]['sniffCodes']) === true
104104
|| isset($tokens[$i]['sniffCodes']['PSR1']) === true
105105
|| isset($tokens[$i]['sniffCodes']['PSR1.Files']) === true
106-
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true)
106+
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true
107+
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects.FoundWithSymbols']) === true)
107108
) {
108109
do {
109110
$i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
110111
} while ($i !== false
111112
&& empty($tokens[$i]['sniffCodes']) === false
112113
&& isset($tokens[$i]['sniffCodes']['PSR1']) === false
113114
&& isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
114-
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false);
115+
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false
116+
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects.FoundWithSymbols']) === false);
115117

116118
if ($i === false) {
117119
// The entire rest of the file is disabled,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
3+
define("MAXSIZE", 100);
4+
// phpcs:enable
5+
$defined = true;
6+
if (defined('MINSIZE') === false) {
7+
$defined = false;
8+
}

0 commit comments

Comments
 (0)