From 341543c3f6942a6386f77662357ca97b99e48c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 15 Oct 2024 14:50:14 +0100 Subject: [PATCH] fix: WhiteSpace\OperatorAndKeywordSpacingSniff - conflict with declare statement since PHP_CodeSniffer 3.9.1 (#203) --- .../OperatorAndKeywordSpacingSniff.php | 17 +++++++++++++- .../OperatorAndKeywordSpacingUnitTest.1.inc | 3 +++ .../OperatorAndKeywordSpacingUnitTest.inc | 2 ++ ...peratorAndKeywordSpacingUnitTest.inc.fixed | 2 ++ .../OperatorAndKeywordSpacingUnitTest.php | 22 +++++++++++-------- 5 files changed, 36 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..3072887c 100644 --- a/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php @@ -11,6 +11,7 @@ use function in_array; use const T_AS; +use const T_DECLARE; use const T_FN_ARROW; use const T_INSTANCEOF; use const T_INSTEADOF; @@ -40,16 +41,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 + * @return null|int */ - 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, ]; }