Repository navigation
Conversation
`ClassMetadataFactory`'s `@template T of ClassMetadata` is invariant, so PHPStan accepts a return type only when its generic argument matches exactly, never merely as a subtype. If a consumer's own `ClassMetadataFactory` implementation binds `T` to a `ClassMetadata` subtype, overriding `ObjectManager::getMetadataFactory(): ClassMetadataFactory<ClassMetadata<object>>` with that narrower return type fails PHPStan's `method.childReturnType` check. Marking `T` as `@template-covariant` lets such a narrower return type pass that check. `setMetadataFor()`'s deprecated `$class` parameter also binds `T`, but in a contravariant position, which PHPStan's variance check forbids for a covariant template. That check validates the interface's own declaration, not each call site, so suppressing it leaves callers' type-checking against `T` untouched. Only a caller holding a covariance-widened reference to the factory loses precision on this one deprecated method.
mpdude
force-pushed
the
mark-classmetadatafactory-covariant
branch
from
November 9, 2026 12:07
bb4f5d0 to
646b55d
Compare
Member
If you try using your branch of persistence on your branch of ORM, what effect does it have on PHPStan (the error you mention, and the existing baseline)? |
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.
I am not entirely sure about this one.
In doctrine/orm#12760, I would like to add an ORM-specific
ClassMetadataFactoryInterfaceinterface.It would use
@extends {Persistence}ClassMetadataFactory<{ORM}ClassMetadata<object>>, with the{...}indicating which package (Persistence vs. ORM) the class comes from.PHPStan rejects the new interface as not being a valid subtype of the CMF interface here:
Or, as Claude would put it:
ClassMetadataFactory's@template T of ClassMetadatais invariant, so PHPStan accepts a return type only when its generic argument matches exactly, never merely as a subtype. If a consumer's ownClassMetadataFactoryimplementation bindsTto aClassMetadatasubtype, overridingObjectManager::getMetadataFactory(): ClassMetadataFactory<ClassMetadata<object>>with that narrower return type fails PHPStan'smethod.childReturnTypecheck.Marking
Tas@template-covariantlets such a narrower return type pass that check.