Skip to content

Commit ae4228d

Browse files
committed
Squiz.WhiteSpace.OperatorSpacing / PSR12.Operators.OperatorSpacing - skip over declare() statements entirely
These sniffs intentionally do not handle declare statements, as these are handled by other sniffs. The logic previously used was not complete enough to bow out in all circumstances. These changes skip over the whole declare statement by registering for its token, and skipping over the statement when found. This ensures that nothing inside it is detected and processed by mistake.
1 parent 676ed65 commit ae4228d

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function register()
3535
$targets[] = T_STRING_CONCAT;
3636
$targets[] = T_INSTANCEOF;
3737

38+
// Also register the contexts we want to specifically skip over.
39+
$targets[] = T_DECLARE;
40+
3841
return $targets;
3942

4043
}//end register()
@@ -47,12 +50,25 @@ public function register()
4750
* @param int $stackPtr The position of the current token in
4851
* the stack passed in $tokens.
4952
*
50-
* @return void
53+
* @return void|int Optionally returns a stack pointer. The sniff will not be
54+
* called again on the current file until the returned stack
55+
* pointer is reached. Return `$phpcsFile->numTokens` to skip
56+
* the rest of the file.
5157
*/
5258
public function process(File $phpcsFile, $stackPtr)
5359
{
5460
$tokens = $phpcsFile->getTokens();
5561

62+
// Skip over declare statements as those should be handled by different sniffs.
63+
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
64+
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
65+
// Parse error / live coding.
66+
return $phpcsFile->numTokens;
67+
}
68+
69+
return $tokens[$stackPtr]['parenthesis_closer'];
70+
}
71+
5672
if ($this->isOperator($phpcsFile, $stackPtr) === false) {
5773
return;
5874
}

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc

+2
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ function setDefault(#[ImportValue(
7575
{
7676
// Do something
7777
}
78+
79+
declare(strict_types=1);

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ function setDefault(#[ImportValue(
7575
{
7676
// Do something
7777
}
78+
79+
declare(strict_types=1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Safeguard to ensure that sniff handles parse error/live coding correctly.
3+
declare(strict_types=

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

+22-5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public function register()
114114
$targets[] = T_INLINE_ELSE;
115115
$targets[] = T_INSTANCEOF;
116116

117+
// Also register the contexts we want to specifically skip over.
118+
$targets[] = T_DECLARE;
119+
117120
return $targets;
118121

119122
}//end register()
@@ -126,12 +129,25 @@ public function register()
126129
* @param int $stackPtr The position of the current token in
127130
* the stack passed in $tokens.
128131
*
129-
* @return void
132+
* @return void|int Optionally returns a stack pointer. The sniff will not be
133+
* called again on the current file until the returned stack
134+
* pointer is reached. Return `$phpcsFile->numTokens` to skip
135+
* the rest of the file.
130136
*/
131137
public function process(File $phpcsFile, $stackPtr)
132138
{
133139
$tokens = $phpcsFile->getTokens();
134140

141+
// Skip over declare statements as those should be handled by different sniffs.
142+
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
143+
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
144+
// Parse error / live coding.
145+
return $phpcsFile->numTokens;
146+
}
147+
148+
return $tokens[$stackPtr]['parenthesis_closer'];
149+
}
150+
135151
if ($this->isOperator($phpcsFile, $stackPtr) === false) {
136152
return;
137153
}
@@ -327,11 +343,13 @@ protected function isOperator(File $phpcsFile, $stackPtr)
327343
{
328344
$tokens = $phpcsFile->getTokens();
329345

346+
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
347+
return false;
348+
}
349+
330350
// Skip default values in function declarations.
331351
// Skip declare statements.
332-
if ($tokens[$stackPtr]['code'] === T_EQUAL
333-
|| $tokens[$stackPtr]['code'] === T_MINUS
334-
) {
352+
if ($tokens[$stackPtr]['code'] === T_EQUAL) {
335353
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
336354
$parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
337355
$bracket = array_pop($parenthesis);
@@ -340,7 +358,6 @@ protected function isOperator(File $phpcsFile, $stackPtr)
340358
if ($tokens[$function]['code'] === T_FUNCTION
341359
|| $tokens[$function]['code'] === T_CLOSURE
342360
|| $tokens[$function]['code'] === T_FN
343-
|| $tokens[$function]['code'] === T_DECLARE
344361
) {
345362
return false;
346363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Safeguard to ensure that sniff handles parse error/live coding correctly.
3+
declare(strict_types=

0 commit comments

Comments
 (0)