Skip to content
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();
}