LazyCollection::filter caused template invariant TValue issue. #1811
Unanswered
SyuTingSong
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I encountered an issue related to template co/invariance of LazyCollection.
Here is the example code:
PHPStan reports an error:
What a confused message!
I searched and read discussion #1697. I understand why you guys use a stub file to overwrite the
@template-covariant TValue
declaration of laravel's Collection. But that's not helpful in my case. I didn't see any change ofTValue
when I just adding a filter function to exclude some elements from the LazyCollection.I tried to use
PHPStan\dumpType
to see the calculated type of whatLazyCollection::filter()
returns, and I gotSeems that the
CollectionFilterDynamicReturnTypeExtension
add an accessory typehasOffsetValue(0, string)
to the intersection types of the originalTValue
. The originalTValue
islist<string>
and the newTValue
returned byfilter
becomelist<string>&hasOffsetValue(0, string)
. Since theTValue
ofLazyCollection
is invariant, it can't be used as the return type ofMyCsvReader::read
.I tried to re-declare the return type of
read
method, but failed. ThehasOffsetValue(0,string)
is an internal accessory type and cannot be defined in phpdoc.I also tried to use
@var
to hint phpstan the return type isLazyCollection<int,list<string>>
. I failed with:Do you have any suggestions about this issue?
Beta Was this translation helpful? Give feedback.
All reactions