From 25ac06fd0f93a4b9fcb58767017a192402cd790e Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Tue, 8 Feb 2022 00:31:59 +0000 Subject: [PATCH 1/6] Support generics in docblock --- .../Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index ba3e1710f0..2f4e7211fe 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,7 +419,9 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { + if (preg_match('/^(.*)<.*>$/', $suggestedTypeHint, $matches)) { + $suggestedTypeHint = $matches[1]; + } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; } else if (strpos($suggestedName, 'callable') !== false) { $suggestedTypeHint = 'callable'; From 44f96a275444d7d6e62bdcdf6ca2fb8990b174c6 Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Tue, 8 Feb 2022 18:58:57 +0000 Subject: [PATCH 2/6] add unit test --- .../Sniffs/Commenting/FunctionCommentSniff.php | 2 +- .../Tests/Commenting/FunctionCommentUnitTest.inc | 14 +++++++++++++- .../Commenting/FunctionCommentUnitTest.inc.fixed | 14 +++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 2f4e7211fe..ee6b76b22f 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,7 +419,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (preg_match('/^(.*)<.*>$/', $suggestedTypeHint, $matches)) { + if (preg_match('/^(.*)<.*>$/', $suggestedName, $matches)) { $suggestedTypeHint = $matches[1]; } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc index 4f59f60b71..926d9847fa 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1042,7 +1042,19 @@ public function ignored() { // phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct -/** +/** * @return void * @throws Exception If any other error occurs. */ function throwCommentOneLine() {} + +/** + * Using generic as a type hint should satisfy a specified object parameter type. + * @see https://phpstan.org/blog/generics-in-php-using-phpdocs + * + * @param Collection $values An object with int, string pairs. + * + * @return void + */ +public function specifiedArray1(Collection $values) { + +}// end specifiedArray1() \ No newline at end of file diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 21a4103eb5..3b21fb1965 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1042,7 +1042,19 @@ public function ignored() { // phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct -/** +/** * @return void * @throws Exception If any other error occurs. */ function throwCommentOneLine() {} + +/** + * Using generic as a type hint should satisfy a specified object parameter type. + * @see https://phpstan.org/blog/generics-in-php-using-phpdocs + * + * @param Collection $values An object with int, string pairs. + * + * @return void + */ +public function specifiedArray1(Collection $values) { + +}// end specifiedArray1() \ No newline at end of file From ac4b5e0c2257b809d1ee9fe51cf8d54cf5aa9bc4 Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Thu, 10 Feb 2022 17:54:55 +0000 Subject: [PATCH 3/6] update per PR --- .../Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 6 +++--- .../Squiz/Tests/Commenting/FunctionCommentUnitTest.inc | 4 ++-- .../Tests/Commenting/FunctionCommentUnitTest.inc.fixed | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index ee6b76b22f..e0862e3859 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,14 +419,14 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (preg_match('/^(.*)<.*>$/', $suggestedName, $matches)) { - $suggestedTypeHint = $matches[1]; - } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { + if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; } else if (strpos($suggestedName, 'callable') !== false) { $suggestedTypeHint = 'callable'; } else if (strpos($suggestedName, 'callback') !== false) { $suggestedTypeHint = 'callable'; + } else if (preg_match('/^([^<]+)<[^>]+>$/', $suggestedName, $matches)) { + $suggestedTypeHint = $matches[1]; } else if (in_array($suggestedName, Common::$allowedTypes, true) === false) { $suggestedTypeHint = $suggestedName; } diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc index 926d9847fa..dd964dc226 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1055,6 +1055,6 @@ function throwCommentOneLine() {} * * @return void */ -public function specifiedArray1(Collection $values) { +public function genericType(Collection $values) { -}// end specifiedArray1() \ No newline at end of file +}// end genericType() diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 3b21fb1965..ffb67e8c9a 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1055,6 +1055,6 @@ function throwCommentOneLine() {} * * @return void */ -public function specifiedArray1(Collection $values) { +public function genericType(Collection $values) { -}// end specifiedArray1() \ No newline at end of file +}// end genericType() From 4afb939c312be2e6d2ab175b3169d4f86074c3c4 Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Tue, 8 Feb 2022 00:31:59 +0000 Subject: [PATCH 4/6] Support generics in docblock --- .../Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index ba3e1710f0..2f4e7211fe 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,7 +419,9 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { + if (preg_match('/^(.*)<.*>$/', $suggestedTypeHint, $matches)) { + $suggestedTypeHint = $matches[1]; + } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; } else if (strpos($suggestedName, 'callable') !== false) { $suggestedTypeHint = 'callable'; From 9684318ec74c20d5159ebe50f15fa5fb4fd8712f Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Tue, 8 Feb 2022 18:58:57 +0000 Subject: [PATCH 5/6] add unit test --- .../Sniffs/Commenting/FunctionCommentSniff.php | 2 +- .../Tests/Commenting/FunctionCommentUnitTest.inc | 14 +++++++++++++- .../Commenting/FunctionCommentUnitTest.inc.fixed | 14 +++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 2f4e7211fe..ee6b76b22f 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,7 +419,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (preg_match('/^(.*)<.*>$/', $suggestedTypeHint, $matches)) { + if (preg_match('/^(.*)<.*>$/', $suggestedName, $matches)) { $suggestedTypeHint = $matches[1]; } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc index 4f59f60b71..926d9847fa 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1042,7 +1042,19 @@ public function ignored() { // phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct -/** +/** * @return void * @throws Exception If any other error occurs. */ function throwCommentOneLine() {} + +/** + * Using generic as a type hint should satisfy a specified object parameter type. + * @see https://phpstan.org/blog/generics-in-php-using-phpdocs + * + * @param Collection $values An object with int, string pairs. + * + * @return void + */ +public function specifiedArray1(Collection $values) { + +}// end specifiedArray1() \ No newline at end of file diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 21a4103eb5..3b21fb1965 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1042,7 +1042,19 @@ public function ignored() { // phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct -/** +/** * @return void * @throws Exception If any other error occurs. */ function throwCommentOneLine() {} + +/** + * Using generic as a type hint should satisfy a specified object parameter type. + * @see https://phpstan.org/blog/generics-in-php-using-phpdocs + * + * @param Collection $values An object with int, string pairs. + * + * @return void + */ +public function specifiedArray1(Collection $values) { + +}// end specifiedArray1() \ No newline at end of file From a155e9b7cf1f70f0707b0b7881bde173f429e0c2 Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Thu, 10 Feb 2022 17:54:55 +0000 Subject: [PATCH 6/6] update per PR --- .../Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 6 +++--- .../Squiz/Tests/Commenting/FunctionCommentUnitTest.inc | 4 ++-- .../Tests/Commenting/FunctionCommentUnitTest.inc.fixed | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index ee6b76b22f..e0862e3859 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -419,14 +419,14 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) // Check type hint for array and custom type. $suggestedTypeHint = ''; - if (preg_match('/^(.*)<.*>$/', $suggestedName, $matches)) { - $suggestedTypeHint = $matches[1]; - } else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { + if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') { $suggestedTypeHint = 'array'; } else if (strpos($suggestedName, 'callable') !== false) { $suggestedTypeHint = 'callable'; } else if (strpos($suggestedName, 'callback') !== false) { $suggestedTypeHint = 'callable'; + } else if (preg_match('/^([^<]+)<[^>]+>$/', $suggestedName, $matches)) { + $suggestedTypeHint = $matches[1]; } else if (in_array($suggestedName, Common::$allowedTypes, true) === false) { $suggestedTypeHint = $suggestedName; } diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc index 926d9847fa..dd964dc226 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1055,6 +1055,6 @@ function throwCommentOneLine() {} * * @return void */ -public function specifiedArray1(Collection $values) { +public function genericType(Collection $values) { -}// end specifiedArray1() \ No newline at end of file +}// end genericType() diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 3b21fb1965..ffb67e8c9a 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1055,6 +1055,6 @@ function throwCommentOneLine() {} * * @return void */ -public function specifiedArray1(Collection $values) { +public function genericType(Collection $values) { -}// end specifiedArray1() \ No newline at end of file +}// end genericType()