Repository navigation
Expand file tree
/
Copy pathTypesAssignedToPropertiesRule.php
More file actions
122 lines (105 loc) · 3.48 KB
/
Copy pathTypesAssignedToPropertiesRule.php
File metadata and controls
122 lines (105 loc) · 3.48 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Properties;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function is_string;
use function sprintf;
/**
* @implements Rule<PropertyAssignNode>
*/
#[RegisteredRule(level: 3)]
final class TypesAssignedToPropertiesRule implements Rule
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private PropertyReflectionFinder $propertyReflectionFinder,
)
{
}
public function getNodeType(): string
{
return PropertyAssignNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$propertyFetch = $node->getPropertyFetch();
$propertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($propertyFetch, $scope);
$errors = [];
foreach ($propertyReflections as $propertyReflection) {
$errors = array_merge($errors, $this->processSingleProperty(
$propertyReflection,
$propertyFetch,
$node->getAssignedExpr(),
));
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function processSingleProperty(
FoundPropertyReflection $propertyReflection,
PropertyFetch|StaticPropertyFetch $fetch,
Node\Expr $assignedExpr,
): array
{
if (!$propertyReflection->isWritable()) {
return [];
}
$scope = $propertyReflection->getScope();
$inFunction = $scope->getFunction();
if (
$fetch instanceof PropertyFetch
&& $fetch->var instanceof Node\Expr\Variable
&& is_string($fetch->var->name)
&& $fetch->var->name === 'this'
&& $fetch->name instanceof Node\Identifier
&& $inFunction instanceof PhpMethodFromParserNodeReflection
&& $inFunction->isPropertyHook()
&& $inFunction->getHookedPropertyName() === $fetch->name->toString()
) {
$propertyType = $propertyReflection->getReadableType();
} else {
$propertyType = $propertyReflection->getWritableType();
}
$assignedValueType = $scope->getType($assignedExpr);
$accepts = $this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes());
if (!$accepts->result) {
$propertyDescription = $this->describePropertyByName($propertyReflection, $propertyReflection->getName());
$givenType = $accepts->witnessType ?? $assignedValueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType);
return [
RuleErrorBuilder::message(sprintf(
'%s (%s) does not accept %s.',
$propertyDescription,
$propertyType->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->line($assignedExpr->getStartLine())
->identifier('assign.propertyType')
->acceptsReasonsTip($accepts->reasons)
->build(),
];
}
return [];
}
private function describePropertyByName(PropertyReflection $property, string $propertyName): string
{
if (!$property->isStatic()) {
return sprintf('Property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName);
}
return sprintf('Static property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName);
}
}