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.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc similarity index 98% rename from src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc rename to src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc index c067e6a2a8..14cf8e9dfd 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.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.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed similarity index 98% rename from src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed rename to src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index 76764291fa..0f52f1cf74 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.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 @@ + */ - 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() diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 87dbed8818..97007fc458 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -114,6 +114,9 @@ public function register() $targets[] = T_INLINE_ELSE; $targets[] = T_INSTANCEOF; + // Also register the contexts we want to specifically skip over. + $targets[] = T_DECLARE; + return $targets; }//end register() @@ -126,12 +129,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; } @@ -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.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.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 @@ + 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', ],