diff --git a/src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php b/src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php index 291c68e..6330263 100644 --- a/src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php +++ b/src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php @@ -58,9 +58,9 @@ private function processAnnotation(File $phpcsFile, int $lineNumber, string $ann } } - if (\str_contains($value, '[]')) { + if (\preg_match('/\b[a-zA-Z_\\\\]+\[\]/', $value) || \preg_match('/\b(?!array\b)[a-zA-Z_\\\\]+\s*[\{\[]/', $value) || \preg_match('/\barray\s*\{\s*(?:\d+\s*[:=]|[^"\']+\s*(?:,|\}))/', $value)) { $phpcsFile->addErrorOnLine( - 'Please use vector type annotation for arrays.', + 'Please use generic types or DTOs instead of short array or shape-like annotations.', $lineNumber, ErrorCodes::ARRAYS_DOC_VECTOR ); diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniffTest.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniffTest.php index ee4babb..a1c0fa1 100644 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniffTest.php +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniffTest.php @@ -41,16 +41,29 @@ public static function provideDataSet(): array 'source' => 'FiveLab.Commenting.Annotations.Prohibited', ], [ - 'message' => 'Please use vector type annotation for arrays.', + 'message' => 'Please use generic types or DTOs instead of short array or shape-like annotations.', 'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector', + 'line' => 25, ], [ - 'message' => 'Please use vector type annotation for arrays.', + 'message' => 'Please use generic types or DTOs instead of short array or shape-like annotations.', 'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector', + 'line' => 26, ], [ - 'message' => 'Please use vector type annotation for arrays.', + 'message' => 'Please use generic types or DTOs instead of short array or shape-like annotations.', 'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector', + 'line' => 28, + ], + [ + 'message' => 'Please use generic types or DTOs instead of short array or shape-like annotations.', + 'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector', + 'line' => 35, + ], + [ + 'message' => 'Please use generic types or DTOs instead of short array or shape-like annotations.', + 'source' => 'FiveLab.Commenting.Annotations.ArraysDocVector', + 'line' => 36, ], ], ]; diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/success.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/success.php index 6109f8d..43da921 100644 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/success.php +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/success.php @@ -29,6 +29,7 @@ function bar(int $a, string $b): int * @param array $c * * @return array + * @return array{"foo": int, "bar": string} */ function baz(array $a, array $b, array $c): array { diff --git a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/wrong.php b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/wrong.php index 9ccaed2..b1dcb42 100644 --- a/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/wrong.php +++ b/tests/PhpCs/FiveLab/Sniffs/Commenting/Resources/annotations/wrong.php @@ -30,3 +30,11 @@ function bar(): int function baz(array $a, array $b, array $c): array { } + +/** + * @param SomeObject{} $a + * @param string{1: int} $b + */ +function bar(array $a, array $b, array $c): array +{ +}