Skip to content

fix: update template annotations to remove covariant keyword #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from

Conversation

deadLocks21
Copy link

@deadLocks21 deadLocks21 commented Jun 3, 2025

Description

This PR addresses PHPStan errors related to template type variance in Doctrine's metadata classes. The errors were occurring because the template type T was declared as covariant but was being used in an invariant position in the getReflectionClass() method.

The Problem

The following errors were reported by PHPStan:

  • Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ODM\MongoDB\Mapping\ClassMetadata::getReflectionClass()
  • Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ORM\Mapping\ClassMetadataInfo::getReflectionClass()
  • Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\Persistence\Mapping\ClassMetadata::getReflectionClass()

The Solution

We removed the -covariant modifier from the template type declarations in:

  • MongoClassMetadataInfo.stub
  • ORM/Mapping/ClassMetadata.stub
  • ORM/Mapping/ClassMetadataInfo.stub
  • Persistence/Mapping/ClassMetadata.stub

Why This Works

The covariance modifier was inappropriate in this case because:

  1. The getReflectionClass() method returns a ReflectionClass<T>
  2. This return type must be exactly of the specified type, not a subtype or supertype
  3. When a type parameter is used in an invariant position (like a return type), it should not be declared as covariant

Issues

#646

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.

1 participant