/
VarianceCheck.php
96 lines (80 loc) · 2.74 KB
/
VarianceCheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Generics;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Type;
use function sprintf;
class VarianceCheck
{
/** @return RuleError[] */
public function checkParametersAcceptor(
ParametersAcceptor $parametersAcceptor,
string $parameterTypeMessage,
string $returnTypeMessage,
string $generalMessage,
bool $isStatic,
bool $isPrivate,
): array
{
$errors = [];
foreach ($parametersAcceptor->getTemplateTypeMap()->getTypes() as $templateType) {
if (!$templateType instanceof TemplateType
|| $templateType->getScope()->getFunctionName() === null
|| $templateType->getVariance()->invariant()
) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Variance annotation is only allowed for type parameters of classes and interfaces, but occurs in template type %s in %s.',
$templateType->getName(),
$generalMessage,
))->build();
}
if ($isPrivate) {
return $errors;
}
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$variance = $isStatic
? TemplateTypeVariance::createStatic()
: TemplateTypeVariance::createContravariant();
$type = $parameterReflection->getType();
$message = sprintf($parameterTypeMessage, $parameterReflection->getName());
foreach ($this->check($variance, $type, $message) as $error) {
$errors[] = $error;
}
}
$variance = TemplateTypeVariance::createCovariant();
$type = $parametersAcceptor->getReturnType();
foreach ($this->check($variance, $type, $returnTypeMessage) as $error) {
$errors[] = $error;
}
return $errors;
}
/** @return RuleError[] */
public function check(TemplateTypeVariance $positionVariance, Type $type, string $messageContext): array
{
$errors = [];
foreach ($type->getReferencedTemplateTypes($positionVariance) as $reference) {
$referredType = $reference->getType();
if (($referredType->getScope()->getFunctionName() !== null && !$referredType->getVariance()->invariant())
|| $this->isTemplateTypeVarianceValid($reference->getPositionVariance(), $referredType)) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Template type %s is declared as %s, but occurs in %s position %s.',
$referredType->getName(),
$referredType->getVariance()->describe(),
$reference->getPositionVariance()->describe(),
$messageContext,
))->build();
}
return $errors;
}
private function isTemplateTypeVarianceValid(TemplateTypeVariance $positionVariance, TemplateType $type): bool
{
return $positionVariance->validPosition($type->getVariance());
}
}