Skip raw generic ObjectType method/property prototype when GenericObjectType ancestor is present in IntersectionType#5678
Closed
phpstan-bot wants to merge 1 commit into
Conversation
…ObjectType` ancestor is present in `IntersectionType` - When an IntersectionType contains both a raw ObjectType for a generic class (e.g. Datagrid) and a GenericObjectType of its ancestor (e.g. DatagridInterface<Proxy>), skip the raw type's method/property prototype contribution if the ancestor also declares the member - The raw ObjectType resolves templates to their bounds (e.g. ProxyQueryInterface), while the GenericObjectType ancestor resolves them to the specific type args (e.g. Proxy). Intersecting these two invariant generic return types produces `never`, which is incorrect - Applied the same fix to instance property, static property, and method prototype resolution in IntersectionType - Methods unique to the raw type (not declared on the ancestor) are still resolved normally Closes phpstan/phpstan#5357
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.
Summary
When an intersection type like
Datagrid&DatagridInterface<Proxy>is formed (e.g. viaassert($x instanceof Datagrid)on aDatagridInterface<Proxy>), calling a method likegetPager()previously returned*NEVER*instead of the expectedPagerInterface<Proxy>.Root cause
IntersectionTypeMethodReflection::getVariants()intersects return types from all types in the intersection that provide the method. When a raw (unparameterized)ObjectTypefor a generic class is part of the intersection alongside aGenericObjectTypeof its ancestor:ObjectType('Datagrid')resolves its template parameters to their bounds (e.g.T → ProxyQueryInterface), sogetPager()returnsPagerInterface<ProxyQueryInterface>GenericObjectType('DatagridInterface', [Proxy])resolves templates to the specific type args, sogetPager()returnsPagerInterface<Proxy>TypeCombinator::intersect(PagerInterface<ProxyQueryInterface>, PagerInterface<Proxy>)returnsneverbecausePagerInterfaceis invariant inTandProxyQueryInterface !== ProxyThe underlying method is the same (inherited from
DatagridInterface), so intersecting two different resolutions of the same method's return type is incorrect.Changes
src/Type/IntersectionType.php:getUnresolvedMethodPrototype(): skip the raw ObjectType's prototype when a GenericObjectType ancestor that also declares the method is present in the intersectiongetUnresolvedInstancePropertyPrototype(): same fix for instance propertiesgetUnresolvedStaticPropertyPrototype(): same fix for static propertieshasGenericAncestorWithMethodInIntersection()andhasGenericAncestorWithPropertyInIntersection()that check whether a raw generic ObjectType has a GenericObjectType ancestor in the intersection that also declares the member being accessedphpstan-baseline.neon: added baseline entries for theinstanceofchecks on internal IntersectionType members (these are valid internal checks on concrete type components)Analogous cases probed:
Test
Added
tests/PHPStan/Analyser/nsrt/bug-5357.phpwith assertions covering:@param Datagrid&DatagridInterface<Proxy>annotation@param Datagrid<Proxy>(already worked, sanity check)ChildDatagrid&Datagrid<Proxy>)ChildDatagrid&DatagridInterface<Proxy>)Datagrid<Proxy>&DatagridInterface<Proxy>)Fixes phpstan/phpstan#5357