Fix phpstan/phpstan#13247: iterable is equal to array|Traversable#5144
Conversation
|
I made bunch of test, i ran multiple times the bot on the issue. |
src/Type/IterableType.php
Outdated
| new GenericObjectType(Traversable::class, [ | ||
| $this->keyType, | ||
| $this->itemType, |
There was a problem hiding this comment.
I think this makes sense as we did a similar thing here
src/Type/UnionType.php
Outdated
| return $this->accepts(new UnionType([ | ||
| new ArrayType($type->getIterableKeyType(), $type->getIterableValueType()), | ||
| new GenericObjectType(Traversable::class, [ | ||
| $type->getIterableKeyType(), | ||
| $type->getIterableValueType(), | ||
| ]), | ||
| ]), $strictTypes); |
There was a problem hiding this comment.
maybe we could/should have a IterableType->toTraversable() method (name to be discussed), so we don't repeat this logic in so many places?
There was a problem hiding this comment.
I added toTraversable
There was a problem hiding this comment.
I was not precise enough. I thought about a method which turns iterable into array|Traversable
There was a problem hiding this comment.
Then should it be toArrayOrTraversable ?
Because toTraversable is unclear (and should be the method I introduced).
Also, the current ToTraversable can be used in the tryRemove method
There was a problem hiding this comment.
Yeah. So maybe add another method
There was a problem hiding this comment.
I simplified and just added toArrayOrTraversable for now.
| @@ -138,9 +138,9 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult | |||
| if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) { | |||
There was a problem hiding this comment.
this case here is guarded also on IntersectionType. does it mean we need a similar fix you do on UnionType->accepts() also in IntersectionType->accepts()?
There was a problem hiding this comment.
This was added by the bot as a fix in a95e598 in order to fix
Parameter #1 $input of function Bug13247\as_array expects array<int, int>|Traversable<int, int>, iterable<int, int> given.
I don't have in mind something where an IntersectionType make sens ; but feel free to provide some case I'll add to my tests.
29af204 to
54cae34
Compare
- Fixed IterableType::isSubTypeOf() to use GenericObjectType for the Traversable part of the decomposition instead of IntersectionType with plain ObjectType - The plain ObjectType(Traversable) lost generic type parameters, causing subtype checks against GenericObjectType(Traversable, [K, V]) to return maybe instead of yes - Consistent with how IterableType::tryRemove() already decomposes iterable - New regression test in tests/PHPStan/Rules/Functions/data/bug-13247.php
54cae34 to
0201f7a
Compare
…phpstan#5144) Co-authored-by: VincentLanglet <[email protected]> Co-authored-by: Vincent Langlet <[email protected]>
Fixes phpstan/phpstan#13247
I merged this strategy with the previous one #5143