Skip to content

Mark ClassMetadataFactory's template parameter as covariant - #536

Open
mpdude wants to merge 1 commit into
doctrine:4.3.xfrom
mpdude:mark-classmetadatafactory-covariant
Open

mpdude wants to merge 1 commit into
doctrine:4.3.xfrom
mpdude:mark-classmetadatafactory-covariant

Conversation

@mpdude

@mpdude mpdude commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

I am not entirely sure about this one.

In doctrine/orm#12760, I would like to add an ORM-specific ClassMetadataFactoryInterface interface.

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:

Return type (Doctrine\ORM\Mapping\ClassMetadataFactoryInterface) of method
Doctrine\ORM\EntityManager::getMetadataFactory() should be compatible with
return type (Doctrine\Persistence\Mapping\ClassMetadataFactory<Doctrine\Persistence\Mapping\ClassMetadata<object>>)
of method Doctrine\Persistence\ObjectManager::getMetadataFactory()
[identifier=method.childReturnType]

Or, as Claude would put it:

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.

`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
mpdude force-pushed the mark-classmetadatafactory-covariant branch from bb4f5d0 to 646b55d Compare November 9, 2026 12:07
@mpdude
mpdude requested a review from stof November 9, 2026 12:09
@greg0ire

Copy link
Copy Markdown
Member

I am not entirely sure about this one.

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants