Skip to content

Commit cd0c4b0

Browse files
kynxmattmichalbundyra
authored
ReferenceSniff: Fix false positive with bitwise and (#162)
Co-authored-by: matt <[email protected]> Co-authored-by: Michał Bundyra <[email protected]>
1 parent e47e6af commit cd0c4b0

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
use function in_array;
1212

1313
use const T_BITWISE_AND;
14-
use const T_COMMA;
1514
use const T_NEW;
1615
use const T_OPEN_PARENTHESIS;
1716
use const T_OPEN_SHORT_ARRAY;
18-
use const T_STRING;
1917
use const T_WHITESPACE;
2018

2119
class ReferenceSniff implements Sniff
@@ -35,6 +33,10 @@ public function process(File $phpcsFile, $stackPtr)
3533
{
3634
$tokens = $phpcsFile->getTokens();
3735

36+
if (! $phpcsFile->isReference($stackPtr)) {
37+
return;
38+
}
39+
3840
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
3941
if ($tokens[$next]['code'] === T_NEW) {
4042
$error = 'Reference operator is redundant before new keyword (objects are always passed by reference)';
@@ -49,16 +51,6 @@ public function process(File $phpcsFile, $stackPtr)
4951

5052
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
5153

52-
$tokenCodes = Tokens::$assignmentTokens + [
53-
T_COMMA => T_COMMA,
54-
T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
55-
T_OPEN_SHORT_ARRAY => T_OPEN_SHORT_ARRAY,
56-
T_STRING => T_STRING,
57-
];
58-
if (! in_array($tokens[$prev]['code'], $tokenCodes, true)) {
59-
return;
60-
}
61-
6254
// One space before &
6355
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']
6456
&& ! in_array($tokens[$stackPtr - 1]['code'], [T_WHITESPACE, T_OPEN_PARENTHESIS, T_OPEN_SHORT_ARRAY], true)

test/Sniffs/Formatting/ReferenceUnitTest.inc

+10
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ $a = &new \DateTime();
1818
$b = & new \DateTime();
1919
$c =& new \DateTime();
2020
$d =&new \DateTime();
21+
22+
class ReferenceClass
23+
{
24+
public int $flag = 1;
25+
26+
public function isFlagged()
27+
{
28+
return (bool) ($this->flag & 4);
29+
}
30+
}

test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ $a = new \DateTime();
1818
$b = new \DateTime();
1919
$c = new \DateTime();
2020
$d =new \DateTime();
21+
22+
class ReferenceClass
23+
{
24+
public int $flag = 1;
25+
26+
public function isFlagged()
27+
{
28+
return (bool) ($this->flag & 4);
29+
}
30+
}

0 commit comments

Comments
 (0)