@@ -56,10 +56,25 @@ class FunctionCommentSniff implements Sniff
56
56
*/
57
57
public $ allowedTypes = [
58
58
'array ' ,
59
+ 'array-key ' ,
60
+ 'bool ' ,
61
+ 'callable ' ,
62
+ 'double ' ,
63
+ 'float ' ,
64
+ 'int ' ,
65
+ 'iterable ' ,
59
66
'mixed ' ,
60
67
'object ' ,
61
68
'resource ' ,
62
69
'callable ' ,
70
+ 'true ' ,
71
+ 'false ' ,
72
+ 'null ' ,
73
+ 'scalar ' ,
74
+ 'stdClass ' ,
75
+ '\stdClass ' ,
76
+ 'string ' ,
77
+ 'void ' ,
63
78
];
64
79
65
80
@@ -276,10 +291,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
276
291
}
277
292
}//end if
278
293
279
- if ($ type === 'void ' ) {
280
- $ error = 'If there is no return value for a function, there must not be a @return tag. ' ;
281
- $ phpcsFile ->addError ($ error , $ return , 'VoidReturn ' );
282
- } else if ($ type !== 'mixed ' ) {
294
+ if ($ type !== 'mixed ' && $ type !== 'void ' ) {
283
295
// If return type is not void, there needs to be a return statement
284
296
// somewhere in the function that returns something.
285
297
if (isset ($ tokens [$ stackPtr ]['scope_closer ' ]) === true ) {
@@ -747,7 +759,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
747
759
if (count ($ typeNames ) === 1 && $ typeName === $ suggestedName ) {
748
760
// Check type hint for array and custom type.
749
761
$ suggestedTypeHint = '' ;
750
- if (strpos ($ suggestedName , 'array ' ) !== false ) {
762
+ if (strpos ($ suggestedName , 'array ' ) !== false && $ suggestedName !== ' array-key ' ) {
751
763
$ suggestedTypeHint = 'array ' ;
752
764
} else if (strpos ($ suggestedName , 'callable ' ) !== false ) {
753
765
$ suggestedTypeHint = 'callable ' ;
@@ -762,7 +774,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
762
774
if ($ suggestedTypeHint !== '' && isset ($ realParams [$ checkPos ]) === true ) {
763
775
$ typeHint = $ realParams [$ checkPos ]['type_hint ' ];
764
776
// Primitive type hints are allowed to be omitted.
765
- if ($ typeHint === '' && in_array ($ suggestedTypeHint , [ ' string ' , ' int ' , ' float ' , ' bool ' ] ) === false ) {
777
+ if ($ typeHint === '' && in_array ($ suggestedTypeHint , $ this -> allowedTypes ) === false ) {
766
778
$ error = 'Type hint "%s" missing for %s ' ;
767
779
$ data = [
768
780
$ suggestedTypeHint ,
@@ -789,12 +801,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
789
801
) {
790
802
$ typeHint = $ realParams [$ checkPos ]['type_hint ' ];
791
803
if ($ typeHint !== ''
792
- && $ typeHint !== 'stdClass '
793
- && $ typeHint !== '\stdClass '
794
- // As of PHP 7.2, object is a valid type hint.
795
- && $ typeHint !== 'object '
796
- // As of PHP 8.0, mixed is a valid type hint.
797
- && $ typeHint !== 'mixed '
804
+ && in_array ($ typeHint , $ this ->allowedTypes ) === false
798
805
) {
799
806
$ error = 'Unknown type hint "%s" found for %s ' ;
800
807
$ data = [
@@ -1000,7 +1007,9 @@ public static function suggestType($type)
1000
1007
return $ type ;
1001
1008
}
1002
1009
1003
- $ type = preg_replace ('/[^a-zA-Z0-9_ \\\[\]]/ ' , '' , $ type );
1010
+ // Also allow "-" for special type hint "array-key" supported by PHPStan
1011
+ // https://phpstan.org/writing-php-code/phpdoc-types#basic-types .
1012
+ $ type = preg_replace ('/[^a-zA-Z0-9_ \\\[\]\-]/ ' , '' , $ type );
1004
1013
1005
1014
return $ type ;
1006
1015
0 commit comments