From e34c1cd91a79f54a1d8eb49e919ef1df325ccb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 15 Oct 2024 14:44:04 +0100 Subject: [PATCH] fix: WhiteSpace\OperatorAndKeywordSpacingSniff - conflict with declare statement since PHP_CodeSniffer 3.9.1 PR https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/289 changes a lot around operator sniff, and now declare statement must be omitted (as it was done also for PSR-12 rule there). There is another sniff to verify declare statement, and we do not want to check it here. --- .../OperatorAndKeywordSpacingSniff.php | 16 +++++++++++++- .../OperatorAndKeywordSpacingUnitTest.1.inc | 3 +++ .../OperatorAndKeywordSpacingUnitTest.inc | 2 ++ ...peratorAndKeywordSpacingUnitTest.inc.fixed | 2 ++ .../OperatorAndKeywordSpacingUnitTest.php | 22 +++++++++++-------- 5 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.1.inc diff --git a/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php b/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php index 164d3197..48788b2c 100644 --- a/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php @@ -40,16 +40,30 @@ public function register() : array $tokens[] = T_INSTEADOF; $tokens[] = T_FN_ARROW; + // Also register the contexts we want to specifically skip over. + $tokens[] = T_DECLARE; + return $tokens; } /** * @param int $stackPtr */ - public function process(File $phpcsFile, $stackPtr) : void + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + // Skip over declare statements as those should be handled by different sniffs. + if ($tokens[$stackPtr]['code'] === T_DECLARE) { + if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { + // Parse error / live coding. + return $phpcsFile->numTokens; + } + + return $tokens[$stackPtr]['parenthesis_closer']; + } + + $originalValue = $this->ignoreNewlines; if (in_array($tokens[$stackPtr]['code'], $this->doNotIgnoreNewLineForTokens, true)) { $this->ignoreNewlines = false; diff --git a/test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.1.inc b/test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.1.inc new file mode 100644 index 00000000..3a0dbac3 --- /dev/null +++ b/test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.1.inc @@ -0,0 +1,3 @@ + 1, 6 => 1, - 11 => 2, - 12 => 2, - 17 => 2, - 21 => 1, + 8 => 1, + 13 => 2, + 14 => 2, + 19 => 2, 23 => 1, - 27 => 1, - 31 => 1, - 35 => 1, - 39 => 2, + 25 => 1, + 29 => 1, + 33 => 1, + 37 => 1, + 41 => 2, ]; }