Fix phpstan/phpstan#6139: False positive for covariant generic parameter#5338
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#6139: False positive for covariant generic parameter#5338phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When a covariant generic type (e.g. Element<T> with @template-covariant T) is used as a method parameter type, covariant template type arguments are no longer falsely reported as variance violations - Added findCovariantTemplatesInCovariantGeneric() in VarianceCheck to detect and skip these cases using getObjectClassReflections() and getTemplateType() - New regression test in tests/PHPStan/Rules/Generics/data/bug-6139.php - Updated existing covariant variance test expectations to reflect the fix
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.
Summary
When a covariant generic type like
Element<T>(where Element declares@template-covariant T) is used as a method parameter type, PHPStan was incorrectly reporting that the covariant template typeToccurs in a contravariant position. Since the covariant generic only produces values of typeT, using it in a parameter position does not create a true contravariant use ofT.Changes
src/Rules/Generics/VarianceCheck.php:findCovariantTemplatesInCovariantGeneric()method that detects when a covariant template type is used directly as a type argument to a covariant generic type in a parameter positiongetObjectClassReflections()andgetTemplateType()instead ofinstanceof GenericObjectTypeto follow PHPStan's type-checking conventionscheck()method now skips variance errors for these identified template typestests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php:@param Out<X>in covariant class test (parameterf)@param Out<X>in static method covariant class test (parameterc)testBug6139()test methodtests/PHPStan/Rules/Generics/data/bug-6139.phpregression testRoot cause
The variance check composed the position variance (contravariant for parameters) with the generic type's template variance (covariant) using the standard multiplication rule:
contravariant × covariant = contravariant. This treated any covariant template type used as a type argument to a covariant generic in a parameter as a variance violation.The fix identifies this specific pattern at the top level (in
VarianceCheck::check) and skips the error for direct template type arguments that have matching covariant variance with the generic class's template parameter. This avoids the false positive while preserving correct variance checking for nested generic types (e.g.,@return In<Out<X>>is still correctly flagged).Test
Added
tests/PHPStan/Rules/Generics/data/bug-6139.phpwhich reproduces the original issue:Element<T>interface with@template-covariant TFormatLoader<T>interface with@template-covariant Tand methodaddElement(Element<T> $element)Fixes phpstan/phpstan#6139