Skip to content

Commit f2df52c

Browse files
authored
feat(VariableComment): Allow more complex PHPStan type annotations (#3355543)
1 parent d5911f8 commit f2df52c

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,12 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
687687
$checkPos++;
688688
}
689689

690+
// Support variadic arguments.
691+
if (preg_match('/(\s+)\.{3}$/', $param['type'], $matches) === 1) {
692+
$param['type_space'] = strlen($matches[1]);
693+
$param['type'] = preg_replace('/\s+\.{3}$/', '', $param['type']);
694+
}
695+
690696
// Check the param type value. This could also be multiple parameter
691697
// types separated by '|'.
692698
$typeNames = explode('|', $param['type']);
@@ -697,12 +703,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
697703

698704
$suggestedType = implode('|', $suggestedNames);
699705

700-
// Support variadic arguments.
701-
if (preg_match('/(\s+)\.{3}$/', $param['type'], $matches) === 1) {
702-
$param['type_space'] = strlen($matches[1]);
703-
$param['type'] = preg_replace('/\s+\.{3}$/', '', $param['type']);
704-
}
705-
706706
if (preg_match('/\s/', $param['type']) === 1) {
707707
// Do not check PHPStan types that contain any kind of brackets.
708708
// See https://phpstan.org/writing-php-code/phpdoc-types#general-arrays .
@@ -920,9 +920,10 @@ public static function suggestType($type)
920920
return $type;
921921
}
922922

923-
// Also allow "-" and "<>" for special type hints supported by PHPStan
923+
// Also allow some more characters for special type hints supported by
924+
// PHPStan:
924925
// https://phpstan.org/writing-php-code/phpdoc-types#basic-types .
925-
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-<>]/', '', $type);
926+
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-<> ,"\{\}\?\':\*]/', '', $type);
926927

927928
return $type;
928929

tests/Drupal/Commenting/FunctionCommentUnitTest.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ function test25() {
305305
/**
306306
* Type declarations should not have any illegal characters.
307307
*
308-
* @param \Ille*al $var
309-
* This asterisk does not belong here.
308+
* @param \Ille§al $var
309+
* This character does not belong here.
310310
*
311311
* @return \Some/Namespace
312312
* This should not have a forward slash.

tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function test25() {
318318
* Type declarations should not have any illegal characters.
319319
*
320320
* @param \Illeal $var
321-
* This asterisk does not belong here.
321+
* This character does not belong here.
322322
*
323323
* @return \SomeNamespace
324324
* This should not have a forward slash.

tests/Drupal/Commenting/VariableCommentUnitTest.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,25 @@ class Test {
6363
*/
6464
protected readonly string $readOnly;
6565

66+
/**
67+
* PHPStan array annotation.
68+
*
69+
* @var array<int,array<int,int>>
70+
*/
71+
protected array $arrayStructure;
72+
73+
/**
74+
* PHPStan array shapes.
75+
*
76+
* @var array{'foo': int, "bar"?: string}
77+
*/
78+
protected array $arrayShape;
79+
80+
/**
81+
* PHPStan constant wildcard.
82+
*
83+
* @var Foo::*
84+
*/
85+
protected string $constant;
86+
6687
}

tests/Drupal/Commenting/VariableCommentUnitTest.inc.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,25 @@ class Test {
6767
*/
6868
protected readonly string $readOnly;
6969

70+
/**
71+
* PHPStan array annotation.
72+
*
73+
* @var array<int,array<int,int>>
74+
*/
75+
protected array $arrayStructure;
76+
77+
/**
78+
* PHPStan array shapes.
79+
*
80+
* @var array{'foo': int, "bar"?: string}
81+
*/
82+
protected array $arrayShape;
83+
84+
/**
85+
* PHPStan constant wildcard.
86+
*
87+
* @var Foo::*
88+
*/
89+
protected string $constant;
90+
7091
}

0 commit comments

Comments
 (0)