Skip to content

Commit d8f11d9

Browse files
phpstan-botclaude
andcommitted
Move template type bound check from VarTagTypeRuleHelper to TemplateTypeVariance
Instead of special-casing GenericObjectType in VarTagTypeRuleHelper, handle the template type equivalence at the type system level: in the invariant variance check, when the other type ($b) is a TemplateType whose bound equals $a, consider them equivalent. This approach uses the existing $acceptsContext infrastructure direction rather than adding rule-level workarounds. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent df5ee10 commit d8f11d9

File tree

3 files changed

+4
-18
lines changed

3 files changed

+4
-18
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ parameters:
726726
-
727727
rawMessage: Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
728728
identifier: phpstanApi.instanceofType
729-
count: 3
729+
count: 1
730730
path: src/Rules/PhpDoc/VarTagTypeRuleHelper.php
731731

732732
-

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,6 @@ private function shouldVarTagTypeBeReported(Scope $scope, Node\Expr $expr, Type
177177

178178
private function checkType(Scope $scope, Type $type, Type $varTagType, int $depth = 0): bool
179179
{
180-
if (
181-
$type instanceof GenericObjectType
182-
&& $varTagType instanceof GenericObjectType
183-
&& $type->getClassName() === $varTagType->getClassName()
184-
) {
185-
$typeArgs = $type->getTypes();
186-
$varTagArgs = $varTagType->getTypes();
187-
if (count($typeArgs) === count($varTagArgs)) {
188-
foreach ($typeArgs as $i => $typeArg) {
189-
if ($this->checkType($scope, $typeArg, $varTagArgs[$i], $depth + 1)) {
190-
return true;
191-
}
192-
}
193-
return false;
194-
}
195-
}
196-
197180
if ($this->strictWideningCheck) {
198181
return !$this->isSuperTypeOfVarType($scope, $type, $varTagType);
199182
}

src/Type/Generic/TemplateTypeVariance.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b): I
177177

178178
if ($this->invariant()) {
179179
$result = $a->equals($b);
180+
if (!$result && $b instanceof TemplateType && $a->equals($b->getBound())) {
181+
$result = true;
182+
}
180183
$reasons = [];
181184
if (!$result) {
182185
if (

0 commit comments

Comments
 (0)