phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

checkMissingIterableValueType: suggest value when possible

Open Slamdunk opened this issue 5 years ago • 3 comments
trafficstars

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?

Slamdunk avatar Jan 28 '20 14:01 Slamdunk

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 :)

ondrejmirtes avatar Jan 28 '20 14:01 ondrejmirtes

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 avatar Jan 29 '20 06:01 Slamdunk

@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

phpstan-bot avatar Oct 06 '24 14:10 phpstan-bot

For the record, seems like rector worked on this feature https://getrector.com/blog/rector-22-new-rules-for-array-docblocks

VincentLanglet avatar Oct 07 '25 21:10 VincentLanglet