Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Rules/Generics/PropertyVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$variance = $node->isReadOnly() || $node->isReadOnlyByPhpDoc()
$variance = $node->isReadOnly() || $node->isReadOnlyByPhpDoc() || $this->isEffectivelyReadOnly($node)
? TemplateTypeVariance::createCovariant()
: TemplateTypeVariance::createInvariant();

Expand All @@ -56,4 +56,24 @@ public function processNode(Node $node, Scope $scope): array
);
}

private function isEffectivelyReadOnly(ClassPropertyNode $node): bool
{
if ($node->isPrivateSet() || $node->isProtectedSet()) {
return true;
}

$hooks = $node->getHooks();
if ($hooks === []) {
return false;
}

foreach ($hooks as $hook) {
if ($hook->name->name === 'set') {
return false;
}
}

return true;
}

}
59 changes: 59 additions & 0 deletions tests/PHPStan/Rules/Generics/PropertyVarianceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,63 @@ public function testBug13049(): void
$this->analyse([__DIR__ . '/data/bug-13049.php'], []);
}

#[RequiresPhp('>= 8.4')]
public function testBug12964(): void
{
$this->analyse([__DIR__ . '/data/bug-12964.php'], [
[
'Template type X is declared as covariant, but occurs in contravariant position in property Bug12964\C::$b.',
51,
],
[
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\C::$d.',
57,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in property Bug12964\D::$a.',
65,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in property Bug12964\D::$c.',
71,
],
[
'Template type X is declared as contravariant, but occurs in invariant position in property Bug12964\D::$d.',
74,
],
[
'Template type X is declared as covariant, but occurs in contravariant position in property Bug12964\E::$b.',
85,
],
[
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\E::$d.',
91,
],
[
'Template type X is declared as covariant, but occurs in contravariant position in property Bug12964\F::$b.',
103,
],
[
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\F::$d.',
109,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in property Bug12964\G::$a.',
118,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in property Bug12964\G::$c.',
124,
],
[
'Template type X is declared as contravariant, but occurs in invariant position in property Bug12964\G::$d.',
127,
],
[
'Template type X is declared as covariant, but occurs in invariant position in property Bug12964\H::$a.',
136,
],
]);
}

}
137 changes: 137 additions & 0 deletions tests/PHPStan/Rules/Generics/data/bug-12964.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php // lint >= 8.4

declare(strict_types = 1);

namespace Bug12964;

/** @template-contravariant T */
interface In {
}

/** @template-covariant T */
interface Out {
}

/** @template T */
interface Invariant {
}

/**
* @template-covariant T
*/
interface A
{
/**
* @var T
*/
public mixed $b { get; }
}

/**
* @template-covariant T
*/
final class B
{
/**
* @param T $data
*/
public function __construct(
public private(set) mixed $data,
) {}
}

/**
* @template-covariant X
*/
class C {
/** @var X */
public private(set) mixed $a;

/** @var In<X> */
public private(set) mixed $b;

/** @var Out<X> */
public private(set) mixed $c;

/** @var Invariant<X> */
public private(set) mixed $d;
}

/**
* @template-contravariant X
*/
class D {
/** @var X */
public private(set) mixed $a;

/** @var In<X> */
public private(set) mixed $b;

/** @var Out<X> */
public private(set) mixed $c;

/** @var Invariant<X> */
public private(set) mixed $d;
}

/**
* @template-covariant X
*/
class E {
/** @var X */
public protected(set) mixed $a;

/** @var In<X> */
public protected(set) mixed $b;

/** @var Out<X> */
public protected(set) mixed $c;

/** @var Invariant<X> */
public protected(set) mixed $d;
}

/**
* @template-covariant X
*/
interface F
{
/** @var X */
public mixed $a { get; }

/** @var In<X> */
public mixed $b { get; }

/** @var Out<X> */
public mixed $c { get; }

/** @var Invariant<X> */
public mixed $d { get; }
}

/**
* @template-contravariant X
*/
interface G
{
/** @var X */
public mixed $a { get; }

/** @var In<X> */
public mixed $b { get; }

/** @var Out<X> */
public mixed $c { get; }

/** @var Invariant<X> */
public mixed $d { get; }
}

/**
* @template-covariant X
*/
interface H
{
/** @var X */
public mixed $a { get; set; }
}
Loading