Skip to content

fix(FunctionComment): Allow complex array type for return comment (#3526462) #267

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

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -258,13 +258,18 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
$typeNames = explode('|', $returnType);
$suggestedNames = [];
$hasNull = false;
// Do not check PHPStan types that contain any kind of brackets.
// See https://phpstan.org/writing-php-code/phpdoc-types#general-arrays .
$isPhpstanType = preg_match('/[<\[\{\(]/', $returnType) === 1;
foreach ($typeNames as $i => $typeName) {
if (strtolower($typeName) === 'null') {
$hasNull = true;
}

$suggestedName = $this->suggestType($typeName);
if (in_array($suggestedName, $suggestedNames, true) === false) {
if (in_array($suggestedName, $suggestedNames, true) === false
|| $isPhpstanType === true
) {
$suggestedNames[] = $suggestedName;
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -2047,3 +2047,16 @@ interface BreadcrumbBuilderInterface {
public function applies(RouteMatchInterface $route_match /* , CacheableMetadata $cacheable_metadata */);

}

/**
* Test that nested array types are ok.
*
* @param array<array<scalar|null>|object|scalar|null> $param
* A complex nested array type.
*
* @return array<array<scalar|null>|object|scalar|null>
* An array of results.
*/
function pdo_weird_return_type($param) {
return pdo();
}