Skip to content

Commit 1516e63

Browse files
committed
Fixed bug #502 : PSR1 SideEffectsSniff sees declare() statements as side effects
1 parent 7bbed33 commit 1516e63

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,19 @@ private function _searchForConflict(PHP_CodeSniffer_File $phpcsFile, $start, $en
119119
continue;
120120
}
121121

122-
// Ignore entire namespace, const and use statements.
123-
if ($tokens[$i]['code'] === T_NAMESPACE) {
124-
$next = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($i + 1));
125-
if ($next === false) {
126-
$next = $i++;
127-
} else if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) {
128-
$next = $tokens[$next]['bracket_closer'];
129-
}
130-
131-
$i = $next;
132-
continue;
133-
} else if ($tokens[$i]['code'] === T_USE
122+
// Ignore entire namespace, declare, const and use statements.
123+
if ($tokens[$i]['code'] === T_NAMESPACE
124+
|| $tokens[$i]['code'] === T_USE
125+
|| $tokens[$i]['code'] === T_DECLARE
134126
|| $tokens[$i]['code'] === T_CONST
135127
) {
136-
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
137-
if ($semicolon !== false) {
138-
$i = $semicolon;
128+
if (isset($tokens[$i]['scope_opener']) === true) {
129+
$i = $tokens[$i]['scope_closer'];
130+
} else {
131+
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
132+
if ($semicolon !== false) {
133+
$i = $semicolon;
134+
}
139135
}
140136

141137
continue;

CodeSniffer/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const CONSTANT2 = 2;
77
use Something;
88
use SomethingElse;
99

10+
declare(ticks=1);
11+
12+
declare(ticks=1) {
13+
// Code.
14+
}
15+
1016
define("MAXSIZE", 100);
1117
if (defined('MINSIZE') === false) {
1218
define("MINSIZE", 10);

CodeSniffer/Tokenizers/PHP.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class PHP_CodeSniffer_Tokenizers_PHP
171171
'shared' => false,
172172
'with' => array(),
173173
),
174+
T_DECLARE => array(
175+
'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
176+
'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
177+
'strict' => false,
178+
'shared' => false,
179+
'with' => array(),
180+
),
174181
T_NAMESPACE => array(
175182
'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
176183
'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6464
- Fixed bug #500 : Functions not supported as values in Squiz ArrayDeclaration sniff
6565
- Fixed bug #501 : ScopeClosingBrace and ScopeIndent conflict with closures used as array values
6666
-- Generic ScopeIndentSniff may now report fewer errors for closures, but perform the same fixes
67+
- Fixed bug #502 : PSR1 SideEffectsSniff sees declare() statements as side effects
6768
</notes>
6869
<contents>
6970
<dir name="/">

0 commit comments

Comments
 (0)