Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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).

Original file line number Diff line number Diff line change
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()