phpstan / phpstan-strict-rules

Extra strict and opinionated rules for PHPStan
MIT License
592 stars 46 forks source link

Overriding a vendor class member with imperfect type declaration: No way to not get errors #236

Closed levu42 closed 7 months ago

levu42 commented 7 months ago

When I have a vendor class that has a member /** @var array */ public $foo = []; and I want to override it, I can either also declare the variable as array, triggering a "has no value specified in iterable type array" error, or I can explicitly set the types of the items in the array, triggering a type mismatch error:

https://phpstan.org/r/9a6aa272-c73a-43ab-a781-f735a68cf24c

ondrejmirtes commented 7 months ago

For fixing wrong 3rd party PHPDocs there's a feature called Stub files: https://phpstan.org/user-guide/stub-files (so you can do that to set the original property as array<int, string>.

If you just want to type this in your code:

/** @var array */
public $foo = ['bar'];

Without getting " has no value type specified in iterable type array." then you should use mixed[] instead of array.

levu42 commented 7 months ago

@ondrejmirtes Thanks for the quick reply - @var mixed[] did the trick :)

github-actions[bot] commented 6 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.