From 2bd19c2e6edb918ab14d87f1c4b37d1f112d8f00 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 22 Mar 2021 08:51:39 +1100 Subject: [PATCH] Fixed bug #3277 : Nullable static return typehint causes whitespace error --- package.xml | 1 + .../Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php | 7 +++++++ .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc | 5 +++++ .../WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed | 5 +++++ .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php | 10 +++++----- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/package.xml b/package.xml index f9575986dd..28c55d4579 100644 --- a/package.xml +++ b/package.xml @@ -143,6 +143,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed bug #3258 : Squiz.Formatting.OperatorBracket duplicate error messages for unary minus -- Thanks to Juliette Reinders Folmer for the patch - Fixed bug #3273 : Squiz.Functions.FunctionDeclarationArgumentSpacing reports line break as 0 spaces between parenthesis + - Fixed bug #3277 : Nullable static return typehint causes whitespace error diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php index 8c5b89446f..ad995dc45b 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php @@ -66,6 +66,13 @@ public function process(File $phpcsFile, $stackPtr) return; } + if ($prevToken !== false + && $tokens[$prevToken]['code'] === T_NULLABLE + ) { + // Not a scope keyword, but a return type. + return; + } + if ($prevToken !== false && $tokens[$prevToken]['code'] === T_COLON ) { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc index d8f21c4e1d..2213817116 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc @@ -89,6 +89,11 @@ public static function fCreate($attributes = []): static return static::factory()->create($attributes); } +public static function fCreate($attributes = []): ?static +{ + return static::factory()->create($attributes); +} + // Also account for static used within union types. public function fCreate($attributes = []): object|static { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed index daae2bca38..e642f0c7b3 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed @@ -84,6 +84,11 @@ public static function fCreate($attributes = []): static return static::factory()->create($attributes); } +public static function fCreate($attributes = []): ?static +{ + return static::factory()->create($attributes); +} + // Also account for static used within union types. public function fCreate($attributes = []): object|static { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php index 9b91298755..de4697c0cb 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php @@ -38,12 +38,12 @@ public function getErrorList() 64 => 1, 67 => 1, 71 => 1, - 98 => 1, - 101 => 1, + 103 => 1, 106 => 1, - 114 => 1, - 116 => 1, - 122 => 2, + 111 => 1, + 119 => 1, + 121 => 1, + 127 => 2, ]; }//end getErrorList()