Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generics in docblock #3544

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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('/^(.*)<.*>$/', $suggestedName, $matches)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. For performance optimization, I'd recommend having this condition as the last condition in the if/elseif chain, not the first.
  2. I'd also suggest optimizing the regex a little more, like so:
    Suggested change
    if (preg_match('/^(.*)<.*>$/', $suggestedName, $matches)) {
    if (preg_match('/^([^<]+)<[^>]+>$/', $suggestedName, $matches)) {

$suggestedTypeHint = $matches[1];
} else if (strpos($suggestedName, 'array') !== false || substr($suggestedName, -2) === '[]') {
$suggestedTypeHint = 'array';
} else if (strpos($suggestedName, 'callable') !== false) {
$suggestedTypeHint = 'callable';
Expand Down
14 changes: 13 additions & 1 deletion src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc
Expand Up @@ -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<int, string> $values An object with int, string pairs.
*
* @return void
*/
public function specifiedArray1(Collection $values) {

}// end specifiedArray1()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line at end of file (also for fixed).

Expand Up @@ -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<int, string> $values An object with int, string pairs.
*
* @return void
*/
public function specifiedArray1(Collection $values) {

}// end specifiedArray1()