Resolve every constant entity type ID in getStorage() return type - #1045
Merged
Conversation
Return null to fall back to the declared return type instead of eagerly computing it via ParametersAcceptorSelector, resolve every constant string in a union instead of only the first, and drop the Concat and MethodCall AST sniffing that the constant-strings check already subsumes. Covers getStorage($cond ? 'node' : 'user') with a proper union type now. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
EntityStorageType now compares by entity type ID in isSuperTypeOf(), equals(), and the cache-level description, so a union such as NodeStorage|SqlContentEntityStorage no longer collapses to the base class. EntityStorageDynamicReturnTypeExtension resolves each union member on its own, so load(), loadMultiple(), and create() on a union storage return every branch instead of the first registered match. An unknown entity type ID anywhere in the union falls back to the declared return type instead of tagging the interface with an ID. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
mglaman
force-pushed
the
audit/2b-getstorage-union
branch
from
September 9, 2026 15:24
b024323 to
c5277e7
Compare
mglaman
marked this pull request as ready for review
September 9, 2026 15:25
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.
Split out of #1027. Type inference improvement, no configuration change.
What changed
getStorage()now resolves every constant string in its argument, sogetStorage($cond ? 'node' : 'user')infersNodeStorage|UserStorageinstead of silently using the first string. Chained calls on that union return every branch:->load(1)isNode|User|null,->loadMultiple()isarray<int, Node|User>, and->create([])isNode|User. Narrowing withinstanceofon a member still works.Three pieces make the union sound:
EntityStorageTypecompares by entity type ID.isSuperTypeOf(),equals(), and the cache-level description treat two storages with different entity type IDs as distinct even when they share a class. Without this,NodeStorage|SqlContentEntityStoragecollapsed to the base class and the node branch was lost. User-facing descriptions are unchanged.EntityStorageDynamicReturnTypeExtensionresolves each union member on its own instead of askingresolveFromStorage()for the first registered match.getStorage($cond ? 'node' : 'not_an_entity_type')isEntityStorageInterface. Before, an unknown ID produced an interface type tagged with the ID, which served no consumer.The
Concat/MethodCallAST sniffing is gone;Scope::getType()already folds constant expressions. The extension declares?Typeand returnsnullto fall back, and no longer computes the declared return type eagerly on every call.Known display quirk
Two config entity types that both use
ConfigEntityStoragedescribe asConfigEntityStorage#1|ConfigEntityStorage#2. The#Nsuffix is PHPStan's own rendering for union members whose names collide. They are different storages, and the chained result (Block|ConfigEntityUsingDefaultStorage) reads normally.Testing
Fixture cases cover: the union on
getStorage()itself, chainedload(),loadMultiple(), andcreate()in both operand orders, two content storages sharingSqlContentEntityStorage, two config storages sharingConfigEntityStorage, a content and config storage together (array<int|string, Block|Node>), a known plus unknown ID, a fully dynamic ID, a first-class callable, andinstanceofnarrowing. Full suite, self-analysis, and phpcs are green.🤖 Generated with Claude Code