phpstan-doctrine
phpstan-doctrine copied to clipboard
fix: update template annotations to remove covariant keyword
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.stubORM/Mapping/ClassMetadata.stubORM/Mapping/ClassMetadataInfo.stubPersistence/Mapping/ClassMetadata.stub
Why This Works
The covariance modifier was inappropriate in this case because:
- The
getReflectionClass()method returns aReflectionClass<T> - This return type must be exactly of the specified type, not a subtype or supertype
- When a type parameter is used in an invariant position (like a return type), it should not be declared as covariant
Issues
#646