Skip to content

Commit 3e1b2d6

Browse files
phpstan-botclaude
andcommitted
Use strict get_class() check in ObjectType::equals() and fix invariant variance for template types
Instead of narrowly rejecting only EnumCaseObjectType and GenericObjectType in ObjectType::equals(), use a strict get_class() comparison for all ObjectType subclasses. This makes equals() properly symmetric and prevents any ObjectType subclass from being considered equal to a plain ObjectType. To preserve the correct behavior where IRepository<E of IEntity> remains a valid subtype of IRepository<IEntity>, add a template type bound check in TemplateTypeVariance::isValidVariance() for invariant positions: when the actual type argument ($b) is a TemplateType whose bound equals the expected type argument ($a), treat them as equal. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ffdd556 commit 3e1b2d6

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,16 +1473,10 @@ parameters:
14731473
count: 1
14741474
path: src/Type/ObjectShapeType.php
14751475

1476-
-
1477-
rawMessage: 'Doing instanceof PHPStan\Type\Enum\EnumCaseObjectType is error-prone and deprecated. Use Type::getEnumCases() instead.'
1478-
identifier: phpstanApi.instanceofType
1479-
count: 1
1480-
path: src/Type/ObjectType.php
1481-
14821476
-
14831477
rawMessage: Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
14841478
identifier: phpstanApi.instanceofType
1485-
count: 2
1479+
count: 1
14861480
path: src/Type/ObjectType.php
14871481

14881482
-

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 (

src/Type/ObjectType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public function equals(Type $type): bool
629629
return false;
630630
}
631631

632-
if (($type instanceof EnumCaseObjectType || $type instanceof GenericObjectType) && get_class($this) !== get_class($type)) {
632+
if (get_class($this) !== get_class($type)) {
633633
return false;
634634
}
635635

0 commit comments

Comments
 (0)