Skip to content

Commit 6e33f43

Browse files
committed
Squiz/OperatorBracket: fix sniff to work with the PHP 8 identifier tokens
Includes extra unit tests covering the change in so far it wasn't already covered.
1 parent 21a7d14 commit 6e33f43

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ public function process(File $phpcsFile, $stackPtr)
115115
}
116116

117117
// Tokens that are allowed inside a bracketed operation.
118-
$allowed = [
118+
$allowed = Tokens::$nameTokens;
119+
$allowed += Tokens::$operators;
120+
$allowed += [
119121
T_VARIABLE => T_VARIABLE,
120122
T_LNUMBER => T_LNUMBER,
121123
T_DNUMBER => T_DNUMBER,
122-
T_STRING => T_STRING,
123124
T_WHITESPACE => T_WHITESPACE,
124-
T_NS_SEPARATOR => T_NS_SEPARATOR,
125125
T_SELF => T_SELF,
126126
T_STATIC => T_STATIC,
127127
T_PARENT => T_PARENT,
@@ -134,8 +134,6 @@ public function process(File $phpcsFile, $stackPtr)
134134
T_BITWISE_NOT => T_BITWISE_NOT,
135135
];
136136

137-
$allowed += Tokens::$operators;
138-
139137
$lastBracket = false;
140138
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
141139
$parenthesis = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true);
@@ -149,7 +147,10 @@ public function process(File $phpcsFile, $stackPtr)
149147
break;
150148
}
151149

152-
if ($prevCode === T_STRING || $prevCode === T_SWITCH || $prevCode === T_MATCH) {
150+
if (isset(Tokens::$nameTokens[$prevCode]) === true
151+
|| $prevCode === T_SWITCH
152+
|| $prevCode === T_MATCH
153+
) {
153154
// We allow simple operations to not be bracketed.
154155
// For example, ceil($one / $two).
155156
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
@@ -260,11 +261,9 @@ public function addMissingBracketsError($phpcsFile, $stackPtr)
260261
T_VARIABLE => true,
261262
T_LNUMBER => true,
262263
T_DNUMBER => true,
263-
T_STRING => true,
264264
T_CONSTANT_ENCAPSED_STRING => true,
265265
T_DOUBLE_QUOTED_STRING => true,
266266
T_WHITESPACE => true,
267-
T_NS_SEPARATOR => true,
268267
T_SELF => true,
269268
T_STATIC => true,
270269
T_OBJECT_OPERATOR => true,
@@ -281,6 +280,7 @@ public function addMissingBracketsError($phpcsFile, $stackPtr)
281280
if (isset(Tokens::$emptyTokens[$tokens[$before]['code']]) === true
282281
|| isset(Tokens::$operators[$tokens[$before]['code']]) === true
283282
|| isset(Tokens::$castTokens[$tokens[$before]['code']]) === true
283+
|| isset(Tokens::$nameTokens[$tokens[$before]['code']]) === true
284284
|| isset($allowed[$tokens[$before]['code']]) === true
285285
) {
286286
continue;
@@ -315,6 +315,7 @@ public function addMissingBracketsError($phpcsFile, $stackPtr)
315315
if (isset(Tokens::$emptyTokens[$tokens[$after]['code']]) === true
316316
|| isset(Tokens::$operators[$tokens[$after]['code']]) === true
317317
|| isset(Tokens::$castTokens[$tokens[$after]['code']]) === true
318+
|| isset(Tokens::$nameTokens[$tokens[$after]['code']]) === true
318319
|| isset($allowed[$tokens[$after]['code']]) === true
319320
) {
320321
continue;

src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.1.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ $cntPages = ceil(count($items) / self::ON_PAGE);
146146

147147
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
148148
error_reporting(E_ALL & ~E_NOTICE | ~E_WARNING);
149-
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC | \PDO::FETCH_GROUP | \PDO::FETCH_UNIQUE);
149+
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC | Relative\PDO::FETCH_GROUP | namespace\PDO::FETCH_UNIQUE);
150150
$di = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS);
151151
foo(1 + 2 + 3);
152152

@@ -201,3 +201,7 @@ match ($a) {
201201
};
202202

203203
$cntPages = ceil(count($items) / parent::ON_PAGE);
204+
205+
$nsRelative = namespace\doSomething($items + 10);
206+
$partiallyQualified = Partially\qualified($items + 10);
207+
$fullyQualified = \Fully\qualified($items + 10);

src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.1.inc.fixed

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ $cntPages = ceil(count($items) / self::ON_PAGE);
146146

147147
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
148148
error_reporting(E_ALL & ~E_NOTICE | ~E_WARNING);
149-
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC | \PDO::FETCH_GROUP | \PDO::FETCH_UNIQUE);
149+
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC | Relative\PDO::FETCH_GROUP | namespace\PDO::FETCH_UNIQUE);
150150
$di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS));
151151
foo(1 + 2 + 3);
152152

@@ -201,3 +201,7 @@ match ($a) {
201201
};
202202

203203
$cntPages = ceil(count($items) / parent::ON_PAGE);
204+
205+
$nsRelative = namespace\doSomething($items + 10);
206+
$partiallyQualified = Partially\qualified($items + 10);
207+
$fullyQualified = \Fully\qualified($items + 10);

0 commit comments

Comments
 (0)