Skip to content

Commit 6b2edff

Browse files
authored
fix(FunctionComment): Allow complex array type for return comment (#3526462) (#267)
1 parent f606589 commit 6b2edff

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,18 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
258258
$typeNames = explode('|', $returnType);
259259
$suggestedNames = [];
260260
$hasNull = false;
261+
// Do not check PHPStan types that contain any kind of brackets.
262+
// See https://phpstan.org/writing-php-code/phpdoc-types#general-arrays .
263+
$isPhpstanType = preg_match('/[<\[\{\(]/', $returnType) === 1;
261264
foreach ($typeNames as $i => $typeName) {
262265
if (strtolower($typeName) === 'null') {
263266
$hasNull = true;
264267
}
265268

266269
$suggestedName = $this->suggestType($typeName);
267-
if (in_array($suggestedName, $suggestedNames, true) === false) {
270+
if (in_array($suggestedName, $suggestedNames, true) === false
271+
|| $isPhpstanType === true
272+
) {
268273
$suggestedNames[] = $suggestedName;
269274
}
270275
}

tests/Drupal/good/good.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,3 +2047,16 @@ interface BreadcrumbBuilderInterface {
20472047
public function applies(RouteMatchInterface $route_match /* , CacheableMetadata $cacheable_metadata */);
20482048

20492049
}
2050+
2051+
/**
2052+
* Test that nested array types are ok.
2053+
*
2054+
* @param array<array<scalar|null>|object|scalar|null> $param
2055+
* A complex nested array type.
2056+
*
2057+
* @return array<array<scalar|null>|object|scalar|null>
2058+
* An array of results.
2059+
*/
2060+
function pdo_weird_return_type($param) {
2061+
return pdo();
2062+
}

0 commit comments

Comments
 (0)