Skip to content

Commit 4ee6a35

Browse files
authored
feat(FunctionComment): Allow PHPStan array shapes in doc data types (#3253472)
1 parent 95052fc commit 4ee6a35

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
385385
if ($fix === true) {
386386
$phpcsFile->fixer->replaceToken(($return + 2), $matches[1]);
387387
}
388-
} else {
388+
389+
// Do not check PHPStan array shapes from
390+
// https://phpstan.org/writing-php-code/phpdoc-types#general-arrays .
391+
} else if (strpos($type, 'array') === false) {
389392
$error = 'Return type "%s" must not contain spaces';
390393
$data = [$type];
391394
$phpcsFile->addError($error, $return, 'ReturnTypeSpaces', $data);
@@ -729,9 +732,13 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
729732
}
730733

731734
if (preg_match('/\s/', $param['type']) === 1) {
732-
$error = 'Parameter type "%s" must not contain spaces';
733-
$data = [$param['type']];
734-
$phpcsFile->addError($error, $param['tag'], 'ParamTypeSpaces', $data);
735+
// Do not check PHPStan array shapes from
736+
// https://phpstan.org/writing-php-code/phpdoc-types#general-arrays .
737+
if (strpos($param['type'], 'array') === false) {
738+
$error = 'Parameter type "%s" must not contain spaces';
739+
$data = [$param['type']];
740+
$phpcsFile->addError($error, $param['tag'], 'ParamTypeSpaces', $data);
741+
}
735742
} else if ($param['type'] !== $suggestedType) {
736743
$error = 'Expected "%s" but found "%s" for parameter type';
737744
$data = [
@@ -745,7 +752,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
745752
$content .= $param['var'];
746753
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
747754
}
748-
}
755+
}//end if
749756

750757
$suggestedName = '';
751758
$typeName = '';
@@ -1007,9 +1014,9 @@ public static function suggestType($type)
10071014
return $type;
10081015
}
10091016

1010-
// Also allow "-" for special type hint "array-key" supported by PHPStan
1017+
// Also allow "-" and "<>" for special type hints supported by PHPStan
10111018
// https://phpstan.org/writing-php-code/phpdoc-types#basic-types .
1012-
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-]/', '', $type);
1019+
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-<>]/', '', $type);
10131020

10141021
return $type;
10151022

tests/Drupal/Commenting/FunctionCommentUnitTest.inc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,62 @@ function test_return_void() {
655655
*/
656656
function test_return_void2(): void {
657657
}
658+
659+
/**
660+
* PHPStan: General arrays.
661+
*
662+
* @param Type[] $param1
663+
* Parameter.
664+
* @param array<Type> $param2
665+
* Parameter.
666+
* @param array<int, Type> $param3
667+
* Parameter.
668+
* @param non-empty-array<Type> $param4
669+
* Parameter.
670+
* @param non-empty-array<int, Type> $param5
671+
* Parameter.
672+
*
673+
* @see https://phpstan.org/writing-php-code/phpdoc-types#general-arrays
674+
*/
675+
function test_arrays(array $param1, array $param2, array $param3, array $param4, array $param5) {
676+
}
677+
678+
/**
679+
* @return Type[]
680+
* Square brackets.
681+
*/
682+
function test_return_type_array(): array {
683+
return [];
684+
}
685+
686+
/**
687+
* @return array<Type>
688+
* Arrow brackets.
689+
*/
690+
function test_return_arrow_array(): array {
691+
return [];
692+
}
693+
694+
/**
695+
* @return array<int, Type>
696+
* Keyed array.
697+
*/
698+
function test_return_keyed_array(): array {
699+
return [];
700+
}
701+
702+
/**
703+
* @return non-empty-array<Type>
704+
* Non empty array with type.
705+
*/
706+
function test_return_non_empty_array(): array {
707+
return [new Type()];
708+
}
709+
710+
/**
711+
* @return non-empty-array<int, Type>
712+
* Non empty keyed array with type.
713+
*/
714+
function test_return_non_empty_keyed_array(): array {
715+
return [0 => new Type()];
716+
}

tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,62 @@ function test_return_void() {
681681
*/
682682
function test_return_void2(): void {
683683
}
684+
685+
/**
686+
* PHPStan: General arrays.
687+
*
688+
* @param Type[] $param1
689+
* Parameter.
690+
* @param array<Type> $param2
691+
* Parameter.
692+
* @param array<int, Type> $param3
693+
* Parameter.
694+
* @param non-empty-array<Type> $param4
695+
* Parameter.
696+
* @param non-empty-array<int, Type> $param5
697+
* Parameter.
698+
*
699+
* @see https://phpstan.org/writing-php-code/phpdoc-types#general-arrays
700+
*/
701+
function test_arrays(array $param1, array $param2, array $param3, array $param4, array $param5) {
702+
}
703+
704+
/**
705+
* @return Type[]
706+
* Square brackets.
707+
*/
708+
function test_return_type_array(): array {
709+
return [];
710+
}
711+
712+
/**
713+
* @return array<Type>
714+
* Arrow brackets.
715+
*/
716+
function test_return_arrow_array(): array {
717+
return [];
718+
}
719+
720+
/**
721+
* @return array<int, Type>
722+
* Keyed array.
723+
*/
724+
function test_return_keyed_array(): array {
725+
return [];
726+
}
727+
728+
/**
729+
* @return non-empty-array<Type>
730+
* Non empty array with type.
731+
*/
732+
function test_return_non_empty_array(): array {
733+
return [new Type()];
734+
}
735+
736+
/**
737+
* @return non-empty-array<int, Type>
738+
* Non empty keyed array with type.
739+
*/
740+
function test_return_non_empty_keyed_array(): array {
741+
return [0 => new Type()];
742+
}

0 commit comments

Comments
 (0)