From 0bdfa273015bfb1f7ad58f5d4c7a77076045992d Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Tue, 6 Jan 2026 15:08:23 +0100 Subject: [PATCH 01/17] feat(OperatorSpacing): Add perCompatible config options to not check union type operator spacing in catch() statements --- CHANGELOG-4.x.md | 5 ++++ .../Operators/OperatorSpacingStandard.xml | 21 ++++++++++++++++ .../Sniffs/Operators/OperatorSpacingSniff.php | 24 +++++++++++++++++++ .../Operators/OperatorSpacingUnitTest.4.inc | 15 ++++++++++++ .../OperatorSpacingUnitTest.4.inc.fixed | 15 ++++++++++++ .../Operators/OperatorSpacingUnitTest.php | 4 ++++ 6 files changed, 84 insertions(+) create mode 100644 src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc create mode 100644 src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed diff --git a/CHANGELOG-4.x.md b/CHANGELOG-4.x.md index 843eb3dc40..c2ba0b5ccd 100644 --- a/CHANGELOG-4.x.md +++ b/CHANGELOG-4.x.md @@ -2,6 +2,11 @@ The file documents changes to the PHP_CodeSniffer project for the 4.x series of releases. +## [Unreleased] + +### Added +- PSR12.Operators.OperatorSpacing: new `perCompatible` property to support PER-CS 2.0. Issue #660. + ## [4.0.1] - 2025-11-10 This release includes all improvements and bugfixes from PHP_CodeSniffer [3.13.5]. diff --git a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml index 981b1d97a1..c5ea56b02c 100644 --- a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml +++ b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml @@ -20,6 +20,27 @@ if ($a===$b) { $foo=$bar??$a??$b; } elseif ($a>$b) { $variable=$foo?'foo':'bar'; +} + ]]> + + + + + + + + + + + diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 824169438a..34fb241220 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -17,6 +17,13 @@ class OperatorSpacingSniff extends SquizOperatorSpacingSniff { + /** + * The PER version to be compatible with. + * + * @var string + */ + public $perCompatible = '1.0'; + /** * Returns an array of tokens this test wants to listen for. @@ -78,6 +85,23 @@ public function process(File $phpcsFile, int $stackPtr) $checkBefore = true; $checkAfter = true; + // PER-CS 2.0: Exception to the rule for catch blocks (union types) where no space is required. + // As union types didn't exist when PSR-12 was created, the pipe in catch statements + // was originally treated as a bitwise operator. This check disables the spacing requirement + // for that specific case when opting in to PER-CS 2.0 or higher. + if ($operator === '|' && version_compare($this->perCompatible, '2.0', '>=') === true) { + if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { + $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); + $bracket = array_pop($parenthesis); + if (isset($tokens[$bracket]['parenthesis_owner']) === true + && $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH + ) { + $checkBefore = false; + $checkAfter = false; + } + } + } + // Skip short ternary. if ($tokens[($stackPtr)]['code'] === T_INLINE_ELSE && $tokens[($stackPtr - 1)]['code'] === T_INLINE_THEN diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc new file mode 100644 index 0000000000..482a9877da --- /dev/null +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc @@ -0,0 +1,15 @@ + 2, 47 => 2, ]; + case 'OperatorSpacingUnitTest.4.inc': + return [ + 14 => 2, + ]; default: return []; } From 2326ce96acd08d6ab3c33ae6eb8ac26017c853bb Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Tue, 6 Jan 2026 15:23:11 +0100 Subject: [PATCH 02/17] revert changelog changes, they are probably done later --- CHANGELOG-4.x.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CHANGELOG-4.x.md b/CHANGELOG-4.x.md index c2ba0b5ccd..843eb3dc40 100644 --- a/CHANGELOG-4.x.md +++ b/CHANGELOG-4.x.md @@ -2,11 +2,6 @@ The file documents changes to the PHP_CodeSniffer project for the 4.x series of releases. -## [Unreleased] - -### Added -- PSR12.Operators.OperatorSpacing: new `perCompatible` property to support PER-CS 2.0. Issue #660. - ## [4.0.1] - 2025-11-10 This release includes all improvements and bugfixes from PHP_CodeSniffer [3.13.5]. From c8e133c0a42e0c26acd3e5bc2ac4b7fe5eb63d6c Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Tue, 6 Jan 2026 17:42:15 +0100 Subject: [PATCH 03/17] Fix PER version to 3.0, add fixer with no space for 3.0 --- .../Operators/OperatorSpacingStandard.xml | 8 ++-- .../Sniffs/Operators/OperatorSpacingSniff.php | 47 +++++++++++++++---- .../Operators/OperatorSpacingUnitTest.4.inc | 12 ++++- .../OperatorSpacingUnitTest.4.inc.fixed | 10 +++- .../Operators/OperatorSpacingUnitTest.php | 4 +- 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml index c5ea56b02c..93185572de 100644 --- a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml +++ b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml @@ -26,21 +26,21 @@ if ($a===$b) { - + - + diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 34fb241220..2c29156056 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -18,7 +18,7 @@ class OperatorSpacingSniff extends SquizOperatorSpacingSniff { /** - * The PER version to be compatible with. + * The PER version to be compatible with. For backwards compatibility this is set to 1.0 by default. * * @var string */ @@ -82,26 +82,53 @@ public function process(File $phpcsFile, int $stackPtr) $operator = $tokens[$stackPtr]['content']; - $checkBefore = true; - $checkAfter = true; - - // PER-CS 2.0: Exception to the rule for catch blocks (union types) where no space is required. + // PER-CS 3.0: Exception to the rule for catch blocks (union types) where no space is required. // As union types didn't exist when PSR-12 was created, the pipe in catch statements - // was originally treated as a bitwise operator. This check disables the spacing requirement - // for that specific case when opting in to PER-CS 2.0 or higher. - if ($operator === '|' && version_compare($this->perCompatible, '2.0', '>=') === true) { + // was originally treated as a bitwise operator. This check changes the spacing requirement + // for that specific case when opting in to PER-CS 3.0 or higher. + if ($operator === '|' && version_compare($this->perCompatible, '3.0', '>=') === true) { if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); $bracket = array_pop($parenthesis); if (isset($tokens[$bracket]['parenthesis_owner']) === true && $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH ) { - $checkBefore = false; - $checkAfter = false; + if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { + $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 ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { + $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), ''); + } + } + + // Now that this special case is handled, we can return early as we don't need to do + // further checks. + return; } } } + $checkBefore = true; + $checkAfter = true; + // Skip short ternary. if ($tokens[($stackPtr)]['code'] === T_INLINE_ELSE && $tokens[($stackPtr - 1)]['code'] === T_INLINE_THEN diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc index 482a9877da..32937ef302 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc @@ -1,10 +1,18 @@ 2, + 7 => 2, + 15 => 2, + 22 => 2, ]; default: return []; From 697d04903bb76ea070c7ed68e2872ea118e4b163 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 09:54:59 +0100 Subject: [PATCH 04/17] Update src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml index 93185572de..4c30500b07 100644 --- a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml +++ b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml @@ -26,7 +26,7 @@ if ($a===$b) { From af1a05e3b7fe9a36557c7dfa32297aeb580c28b3 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 09:55:22 +0100 Subject: [PATCH 05/17] Update src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml index 4c30500b07..0bd9486769 100644 --- a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml +++ b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml @@ -30,7 +30,7 @@ if ($a===$b) { ]]> - + Date: Fri, 9 Jan 2026 09:56:55 +0100 Subject: [PATCH 06/17] Update src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml index 0bd9486769..dd30c3bd8f 100644 --- a/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml +++ b/src/Standards/PSR12/Docs/Operators/OperatorSpacingStandard.xml @@ -37,7 +37,7 @@ try { } ]]> - + Date: Fri, 9 Jan 2026 10:19:17 +0100 Subject: [PATCH 07/17] Expanded test cases --- .../OperatorSpacingUnitTest.1.inc.fixed | 6 +- .../Operators/OperatorSpacingUnitTest.4.inc | 74 +++++++++++++++++++ .../OperatorSpacingUnitTest.4.inc.fixed | 74 +++++++++++++++++++ .../Operators/OperatorSpacingUnitTest.php | 13 ++-- 4 files changed, 159 insertions(+), 8 deletions(-) diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index 0f52f1cf74..c2b9d3ecc8 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -1,5 +1,5 @@ $b) { $variable = $foo ? 'foo' : 'bar'; @@ -15,8 +15,8 @@ $foo = $a >> $b; function foo(&$a,& $b) {} -$foo = $a and $b; -$foo = $a or $b; +$foo = $a and $b; +$foo = $a or $b; $foo = $a xor $b; $foo = !$a; $foo = $a && $b; diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc index 32937ef302..c492ac8e29 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc @@ -2,11 +2,48 @@ // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 +// Valid. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + |RuntimeException + |KlausiException + |AnotherException + |ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception| + RuntimeException| + KlausiException| + AnotherException| + ItNeverEndsException $e +) { +} + +// Invalid becasue of one space. try { // nothing } catch (Exception | RuntimeException $e) { } +// Invalid because of multiple spaces. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + // Testing that it works with future versions as well. // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 4.0 @@ -17,7 +54,44 @@ try { // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 1.0 +// Valid. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + | RuntimeException + | KlausiException + | AnotherException + | ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception | + RuntimeException | + KlausiException | + AnotherException | + ItNeverEndsException $e +) { +} + +// Invalid becasue of no space. try { // nothing } catch (Exception|RuntimeException $e) { } + +// Invalid because of multiple spaces. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed index c005c70ce5..21450ac85e 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed @@ -2,6 +2,43 @@ // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 +// Valid. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + |RuntimeException + |KlausiException + |AnotherException + |ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception| + RuntimeException| + KlausiException| + AnotherException| + ItNeverEndsException $e +) { +} + +// Invalid becasue of one space. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Invalid because of multiple spaces. try { // nothing } catch (Exception|RuntimeException $e) { @@ -17,6 +54,43 @@ try { // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 1.0 +// Valid. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + | RuntimeException + | KlausiException + | AnotherException + | ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception | + RuntimeException | + KlausiException | + AnotherException | + ItNeverEndsException $e +) { +} + +// Invalid becasue of no space. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Invalid because of multiple spaces. try { // nothing } catch (Exception | RuntimeException $e) { diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index 4acd31d3e3..767fed9560 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -36,7 +36,7 @@ public function getErrorList($testFile = '') switch ($testFile) { case 'OperatorSpacingUnitTest.1.inc': return [ - 2 => 1, + 2 => 2, 3 => 2, 4 => 1, 5 => 2, @@ -46,7 +46,8 @@ public function getErrorList($testFile = '') 11 => 3, 13 => 3, 14 => 2, - 18 => 1, + 18 => 2, + 19 => 1, 20 => 1, 22 => 2, 23 => 2, @@ -59,9 +60,11 @@ public function getErrorList($testFile = '') ]; case 'OperatorSpacingUnitTest.4.inc': return [ - 7 => 2, - 15 => 2, - 22 => 2, + 38 => 2, + 44 => 2, + 52 => 2, + 90 => 2, + 96 => 2, ]; default: return []; From 16febedde09f82d71bbe812f7bd5c631db3e67a4 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 10:40:35 +0100 Subject: [PATCH 08/17] fix multiline handling --- .../Sniffs/Operators/OperatorSpacingSniff.php | 69 ++++++++++--------- .../OperatorSpacingUnitTest.1.inc.fixed | 6 +- .../Operators/OperatorSpacingUnitTest.4.inc | 8 +-- .../OperatorSpacingUnitTest.4.inc.fixed | 6 +- .../Operators/OperatorSpacingUnitTest.php | 7 +- 5 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 2c29156056..10049fc358 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -86,43 +86,48 @@ public function process(File $phpcsFile, int $stackPtr) // As union types didn't exist when PSR-12 was created, the pipe in catch statements // was originally treated as a bitwise operator. This check changes the spacing requirement // for that specific case when opting in to PER-CS 3.0 or higher. - if ($operator === '|' && version_compare($this->perCompatible, '3.0', '>=') === true) { - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { - $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); - $bracket = array_pop($parenthesis); - if (isset($tokens[$bracket]['parenthesis_owner']) === true - && $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH + if ($operator === '|' && version_compare($this->perCompatible, '3.0', '>=') === true + && isset($tokens[$stackPtr]['nested_parenthesis']) === true + ) { + $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); + $bracket = array_pop($parenthesis); + if (isset($tokens[$bracket]['parenthesis_owner']) === true + && $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH + ) { + if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE + && strpos($tokens[($stackPtr - 1)]['content'], $phpcsFile->eolChar) === false + && $tokens[($stackPtr - 1)]['column'] !== 1 ) { - if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { - $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), ''); - } + $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 ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { - $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), ''); - } + if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE + && strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false + ) { + $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), ''); } - - // Now that this special case is handled, we can return early as we don't need to do - // further checks. - return; } + + // Now that this special case is handled, we can return early as we don't need to do + // further checks. + return; } } diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index c2b9d3ecc8..0f52f1cf74 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -1,5 +1,5 @@ $b) { $variable = $foo ? 'foo' : 'bar'; @@ -15,8 +15,8 @@ $foo = $a >> $b; function foo(&$a,& $b) {} -$foo = $a and $b; -$foo = $a or $b; +$foo = $a and $b; +$foo = $a or $b; $foo = $a xor $b; $foo = !$a; $foo = $a && $b; diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc index c492ac8e29..30350ac8f3 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc @@ -84,14 +84,14 @@ try { ) { } -// Invalid becasue of no space. +// Valid because PSR 12 allows more than one space around operators. try { // nothing -} catch (Exception|RuntimeException $e) { +} catch (Exception | RuntimeException $e) { } -// Invalid because of multiple spaces. +// Invalid because of no space. try { // nothing -} catch (Exception | RuntimeException $e) { +} catch (Exception|RuntimeException $e) { } diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed index 21450ac85e..b4588dd3fc 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed @@ -84,13 +84,13 @@ try { ) { } -// Invalid becasue of no space. +// Valid because PSR 12 allows more than one space around operators. try { // nothing -} catch (Exception | RuntimeException $e) { +} catch (Exception | RuntimeException $e) { } -// Invalid because of multiple spaces. +// Invalid because of no space. try { // nothing } catch (Exception | RuntimeException $e) { diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index 767fed9560..29b5e4e634 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -36,7 +36,7 @@ public function getErrorList($testFile = '') switch ($testFile) { case 'OperatorSpacingUnitTest.1.inc': return [ - 2 => 2, + 2 => 1, 3 => 2, 4 => 1, 5 => 2, @@ -46,8 +46,7 @@ public function getErrorList($testFile = '') 11 => 3, 13 => 3, 14 => 2, - 18 => 2, - 19 => 1, + 18 => 1, 20 => 1, 22 => 2, 23 => 2, @@ -60,7 +59,7 @@ public function getErrorList($testFile = '') ]; case 'OperatorSpacingUnitTest.4.inc': return [ - 38 => 2, + 38 => 2, 44 => 2, 52 => 2, 90 => 2, From 41ef9188147d0577c8fb962a2bb2fcc3f0310ae7 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:03:02 +0100 Subject: [PATCH 09/17] Apply suggestions from code review Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 10049fc358..033851b66b 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -82,7 +82,7 @@ public function process(File $phpcsFile, int $stackPtr) $operator = $tokens[$stackPtr]['content']; - // PER-CS 3.0: Exception to the rule for catch blocks (union types) where no space is required. + // PER-CS 3.0: Exception to the rule for pipe operators in multi-catch blocks where no space is required. // As union types didn't exist when PSR-12 was created, the pipe in catch statements // was originally treated as a bitwise operator. This check changes the spacing requirement // for that specific case when opting in to PER-CS 3.0 or higher. From 44632ed3a5c670e3452e8d6b982974db89e87656 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:02:04 +0100 Subject: [PATCH 10/17] Moved tests to 1 test file --- .../Operators/OperatorSpacingUnitTest.1.inc | 94 ++++++++++++++++++ .../OperatorSpacingUnitTest.1.inc.fixed | 94 ++++++++++++++++++ .../Operators/OperatorSpacingUnitTest.4.inc | 97 ------------------- .../OperatorSpacingUnitTest.4.inc.fixed | 97 ------------------- .../Operators/OperatorSpacingUnitTest.php | 12 +-- 5 files changed, 192 insertions(+), 202 deletions(-) delete mode 100644 src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc delete mode 100644 src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc.fixed diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc index 14cf8e9dfd..77970f1a55 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc @@ -77,3 +77,97 @@ function setDefault(#[ImportValue( } declare(strict_types=1); + +// Valid. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + | RuntimeException + | KlausiException + | AnotherException + | ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception | + RuntimeException | + KlausiException | + AnotherException | + ItNeverEndsException $e +) { +} + +// Valid because PSR 12 allows more than one space around operators. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Invalid because of no space. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 + +// Valid. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + |RuntimeException + |KlausiException + |AnotherException + |ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception| + RuntimeException| + KlausiException| + AnotherException| + ItNeverEndsException $e +) { +} + +// Invalid becasue of one space. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Invalid because of multiple spaces. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Testing that it works with future versions as well. +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 4.0 + +try { + // nothing +} catch (Exception | RuntimeException $e) { +} diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index 0f52f1cf74..cecc23d189 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -77,3 +77,97 @@ function setDefault(#[ImportValue( } declare(strict_types=1); + +// Valid. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + | RuntimeException + | KlausiException + | AnotherException + | ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception | + RuntimeException | + KlausiException | + AnotherException | + ItNeverEndsException $e +) { +} + +// Valid because PSR 12 allows more than one space around operators. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// Invalid because of no space. +try { + // nothing +} catch (Exception | RuntimeException $e) { +} + +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 + +// Valid. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception + |RuntimeException + |KlausiException + |AnotherException + |ItNeverEndsException $e +) { +} + +// Valid because multiline formatting is undefined. +try { + // nothing +} catch ( + Exception| + RuntimeException| + KlausiException| + AnotherException| + ItNeverEndsException $e +) { +} + +// Invalid becasue of one space. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Invalid because of multiple spaces. +try { + // nothing +} catch (Exception|RuntimeException $e) { +} + +// Testing that it works with future versions as well. +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 4.0 + +try { + // nothing +} catch (Exception|RuntimeException $e) { +} diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc deleted file mode 100644 index 30350ac8f3..0000000000 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.4.inc +++ /dev/null @@ -1,97 +0,0 @@ - 1, 44 => 2, 47 => 2, - ]; - case 'OperatorSpacingUnitTest.4.inc': - return [ - 38 => 2, - 44 => 2, - 52 => 2, - 90 => 2, - 96 => 2, + 120 => 2, + 158 => 2, + 164 => 2, + 172 => 2, ]; default: return []; From c048e0085aaf352e1f1093fe5a5fbaa626d9d553 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:07:40 +0100 Subject: [PATCH 11/17] fix coding standards --- .../Operators/OperatorSpacingUnitTest.php | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index 6dcb3c0876..7019a5fb26 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -36,26 +36,26 @@ public function getErrorList($testFile = '') 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, + 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, 120 => 2, 158 => 2, 164 => 2, From 3c00f7b992d8f2f965bbd958fdadbed63887e47b Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:14:20 +0100 Subject: [PATCH 12/17] remove duplicate test case --- .../PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc | 8 +------- .../Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed | 8 +------- .../PSR12/Tests/Operators/OperatorSpacingUnitTest.php | 5 ++--- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc index 77970f1a55..25e0bd7103 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc @@ -114,12 +114,6 @@ try { } catch (Exception | RuntimeException $e) { } -// Invalid because of no space. -try { - // nothing -} catch (Exception|RuntimeException $e) { -} - // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 // Valid. @@ -152,7 +146,7 @@ try { ) { } -// Invalid becasue of one space. +// Invalid because of one space. try { // nothing } catch (Exception | RuntimeException $e) { diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index cecc23d189..f780cc6761 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -114,12 +114,6 @@ try { } catch (Exception | RuntimeException $e) { } -// Invalid because of no space. -try { - // nothing -} catch (Exception | RuntimeException $e) { -} - // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 // Valid. @@ -152,7 +146,7 @@ try { ) { } -// Invalid becasue of one space. +// Invalid because of one space. try { // nothing } catch (Exception|RuntimeException $e) { diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index 7019a5fb26..21963420a5 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -56,10 +56,9 @@ public function getErrorList($testFile = '') 40 => 1, 44 => 2, 47 => 2, - 120 => 2, + 152 => 2, 158 => 2, - 164 => 2, - 172 => 2, + 166 => 2, ]; default: return []; From e124d49c4234b3b46ba3d4e19501741d75808fd8 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:18:03 +0100 Subject: [PATCH 13/17] use token code for comparison --- src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 033851b66b..bb118c0d42 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -86,8 +86,9 @@ public function process(File $phpcsFile, int $stackPtr) // As union types didn't exist when PSR-12 was created, the pipe in catch statements // was originally treated as a bitwise operator. This check changes the spacing requirement // for that specific case when opting in to PER-CS 3.0 or higher. - if ($operator === '|' && version_compare($this->perCompatible, '3.0', '>=') === true + if ($tokens[$stackPtr]['code'] === T_BITWISE_OR && isset($tokens[$stackPtr]['nested_parenthesis']) === true + && version_compare($this->perCompatible, '3.0', '>=') === true ) { $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); $bracket = array_pop($parenthesis); From f8bb902ba46b5da417448e07b8eac2ef7af39fc3 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:32:02 +0100 Subject: [PATCH 14/17] fix array_keys() call --- .../PSR12/Sniffs/Operators/OperatorSpacingSniff.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index bb118c0d42..97da83ff2d 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -90,8 +90,11 @@ public function process(File $phpcsFile, int $stackPtr) && isset($tokens[$stackPtr]['nested_parenthesis']) === true && version_compare($this->perCompatible, '3.0', '>=') === true ) { - $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']); - $bracket = array_pop($parenthesis); + // Calling array_keys() on a nested sub-array can have memory leaks, assign to a variable first. + // See https://github.com/squizlabs/PHP_CodeSniffer/pull/2273 . + $parenthesis = $tokens[$stackPtr]['nested_parenthesis']; + $parenthesisKeys = array_keys($parenthesis); + $bracket = array_pop($parenthesisKeys); if (isset($tokens[$bracket]['parenthesis_owner']) === true && $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH ) { From ee3659914ea98392dda69e018aed800a2ed3b7b1 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 9 Jan 2026 11:36:32 +0100 Subject: [PATCH 15/17] unique error code --- .../PSR12/Sniffs/Operators/OperatorSpacingSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 97da83ff2d..898f1a0e8d 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -102,13 +102,13 @@ public function process(File $phpcsFile, int $stackPtr) && strpos($tokens[($stackPtr - 1)]['content'], $phpcsFile->eolChar) === false && $tokens[($stackPtr - 1)]['column'] !== 1 ) { - $error = 'Expected 0 spaces before "%s"; %s found'; + $error = 'Expected 0 spaces before "%s" in multi-catch statement; %s found'; $data = [ $operator, $tokens[($stackPtr - 1)]['length'], ]; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBefore', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiCatchSpaceBefore', $data); if ($fix === true) { $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); } @@ -117,13 +117,13 @@ public function process(File $phpcsFile, int $stackPtr) if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE && strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false ) { - $error = 'Expected 0 spaces after "%s"; %s found'; + $error = 'Expected 0 spaces after "%s" in multi-catch statement; %s found'; $data = [ $operator, $tokens[($stackPtr + 1)]['length'], ]; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfter', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiCatchSpaceAfter', $data); if ($fix === true) { $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); } From 60f819380806571ea8e263a83792889adbbdcb48 Mon Sep 17 00:00:00 2001 From: bigdevlarry Date: Fri, 9 Jan 2026 14:47:38 +0000 Subject: [PATCH 16/17] PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase with colon and bracket is unclear --- .../Sniffs/ControlStructures/SwitchDeclarationSniff.php | 8 ++++++-- .../ControlStructures/SwitchDeclarationUnitTest.inc | 9 +++++++++ .../SwitchDeclarationUnitTest.inc.fixed | 9 +++++++++ .../ControlStructures/SwitchDeclarationUnitTest.php | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php index df12e7622a..3118942eeb 100644 --- a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php @@ -197,8 +197,12 @@ public function process(File $phpcsFile, int $stackPtr) } } } else { - // Probably a case/default statement with colon + curly braces. - $phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type); + if ($tokens[$opener]['code'] === T_OPEN_CURLY_BRACKET) { + $error = '%s statements must not use a braced block after the colon'; + $phpcsFile->addError($error, $nextCase, 'WrongOpener', [strtoupper($type)]); + } else { + $phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type); + } } } diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc index 54438e81f7..d4ca3a0cfa 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc @@ -638,3 +638,12 @@ switch ($foo):
Some html
1, 634 => 1, 637 => 1, + 644 => 1, ]; } From 83c46600a053363cecd6fc0e8028a6f35a6aa11d Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 25 Jan 2026 17:20:45 +0100 Subject: [PATCH 17/17] expanded test coverage --- .../Operators/OperatorSpacingUnitTest.1.inc | 43 +++++++++++++++++++ .../OperatorSpacingUnitTest.1.inc.fixed | 43 +++++++++++++++++++ .../Operators/OperatorSpacingUnitTest.php | 10 +++-- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc index 25e0bd7103..3437e49078 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc @@ -114,6 +114,26 @@ try { } catch (Exception | RuntimeException $e) { } +// Valid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Valid usage of bitwise OR operator between function definition parentheses. +function testFunction($flags = E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR|E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between function definition parentheses. +function testFunction2($flags = E_ERROR|E_DEPRECATED) { + // do something +} + // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 // Valid. @@ -158,6 +178,26 @@ try { } catch (Exception | RuntimeException $e) { } +// Valid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Valid usage of bitwise OR operator between function definition parentheses. +function testFunction3($flags = E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR|E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between function definition parentheses. +function testFunction4($flags = E_ERROR|E_DEPRECATED) { + // do something +} + // Testing that it works with future versions as well. // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 4.0 @@ -165,3 +205,6 @@ try { // nothing } catch (Exception | RuntimeException $e) { } + +// Reset property back to its default value. +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 1.0 diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed index f780cc6761..8b59208f83 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.1.inc.fixed @@ -114,6 +114,26 @@ try { } catch (Exception | RuntimeException $e) { } +// Valid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Valid usage of bitwise OR operator between function definition parentheses. +function testFunction($flags = E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between function definition parentheses. +function testFunction2($flags = E_ERROR | E_DEPRECATED) { + // do something +} + // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 3.0 // Valid. @@ -158,6 +178,26 @@ try { } catch (Exception|RuntimeException $e) { } +// Valid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Valid usage of bitwise OR operator between function definition parentheses. +function testFunction3($flags = E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between if parentheses. +if ($error === E_ERROR | E_DEPRECATED) { + // do something +} + +// Invalid usage of bitwise OR operator between function definition parentheses. +function testFunction4($flags = E_ERROR | E_DEPRECATED) { + // do something +} + // Testing that it works with future versions as well. // phpcs:set PSR12.Operators.OperatorSpacing perCompatible 4.0 @@ -165,3 +205,6 @@ try { // nothing } catch (Exception|RuntimeException $e) { } + +// Reset property back to its default value. +// phpcs:set PSR12.Operators.OperatorSpacing perCompatible 1.0 diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php index 21963420a5..cb0ca60db9 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php @@ -56,9 +56,13 @@ public function getErrorList($testFile = '') 40 => 1, 44 => 2, 47 => 2, - 152 => 2, - 158 => 2, - 166 => 2, + 128 => 2, + 133 => 2, + 172 => 2, + 178 => 2, + 192 => 2, + 197 => 2, + 206 => 2, ]; default: return [];