Skip to content

Commit 23a0cf7

Browse files
phpstan-botclaude
andcommitted
Treat protected(set) properties as invariant for template variance
protected(set) allows subclasses to write to the property, just like regular protected properties. Only private(set) truly restricts writes to the declaring class, so only private(set) should be treated as covariant. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ab3256e commit 23a0cf7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Rules/Generics/PropertyVarianceRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function processNode(Node $node, Scope $scope): array
5858

5959
private function hasRestrictedWriteAccess(ClassPropertyNode $node): bool
6060
{
61-
if ($node->isPrivateSet() || $node->isProtectedSet()) {
61+
if ($node->isPrivateSet()) {
6262
return true;
6363
}
6464

tests/PHPStan/Rules/Generics/PropertyVarianceRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,17 @@ public function testBug12964(): void
162162
74,
163163
],
164164
[
165-
'Template type X is declared as covariant, but occurs in contravariant position in property Bug12964\E::$b.',
165+
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\E::$a.',
166+
82,
167+
],
168+
[
169+
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\E::$b.',
166170
85,
167171
],
172+
[
173+
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\E::$c.',
174+
88,
175+
],
168176
[
169177
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\E::$d.',
170178
91,

0 commit comments

Comments
 (0)