From 0f2ba2ed47bcb3c55a81f351388e597efa972d53 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 25 May 2025 12:39:49 +0200 Subject: [PATCH] Squiz/ScopeKeywordSpacing: update for properties with asymmetric visibility Prevent false negatives for the "incorrect space after keyword" error. Includes tests. --- .../Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php | 5 +++-- .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc | 10 ++++++++++ .../WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed | 10 ++++++++++ .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php | 3 +++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php index abf05dc17e..7c3ce113d7 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php @@ -24,8 +24,9 @@ class ScopeKeywordSpacingSniff implements Sniff */ public function register() { - $register = Tokens::$methodPrefixes; - $register[] = T_READONLY; + $register = Tokens::$methodPrefixes; + $register += Tokens::$scopeModifiers; + $register[T_READONLY] = T_READONLY; return $register; }//end register() diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc index de53edcaf0..e53511f8f9 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc @@ -192,3 +192,13 @@ class FinalProperties { final readonly public ?MyType $spacing_correct; protected final $spacing_incorrect = 'foo'; } + +class AsymVisibility { + public(set) string $asymPublic = 'hello'; + public protected(set) final $asymProtected = 'hello'; + private(set) public string|false $asymPrivate = 'hello'; + + public public(set) $asymPublicPublic = 'hello'; + protected(set) public $asymPublicProtected = 'hello'; + protected private(set) $asymProtectedPrivate = 'hello'; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed index 55d1a5492d..8376b5604d 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed @@ -185,3 +185,13 @@ class FinalProperties { final readonly public ?MyType $spacing_correct; protected final $spacing_incorrect = 'foo'; } + +class AsymVisibility { + public(set) string $asymPublic = 'hello'; + public protected(set) final $asymProtected = 'hello'; + private(set) public string|false $asymPrivate = 'hello'; + + public public(set) $asymPublicPublic = 'hello'; + protected(set) public $asymPublicProtected = 'hello'; + protected private(set) $asymProtectedPrivate = 'hello'; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php index 64b82e365e..2d20694b7f 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php @@ -70,6 +70,9 @@ public function getErrorList($testFile='') 187 => 1, 188 => 1, 193 => 2, + 197 => 1, + 198 => 3, + 199 => 2, ]; case 'ScopeKeywordSpacingUnitTest.3.inc':