Repository navigation
Conversation
delacry
added this pull request to stack #58
October 10, 2026 12:10
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
delacry
force-pushed
the
fix/immutable-map-widening
branch
from
October 10, 2026 12:17
0e15bf5 to
5919bbf
Compare
A value typed as a concrete map (ImmutableHashMap, MutableHashMap, or a class using the map logic traits) resolves its methods from the traits, whose docblocks diverged from the map interfaces. put(), putFirst(), putIfAbsent(), putAll() and putAllPairs() overrode the ImmutableMap interface's widening NK/NV with the map's own K/V, so PHPStan rejected a put the interface allows. They now inherit the interface docblock. A class that rebuilds itself through newMapOf() and validates its entries keeps them strict with @method; the extending and upgrade guides show how, and the extending fixture does it. containsKey(), containsValue() and remove() narrowed the key or value to K or V, while every map interface accepts any; they inherit the interfaces now too. putIfAbsent() on an immutable IntMap or StringMap threw InvalidKeyTypeException for a key of another type, while put() falls back to a hash map. Both classes now override it like put() and its siblings. The FAQ, the map guide and their class docblocks said every foreign key throws, which only the mutable variants do. toArray() was typed array<array-key, V> on every map, mutable ones included, losing the key type the interface keeps. Inheriting the interface's conditional is not enough: with a still generic K, as on ImmutableHashMap, PHPStan falls back to a bare array, so the trait repeats it.
delacry
force-pushed
the
fix/immutable-map-widening
branch
from
October 13, 2026 10:58
5919bbf to
d2914b0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #56, so merge that one first. After it's merged, I'll rebase this onto
0.2.xand retarget it.This fixes the same problem as #56, but for maps. On a concrete map class, some trait docblocks didn't match the map interfaces:
It also fixes a runtime bug:
intMapOf([1 => 'a'])->putIfAbsent('b', 'x')threwInvalidKeyTypeException, whileput()falls back to a hash map.putIfAbsent()on immutable IntMap and StringMap now falls back the same way.Docs:
put()strict with@method.UPGRADE-0.2.mdis updated.The new
tests/Type/Map/ConcreteClassTypeTest.phpand theputIfAbsent_rejects_*tests fail without this change.