From d6778b03f951101ebf2e6780c0a38e01c9f1bf83 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 4 Mar 2024 21:00:57 +0000 Subject: [PATCH 1/3] Squiz.WhiteSpace.OperatorSpacing: rename test file There are no functional code changes being applied here. This is a preparation step to allow adding an additional test case file for this sniff later. --- ...torSpacingUnitTest.inc => OperatorSpacingUnitTest.1.inc} | 0 ...itTest.inc.fixed => OperatorSpacingUnitTest.1.inc.fixed} | 0 .../Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php | 2 +- tests/Core/Filters/AbstractFilterTestCase.php | 4 ++-- tests/Core/Filters/GitModifiedTest.php | 6 +++--- tests/Core/Filters/GitStagedTest.php | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) rename src/Standards/Squiz/Tests/WhiteSpace/{OperatorSpacingUnitTest.inc => OperatorSpacingUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/WhiteSpace/{OperatorSpacingUnitTest.inc.fixed => OperatorSpacingUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc rename to src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed diff --git a/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php index e9a50c7e7d..6718664058 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php @@ -33,7 +33,7 @@ final class OperatorSpacingUnitTest extends AbstractSniffUnitTest public function getErrorList($testFile='') { switch ($testFile) { - case 'OperatorSpacingUnitTest.inc': + case 'OperatorSpacingUnitTest.1.inc': return [ 4 => 1, 5 => 2, diff --git a/tests/Core/Filters/AbstractFilterTestCase.php b/tests/Core/Filters/AbstractFilterTestCase.php index d196ce6917..15dd5bcbc3 100644 --- a/tests/Core/Filters/AbstractFilterTestCase.php +++ b/tests/Core/Filters/AbstractFilterTestCase.php @@ -184,8 +184,8 @@ protected static function getFakeFileList() $basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php', $basedir.'/src/Standards/Squiz/Tests', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace', - $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc', - $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed', + $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc', + $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php', diff --git a/tests/Core/Filters/GitModifiedTest.php b/tests/Core/Filters/GitModifiedTest.php index 1b2189f900..2fe894096e 100644 --- a/tests/Core/Filters/GitModifiedTest.php +++ b/tests/Core/Filters/GitModifiedTest.php @@ -175,8 +175,8 @@ public static function dataAcceptOnlyGitModified() '.yamllint.yml', 'autoload.php', 'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php', - 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc', - 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed', + 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc', + 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php', @@ -191,7 +191,7 @@ public static function dataAcceptOnlyGitModified() $basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php', $basedir.'/src/Standards/Squiz/Tests', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace', - $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc', + $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php', ], diff --git a/tests/Core/Filters/GitStagedTest.php b/tests/Core/Filters/GitStagedTest.php index 545f5839fa..0da18af9a3 100644 --- a/tests/Core/Filters/GitStagedTest.php +++ b/tests/Core/Filters/GitStagedTest.php @@ -175,8 +175,8 @@ public static function dataAcceptOnlyGitStaged() '.yamllint.yml', 'autoload.php', 'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php', - 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc', - 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed', + 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc', + 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed', 'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php', @@ -191,7 +191,7 @@ public static function dataAcceptOnlyGitStaged() $basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php', $basedir.'/src/Standards/Squiz/Tests', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace', - $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc', + $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js', $basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php', ], From 676ed654be25470f67ca384d4070f52436fccb07 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 4 Mar 2024 21:08:34 +0000 Subject: [PATCH 2/3] PSR12.Operators.OperatorSpacing: rename test files There are no functional code changes being applied here. This is a preparation step to allow adding an additional test case file for this sniff later. --- ...Test.inc => OperatorSpacingUnitTest.1.inc} | 0 ...ed => OperatorSpacingUnitTest.1.inc.fixed} | 0 .../Operators/OperatorSpacingUnitTest.php | 53 +++++++++++-------- 3 files changed, 30 insertions(+), 23 deletions(-) rename src/Standards/PSR12/Tests/Operators/{OperatorSpacingUnitTest.inc => OperatorSpacingUnitTest.1.inc} (100%) rename src/Standards/PSR12/Tests/Operators/{OperatorSpacingUnitTest.inc.fixed => OperatorSpacingUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc similarity index 100% rename from src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc rename to src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed rename to src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index abcce946cd..4affd51c57 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -26,32 +26,39 @@ final class OperatorSpacingUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 2 => 1, - 3 => 2, - 4 => 1, - 5 => 2, - 6 => 4, - 9 => 3, - 10 => 2, - 11 => 3, - 13 => 3, - 14 => 2, - 18 => 1, - 20 => 1, - 22 => 2, - 23 => 2, - 26 => 1, - 37 => 4, - 39 => 1, - 40 => 1, - 44 => 2, - 47 => 2, - ]; + switch ($testFile) { + case 'OperatorSpacingUnitTest.1.inc': + return [ + 2 => 1, + 3 => 2, + 4 => 1, + 5 => 2, + 6 => 4, + 9 => 3, + 10 => 2, + 11 => 3, + 13 => 3, + 14 => 2, + 18 => 1, + 20 => 1, + 22 => 2, + 23 => 2, + 26 => 1, + 37 => 4, + 39 => 1, + 40 => 1, + 44 => 2, + 47 => 2, + ]; + default: + return []; + }//end switch }//end getErrorList() From 73ca839e567266f36c1e631dc5e7755e9e2d4b19 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 4 Mar 2024 21:24:11 +0000 Subject: [PATCH 3/3] Squiz.WhiteSpace.OperatorSpacing / PSR12.Operators.OperatorSpacing - skip over declare() statements entirely These sniffs intentionally do not handle declare statements, as these are handled by other sniffs. The logic previously used was not complete enough to bow out in all circumstances. These changes skip over the whole declare statement by registering for its token, and skipping over the statement when found. This ensures that nothing inside it is detected and processed by mistake. --- .../Sniffs/Operators/OperatorSpacingSniff.php | 18 ++++++++++++- .../Operators/OperatorSpacingUnitTest.1.inc | 2 ++ .../OperatorSpacingUnitTest.1.inc.fixed | 2 ++ .../Operators/OperatorSpacingUnitTest.2.inc | 3 +++ .../WhiteSpace/OperatorSpacingSniff.php | 27 +++++++++++++++---- .../WhiteSpace/OperatorSpacingUnitTest.2.inc | 3 +++ 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.2.inc diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 0e58f18c13..41628ce3a0 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -35,6 +35,9 @@ public function register() $targets[] = T_STRING_CONCAT; $targets[] = T_INSTANCEOF; + // Also register the contexts we want to specifically skip over. + $targets[] = T_DECLARE; + return $targets; }//end register() @@ -47,12 +50,25 @@ public function register() * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void + * @return void|int Optionally returns a stack pointer. The sniff will not be + * called again on the current file until the returned stack + * pointer is reached. Return `$phpcsFile->numTokens` to skip + * the rest of the file. */ 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']; + } + if ($this->isOperator($phpcsFile, $stackPtr) === false) { return; } diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc index c067e6a2a8..14cf8e9dfd 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc @@ -75,3 +75,5 @@ function setDefault(#[ImportValue( { // Do something } + +declare(strict_types=1); diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index 76764291fa..0f52f1cf74 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -75,3 +75,5 @@ function setDefault(#[ImportValue( { // Do something } + +declare(strict_types=1); diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.2.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.2.inc new file mode 100644 index 0000000000..3a0dbac3e9 --- /dev/null +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.2.inc @@ -0,0 +1,3 @@ +numTokens` to skip + * the rest of the file. */ 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']; + } + if ($this->isOperator($phpcsFile, $stackPtr) === false) { return; } @@ -327,11 +343,13 @@ protected function isOperator(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + if ($tokens[$stackPtr]['code'] === T_DECLARE) { + return false; + } + // Skip default values in function declarations. // Skip declare statements. - if ($tokens[$stackPtr]['code'] === T_EQUAL - || $tokens[$stackPtr]['code'] === T_MINUS - ) { + if ($tokens[$stackPtr]['code'] === T_EQUAL) { if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); $bracket = array_pop($parenthesis); @@ -340,7 +358,6 @@ protected function isOperator(File $phpcsFile, $stackPtr) if ($tokens[$function]['code'] === T_FUNCTION || $tokens[$function]['code'] === T_CLOSURE || $tokens[$function]['code'] === T_FN - || $tokens[$function]['code'] === T_DECLARE ) { return false; } diff --git a/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.2.inc b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.2.inc new file mode 100644 index 0000000000..3a0dbac3e9 --- /dev/null +++ b/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.2.inc @@ -0,0 +1,3 @@ +