phpstan
phpstan copied to clipboard
checkMissingIterableValueType: suggest value when possible
Feature request
Hi, we have tons of easy-to-inspect array return type like in the test data providers:
public function provideData(): array
{
return [
[1, 'foo'],
[2, 'bar'],
];
}
Manually writing value types for array return type is hard, tedious and error-prone, and in the case for PHPStan would be easy to suggest array<int, array<mixed>>. Would it be possible to integrate such suggestion in the related error?
Hi, it doesn't make much sense to require array values in unit tests data providers as PHPStan doesn't take advantage of the types in any way. I'd recommend you to ignore this error for your data provider methods :)
You are right, data providers are a bad example, but not the only one. I'm asking this feature because as soon as you write an erroneous format, PHPStan already suggests you the correct one:
https://phpstan.org/r/011c06f5-1c1c-406a-9c48-73b40f688cb5
So it should be straightforward to suggest it even when it's missing, isn't it?
@Slamdunk After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
@@ @@
-8: Function provideData() should return array<int, int> but returns array<int, array<int, int|string>>.
+8: Function provideData() should return array<int, int> but returns array<int, list<int|string>>.
Full report
| Line | Error |
|---|---|
| 8 | `Function provideData() should return array<int, int> but returns array<int, list<int |
For the record, seems like rector worked on this feature https://getrector.com/blog/rector-22-new-rules-for-array-docblocks