From a176290b42b9d3c3c255b95ebe298474624c0484 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 28 Nov 2026 19:38:57 +0530 Subject: [PATCH] Add OperatorSpacingSniff for union/intersection type separators in catch statements --- .../Sniffs/Operators/OperatorSpacingSniff.php | 236 ++++++++++++++++++ .../Operators/OperatorSpacingUnitTest.inc | 40 +++ .../OperatorSpacingUnitTest.inc.fixed | 40 +++ .../Operators/OperatorSpacingUnitTest.php | 32 +++ CakePHP/ruleset.xml | 5 + docs/README.md | 6 +- 6 files changed, 356 insertions(+), 3 deletions(-) create mode 100644 CakePHP/Sniffs/Operators/OperatorSpacingSniff.php create mode 100644 CakePHP/Tests/Operators/OperatorSpacingUnitTest.inc create mode 100644 CakePHP/Tests/Operators/OperatorSpacingUnitTest.inc.fixed create mode 100644 CakePHP/Tests/Operators/OperatorSpacingUnitTest.php diff --git a/CakePHP/Sniffs/Operators/OperatorSpacingSniff.php b/CakePHP/Sniffs/Operators/OperatorSpacingSniff.php new file mode 100644 index 0000000..b07456d --- /dev/null +++ b/CakePHP/Sniffs/Operators/OperatorSpacingSniff.php @@ -0,0 +1,236 @@ + + * @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600) + * @copyright 2023 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence + */ +class OperatorSpacingSniff extends SquizOperatorSpacingSniff +{ + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register(): array + { + parent::register(); + + $targets = Tokens::COMPARISON_TOKENS; + $targets += Tokens::OPERATORS; + $targets += Tokens::ASSIGNMENT_TOKENS; + $targets += Tokens::BOOLEAN_OPERATORS; + $targets[] = T_INLINE_THEN; + $targets[] = T_INLINE_ELSE; + $targets[] = T_STRING_CONCAT; + $targets[] = T_INSTANCEOF; + + // Also register the contexts we want to specifically skip over. + $targets[] = T_DECLARE; + + return $targets; + } + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. + * @return int|void 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, int $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']; + } + + // Union/intersection type separators in catch statements must not be surrounded by spaces. + if ($this->isCatchTypeOperator($phpcsFile, $stackPtr) === true) { + $this->processCatchTypeOperator($phpcsFile, $stackPtr); + + return; + } + + if ($this->isOperator($phpcsFile, $stackPtr) === false) { + return; + } + + $operator = $tokens[$stackPtr]['content']; + + $checkBefore = true; + $checkAfter = true; + + // Skip short ternary. + if ( + $tokens[$stackPtr]['code'] === T_INLINE_ELSE && + $tokens[$stackPtr - 1]['code'] === T_INLINE_THEN + ) { + $checkBefore = false; + } + + // Skip operator with comment on previous line. + if ( + $tokens[$stackPtr - 1]['code'] === T_COMMENT && + $tokens[$stackPtr - 1]['line'] < $tokens[$stackPtr]['line'] + ) { + $checkBefore = false; + } + + if (isset($tokens[$stackPtr + 1]) === true) { + // Skip short ternary. + if ( + $tokens[$stackPtr]['code'] === T_INLINE_THEN && + $tokens[$stackPtr + 1]['code'] === T_INLINE_ELSE + ) { + $checkAfter = false; + } + } else { + // Skip partial files. + $checkAfter = false; + } + + if ($checkBefore === true && $tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) { + $error = 'Expected at least 1 space before "%s"; 0 found'; + $data = [$operator]; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data); + if ($fix === true) { + $phpcsFile->fixer->addContentBefore($stackPtr, ' '); + } + } + + if ($checkAfter === true && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) { + $error = 'Expected at least 1 space after "%s"; 0 found'; + $data = [$operator]; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfter', $data); + if ($fix === true) { + $phpcsFile->fixer->addContent($stackPtr, ' '); + } + } + } + + /** + * Checks that there is no space around a union/intersection type separator in a catch statement. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. + * @param int $stackPtr The position of the operator in + * the stack. + * @return void + */ + private function processCatchTypeOperator(File $phpcsFile, int $stackPtr): void + { + $tokens = $phpcsFile->getTokens(); + $operator = $tokens[$stackPtr]['content']; + + // Allow a newline instead of no space. + if ( + $tokens[$stackPtr - 1]['code'] === T_WHITESPACE && + $tokens[$stackPtr - 1]['line'] === $tokens[$stackPtr]['line'] + ) { + $error = 'Expected 0 spaces before "%s"; %s found'; + $data = [ + $operator, + $tokens[$stackPtr - 1]['length'], + ]; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBefore', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($stackPtr - 1, ''); + } + } + + if (isset($tokens[$stackPtr + 1]) === true) { + // Allow a newline instead of no space. + if ( + $tokens[$stackPtr + 1]['code'] === T_WHITESPACE && + $tokens[$stackPtr + 1]['line'] === $tokens[$stackPtr]['line'] + ) { + $error = 'Expected 0 spaces after "%s"; %s found'; + $data = [ + $operator, + $tokens[$stackPtr + 1]['length'], + ]; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfter', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); + } + } + } + } + + /** + * Checks if the token is a union/intersection type separator in a catch statement. + * + * The PHPCS tokenizer does not convert the type separators in catch statements + * to T_TYPE_UNION/T_TYPE_INTERSECTION, so they are still seen as bitwise operators. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. + * @param int $stackPtr The position of the operator in + * the stack. + * @return bool + */ + private function isCatchTypeOperator(File $phpcsFile, int $stackPtr): bool + { + $tokens = $phpcsFile->getTokens(); + + if ( + $tokens[$stackPtr]['code'] !== T_BITWISE_OR && + $tokens[$stackPtr]['code'] !== T_BITWISE_AND + ) { + return false; + } + + if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { + return false; + } + + foreach (array_keys($tokens[$stackPtr]['nested_parenthesis']) as $opener) { + if (isset($tokens[$opener]['parenthesis_owner']) === false) { + continue; + } + + $owner = $tokens[$opener]['parenthesis_owner']; + if ($tokens[$owner]['code'] === T_CATCH) { + return true; + } + } + + return false; + } +} diff --git a/CakePHP/Tests/Operators/OperatorSpacingUnitTest.inc b/CakePHP/Tests/Operators/OperatorSpacingUnitTest.inc new file mode 100644 index 0000000..4224256 --- /dev/null +++ b/CakePHP/Tests/Operators/OperatorSpacingUnitTest.inc @@ -0,0 +1,40 @@ + 2, + 29 => 2, + 33 => 2, + 34 => 1, + 35 => 1, + 37 => 2, + 38 => 1, + ]; + } + + /** + * @inheritDoc + */ + public function getWarningList() + { + return []; + } +} diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index 612d0ac..8c46c66 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -16,6 +16,11 @@ + + diff --git a/docs/README.md b/docs/README.md index 5d33759..b4a7007 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ The CakePHP standard contains 157 sniffs -CakePHP (19 sniffs) +CakePHP (20 sniffs) ------------------- - CakePHP.Classes.ReturnTypeHint - CakePHP.Commenting.DocBlockAlignment @@ -16,6 +16,7 @@ CakePHP (19 sniffs) - CakePHP.NamingConventions.ValidEnumName - CakePHP.NamingConventions.ValidFunctionName - CakePHP.NamingConventions.ValidTraitName +- CakePHP.Operators.OperatorSpacing - CakePHP.PHP.DisallowShortOpenTag - CakePHP.PHP.SingleQuote - CakePHP.WhiteSpace.EmptyLines @@ -75,7 +76,7 @@ PSR2 (9 sniffs) - PSR2.Methods.FunctionClosingBrace - PSR2.Methods.MethodDeclaration -PSR12 (17 sniffs) +PSR12 (16 sniffs) ----------------- - PSR12.Classes.AnonClassDeclaration - PSR12.Classes.ClassInstantiation @@ -91,7 +92,6 @@ PSR12 (17 sniffs) - PSR12.Functions.ReturnTypeDeclaration - PSR12.Keywords.ShortFormTypeKeywords - PSR12.Namespaces.CompoundNamespaceDepth -- PSR12.Operators.OperatorSpacing - PSR12.Properties.ConstantVisibility - PSR12.Traits.UseDeclaration