|
16 | 16 | use PHPStan\Type\Type; |
17 | 17 | use PHPStan\Type\TypeTraverser; |
18 | 18 | use PHPStan\Type\VerbosityLevel; |
| 19 | +use ReflectionClass; |
19 | 20 | use function array_filter; |
20 | 21 | use function array_keys; |
21 | 22 | use function array_values; |
@@ -105,17 +106,24 @@ public function check( |
105 | 106 | $genericTypeVariance = $genericTypeVariances[$i] ?? TemplateTypeVariance::createInvariant(); |
106 | 107 | if ($templateType instanceof TemplateType && !$genericTypeVariance->invariant()) { |
107 | 108 | if ($genericTypeVariance->equals($templateType->getVariance())) { |
108 | | - $messages[] = RuleErrorBuilder::message(sprintf( |
109 | | - $typeProjectionIsRedundantMessage, |
110 | | - TypeProjectionHelper::describe($genericTypeType, $genericTypeVariance, VerbosityLevel::typeOnly()), |
111 | | - $genericType->describe(VerbosityLevel::typeOnly()), |
112 | | - $templateType->describe(VerbosityLevel::typeOnly()), |
113 | | - $classLikeDescription, |
114 | | - $classReflection->getDisplayName(false), |
115 | | - )) |
116 | | - ->identifier('generics.callSiteVarianceRedundant') |
117 | | - ->tip('You can safely remove the call-site variance annotation.') |
118 | | - ->build(); |
| 109 | + if ( |
| 110 | + // allow ReflectionClass<covariant X> |
| 111 | + // so that same code works for PHP 8.3 and 8.4+ |
| 112 | + $classReflection->getName() !== ReflectionClass::class |
| 113 | + || $templateType->getName() !== 'T' |
| 114 | + ) { |
| 115 | + $messages[] = RuleErrorBuilder::message(sprintf( |
| 116 | + $typeProjectionIsRedundantMessage, |
| 117 | + TypeProjectionHelper::describe($genericTypeType, $genericTypeVariance, VerbosityLevel::typeOnly()), |
| 118 | + $genericType->describe(VerbosityLevel::typeOnly()), |
| 119 | + $templateType->describe(VerbosityLevel::typeOnly()), |
| 120 | + $classLikeDescription, |
| 121 | + $classReflection->getDisplayName(false), |
| 122 | + )) |
| 123 | + ->identifier('generics.callSiteVarianceRedundant') |
| 124 | + ->tip('You can safely remove the call-site variance annotation.') |
| 125 | + ->build(); |
| 126 | + } |
119 | 127 | } elseif (!$genericTypeVariance->validPosition($templateType->getVariance())) { |
120 | 128 | $messages[] = RuleErrorBuilder::message(sprintf( |
121 | 129 | $typeProjectionHasConflictingVarianceMessage, |
|
0 commit comments