From 1f73efedf9ac55f927c139970024b06624c85ad0 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sun, 1 Feb 2026 11:50:32 +0100 Subject: [PATCH] Fix containsTypeArray to recognize array shape syntax containsTypeArray() only checked for `array<` and `[]` patterns but not `array{` shape syntax. This caused false positives in DocBlockParamAllowDefaultValue where `array{key: type}` was not recognized as satisfying the `array` typehint requirement. --- PhpCollective/Traits/CommentingTrait.php | 2 +- .../DocBlockParamAllowDefaultValueSniffTest.php | 2 +- tests/_data/DocBlockParamAllowDefaultValue/after.php | 8 ++++++++ tests/_data/DocBlockParamAllowDefaultValue/before.php | 8 ++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/PhpCollective/Traits/CommentingTrait.php b/PhpCollective/Traits/CommentingTrait.php index 57ff3bb..fa2270f 100644 --- a/PhpCollective/Traits/CommentingTrait.php +++ b/PhpCollective/Traits/CommentingTrait.php @@ -183,7 +183,7 @@ protected function hasInheritDoc(File $phpcsFile, $docBlockStartIndex, $docBlock protected function containsTypeArray(array $docBlockTypes, string $iterableType = 'array'): bool { foreach ($docBlockTypes as $docBlockType) { - if (str_contains($docBlockType, '[]') || str_starts_with($docBlockType, $iterableType . '<')) { + if (str_contains($docBlockType, '[]') || str_starts_with($docBlockType, $iterableType . '<') || str_starts_with($docBlockType, $iterableType . '{')) { return true; } } diff --git a/tests/PhpCollective/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniffTest.php b/tests/PhpCollective/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniffTest.php index 77a7828..af70bea 100644 --- a/tests/PhpCollective/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniffTest.php +++ b/tests/PhpCollective/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniffTest.php @@ -17,7 +17,7 @@ class DocBlockParamAllowDefaultValueSniffTest extends TestCase */ public function testDocBlockConstSniffer(): void { - $this->assertSnifferFindsErrors(new DocBlockParamAllowDefaultValueSniff(), 4); + $this->assertSnifferFindsErrors(new DocBlockParamAllowDefaultValueSniff(), 5); } /** diff --git a/tests/_data/DocBlockParamAllowDefaultValue/after.php b/tests/_data/DocBlockParamAllowDefaultValue/after.php index a1424fe..a34b335 100644 --- a/tests/_data/DocBlockParamAllowDefaultValue/after.php +++ b/tests/_data/DocBlockParamAllowDefaultValue/after.php @@ -77,4 +77,12 @@ public function toPHP(mixed $value): mixed { public function multipleUnion(int|string|null $value): void { } + + /** + * @param array{msgid: string, msgid_plural: (string | null)}|null $array + * @return void + */ + public function arrayShape(array $array = null): void + { + } } diff --git a/tests/_data/DocBlockParamAllowDefaultValue/before.php b/tests/_data/DocBlockParamAllowDefaultValue/before.php index fe80e3c..2abb19a 100644 --- a/tests/_data/DocBlockParamAllowDefaultValue/before.php +++ b/tests/_data/DocBlockParamAllowDefaultValue/before.php @@ -77,4 +77,12 @@ public function toPHP(mixed $value): mixed { public function multipleUnion(int|string|null $value): void { } + + /** + * @param array{msgid: string, msgid_plural: string|null} $array + * @return void + */ + public function arrayShape(array $array = null): void + { + } }