-
Notifications
You must be signed in to change notification settings - Fork 426
/
UnusedPrivatePropertyRule.php
198 lines (173 loc) · 5.55 KB
/
UnusedPrivatePropertyRule.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertiesNode;
use PHPStan\Node\Property\PropertyRead;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use function array_key_exists;
use function array_map;
use function count;
use function sprintf;
use function strpos;
/**
* @implements Rule<ClassPropertiesNode>
*/
class UnusedPrivatePropertyRule implements Rule
{
/**
* @param string[] $alwaysWrittenTags
* @param string[] $alwaysReadTags
*/
public function __construct(
private ReadWritePropertiesExtensionProvider $extensionProvider,
private array $alwaysWrittenTags,
private array $alwaysReadTags,
private bool $checkUninitializedProperties,
)
{
}
public function getNodeType(): string
{
return ClassPropertiesNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->getClass() instanceof Node\Stmt\Class_) {
return [];
}
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
$classReflection = $scope->getClassReflection();
$classType = new ObjectType($classReflection->getName());
$properties = [];
foreach ($node->getProperties() as $property) {
if (!$property->isPrivate()) {
continue;
}
if ($property->isDeclaredInTrait()) {
continue;
}
$alwaysRead = false;
$alwaysWritten = false;
if ($property->getPhpDoc() !== null) {
$text = $property->getPhpDoc();
foreach ($this->alwaysReadTags as $tag) {
if (strpos($text, $tag) === false) {
continue;
}
$alwaysRead = true;
break;
}
foreach ($this->alwaysWrittenTags as $tag) {
if (strpos($text, $tag) === false) {
continue;
}
$alwaysWritten = true;
break;
}
}
$propertyName = $property->getName();
if (!$alwaysRead || !$alwaysWritten) {
if (!$classReflection->hasNativeProperty($propertyName)) {
continue;
}
$propertyReflection = $classReflection->getNativeProperty($propertyName);
foreach ($this->extensionProvider->getExtensions() as $extension) {
if ($alwaysRead && $alwaysWritten) {
break;
}
if (!$alwaysRead && $extension->isAlwaysRead($propertyReflection, $propertyName)) {
$alwaysRead = true;
}
if ($alwaysWritten || !$extension->isAlwaysWritten($propertyReflection, $propertyName)) {
continue;
}
$alwaysWritten = true;
}
}
$read = $alwaysRead;
$written = $alwaysWritten || $property->getDefault() !== null;
$properties[$propertyName] = [
'read' => $read,
'written' => $written,
'node' => $property,
];
}
foreach ($node->getPropertyUsages() as $usage) {
$fetch = $usage->getFetch();
if ($fetch->name instanceof Node\Identifier) {
$propertyNames = [$fetch->name->toString()];
} else {
$propertyNameType = $usage->getScope()->getType($fetch->name);
$strings = $propertyNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
}
$propertyNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);
}
if ($fetch instanceof Node\Expr\PropertyFetch) {
$fetchedOnType = $usage->getScope()->getType($fetch->var);
} else {
if (!$fetch->class instanceof Node\Name) {
continue;
}
$fetchedOnType = $usage->getScope()->resolveTypeByName($fetch->class);
}
if ($classType->isSuperTypeOf($fetchedOnType)->no()) {
continue;
}
if ($fetchedOnType instanceof MixedType) {
continue;
}
foreach ($propertyNames as $propertyName) {
if (!array_key_exists($propertyName, $properties)) {
continue;
}
if ($usage instanceof PropertyRead) {
$properties[$propertyName]['read'] = true;
} else {
$properties[$propertyName]['written'] = true;
}
}
}
[$uninitializedProperties] = $node->getUninitializedProperties($scope, []);
$errors = [];
foreach ($properties as $name => $data) {
$propertyNode = $data['node'];
if ($propertyNode->isStatic()) {
$propertyName = sprintf('Static property %s::$%s', $scope->getClassReflection()->getDisplayName(), $name);
} else {
$propertyName = sprintf('Property %s::$%s', $scope->getClassReflection()->getDisplayName(), $name);
}
$tip = sprintf('See: %s', 'https://phpstan.org/developing-extensions/always-read-written-properties');
if (!$data['read']) {
if (!$data['written']) {
$errors[] = RuleErrorBuilder::message(sprintf('%s is unused.', $propertyName))
->line($propertyNode->getStartLine())
->identifier('deadCode.unusedProperty')
->metadata([
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
'classStartLine' => $node->getClass()->getStartLine(),
'propertyName' => $name,
])
->tip($tip)
->build();
} else {
$errors[] = RuleErrorBuilder::message(sprintf('%s is never read, only written.', $propertyName))->line($propertyNode->getStartLine())->tip($tip)->build();
}
} elseif (!$data['written'] && (!array_key_exists($name, $uninitializedProperties) || !$this->checkUninitializedProperties)) {
$errors[] = RuleErrorBuilder::message(sprintf('%s is never written, only read.', $propertyName))->line($propertyNode->getStartLine())->tip($tip)->build();
}
}
return $errors;
}
}