From 2a8dea262efe080af189721895e2cc397ebf985d Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 14 Feb 2022 23:41:48 +0000 Subject: [PATCH 1/3] Added failing test for bitwise operations on class properties --- test/Sniffs/Formatting/ReferenceUnitTest.inc | 10 ++++++++++ test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/test/Sniffs/Formatting/ReferenceUnitTest.inc b/test/Sniffs/Formatting/ReferenceUnitTest.inc index fade63e1..8772d121 100644 --- a/test/Sniffs/Formatting/ReferenceUnitTest.inc +++ b/test/Sniffs/Formatting/ReferenceUnitTest.inc @@ -18,3 +18,13 @@ $a = &new \DateTime(); $b = & new \DateTime(); $c =& new \DateTime(); $d =&new \DateTime(); + +class ReferenceClass +{ + public int $flag = 1; + + public function isFlagged() + { + return (bool) ($this->flag & 4); + } +} diff --git a/test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed b/test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed index acb3da7a..eebf9cf0 100644 --- a/test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed +++ b/test/Sniffs/Formatting/ReferenceUnitTest.inc.fixed @@ -18,3 +18,13 @@ $a = new \DateTime(); $b = new \DateTime(); $c = new \DateTime(); $d =new \DateTime(); + +class ReferenceClass +{ + public int $flag = 1; + + public function isFlagged() + { + return (bool) ($this->flag & 4); + } +} From bbe886dcb2602a412b98bb3d225cc195d077da7d Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 14 Feb 2022 23:43:15 +0000 Subject: [PATCH 2/3] Check if $stackPtr is reference --- .../Sniffs/Formatting/ReferenceSniff.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php b/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php index 9ea787dc..0168a77e 100644 --- a/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php @@ -35,6 +35,10 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + if (! $phpcsFile->isReference($stackPtr)) { + return; + } + $next = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true); if ($tokens[$next]['code'] === T_NEW) { $error = 'Reference operator is redundant before new keyword (objects are always passed by reference)'; From a0c8f5d7f9e1f1077091aa203e4e3ec76389273a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 15 Feb 2022 19:44:52 +0000 Subject: [PATCH 3/3] remove redundant condition --- .../Sniffs/Formatting/ReferenceSniff.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php b/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php index 0168a77e..b97130a9 100644 --- a/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Formatting/ReferenceSniff.php @@ -11,11 +11,9 @@ use function in_array; use const T_BITWISE_AND; -use const T_COMMA; use const T_NEW; use const T_OPEN_PARENTHESIS; use const T_OPEN_SHORT_ARRAY; -use const T_STRING; use const T_WHITESPACE; class ReferenceSniff implements Sniff @@ -53,16 +51,6 @@ public function process(File $phpcsFile, $stackPtr) $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true); - $tokenCodes = Tokens::$assignmentTokens + [ - T_COMMA => T_COMMA, - T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS, - T_OPEN_SHORT_ARRAY => T_OPEN_SHORT_ARRAY, - T_STRING => T_STRING, - ]; - if (! in_array($tokens[$prev]['code'], $tokenCodes, true)) { - return; - } - // One space before & if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line'] && ! in_array($tokens[$stackPtr - 1]['code'], [T_WHITESPACE, T_OPEN_PARENTHESIS, T_OPEN_SHORT_ARRAY], true)