-
Notifications
You must be signed in to change notification settings - Fork 108
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
fix: update template annotations to remove covariant keyword #663
Conversation
mroeling
commented
Aug 27, 2025
What is the expected moment to have this merged please? We want to upgrade to php8.4 but this is a blocking issue!
@mroeling Please describe what error are you experiencing and why it's blocking you from upgrading to 8.4. It'd be helpful in helping me underseand why this change is needed.
@mroeling Please describe what error are you experiencing and why it's blocking you from upgrading to 8.4. It'd be helpful in helping me understand why this change is needed.
Ofc, no problem.
Running phpstan on our code raises errors causing phpStan to fail.
Info:
phpstan level 4
Ubuntu 24.04 LTS
php 8.4.11
Line deploy/app/vendor/phpstan/phpstan-doctrine/stubs/MongoClassMetadataInfo.stub
------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
40 Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ODM\MongoDB\Mapping\ClassMetadata::getReflectionClass().
------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line deploy/app/vendor/phpstan/phpstan-doctrine/stubs/ORM/Mapping/ClassMetadataInfo.stub
------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------
59 Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\ORM\Mapping\ClassMetadataInfo::getReflectionClass().
------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line deploy/app/vendor/phpstan/phpstan-doctrine/stubs/Persistence/Mapping/ClassMetadata.stub
------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
20 Template type T is declared as covariant, but occurs in invariant position in return type of method Doctrine\Persistence\Mapping\ClassMetadata::getReflectionClass().
------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
[ERROR] Found 3 errors
So basically we are waiting for this change to get into the mainstream so that we can test with php8.4. Using php8.4-fpm there is no issue, but for CLI this is blocking.
For comparison, cli php8.3 does not raise errors (obviously).
Please show the path where you're running PHPStan from, the path to bin/phpstan. There is something atypical about your setup. These errors from vendor stubs are usually filtered and not showed.
We use phing as "executor".
From the root (=phing.dir), the vendor map is in deploy/app/vendor
phpstan is run as deploy/app/vendor/bin/phpstan analyze
<property name="phpStanBinary"
value="${phing.dir}/deploy/app/vendor/bin/phpstan"
override="no"/>
<exec executable="${phpStanBinary}"
passthru="true"
checkreturn="true"
dir="${phing.dir}"
>
<arg value="analyze"/>
<arg line="--memory-limit 2G"/>
<arg line="--no-progress"/>
<arg path="${phing.dir}/deploy/app/include"/>
<arg path="${phing.dir}/deploy/app/config"/>
<arg path="${phing.dir}/deploy/app/services"/>
<arg path="${phing.dir}/tests"/>
</exec>
The getReflectionClass() functions from these 3 Doctrine classes are called within our codebase, in the deploy/app/include
folder
That looks okay. Personally I'd try this:
<exec executable="vendor/bin/phpstan"
passthru="true"
checkreturn="true"
dir="${phing.dir}/deploy/app"
>
<arg value="analyze"/>
<arg line="--memory-limit 2G"/>
<arg line="--no-progress"/>
<arg path="include"/>
<arg path="config"/>
<arg path="services"/>
<arg path="tests"/>
</exec>
Typically people don't run PHPStan with these absolute paths and that might be throwing off the filtering logic.
These bug(s) still need to be fixed, I just want to help you meanwhile.
mroeling
commented
Aug 27, 2025
These bug(s) still need to be fixed, I just want to help you meanwhile.
Much appreciated! I'll give it a try.
This is maybe not the right way to solve this since in the original repository, there is the covariant
annotation
https://github.com/doctrine/mongodb-odm/blob/e3352c0783886e525d2b8accb8bc7984148017bf/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php#L272
But you'll see there is no phpdoc for getReflectionClass method
https://github.com/doctrine/mongodb-odm/blob/e3352c0783886e525d2b8accb8bc7984148017bf/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php#L894
Might be better to consider removing the getReflectionClass stub in order to have the same situation that the original library
or to update the original libraries first ?
Uh oh!
There was an error while loading. Please reload this page.
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 thegetReflectionClass()
method.The Problem
The following errors were reported by PHPStan:
Doctrine\ODM\MongoDB\Mapping\ClassMetadata::getReflectionClass()
Doctrine\ORM\Mapping\ClassMetadataInfo::getReflectionClass()
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:
getReflectionClass()
method returns aReflectionClass<T>
Issues
#646