vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 660 forks source link

Enforce specifying array value type #10287

Open rieschl opened 1 year ago

rieschl commented 1 year ago

When having an array parameter or return type, psalm does not check if there is a type of the values specified in the docblock. It would be nice to enforce specifying a type (or multiple types) of the array values.

For example:

/**
 * @param array $data
 * @return array
 */
function arrayShapes(array $data): array {
    if (count($data) > 1) {
        return ['bar'];
    }
    return [2];
}

(code snippet in the playground) This should emit errors that no array type is specified, just like PHPStan does (playground) with its "No value type specified in iterable type" rule.

Do you think this is something that could be added to psalm? Thank you for your help!

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/7d42144f5e ```php 1) { return ['bar']; } return [2]; } ``` ``` Psalm output (using commit 67d6468): No issues! ```
weirdan commented 8 months ago

As a workaround you can use https://github.com/slevomat/coding-standard/blob/master/doc/type-hints.md#slevomatcodingstandardtypehintsparametertypehint- and https://github.com/slevomat/coding-standard/blob/master/doc/type-hints.md#slevomatcodingstandardtypehintsreturntypehint-