-
Notifications
You must be signed in to change notification settings - Fork 426
/
PropertyVarianceRule.php
50 lines (40 loc) · 1.2 KB
/
PropertyVarianceRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Generics;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use function sprintf;
/**
* @implements Rule<ClassPropertyNode>
*/
class PropertyVarianceRule implements Rule
{
public function __construct(private VarianceCheck $varianceCheck)
{
}
public function getNodeType(): string
{
return ClassPropertyNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof ClassReflection) {
return [];
}
if (!$classReflection->hasNativeProperty($node->getName())) {
return [];
}
$propertyReflection = $classReflection->getNativeProperty($node->getName());
return $this->varianceCheck->checkProperty(
$propertyReflection,
sprintf('in property %s::$%s', SprintfHelper::escapeFormatString($classReflection->getDisplayName()), SprintfHelper::escapeFormatString($node->getName())),
$propertyReflection->isStatic(),
$propertyReflection->isPrivate(),
$propertyReflection->isReadOnly(),
);
}
}