Skip to content

Commit 2db3c26

Browse files
committed
Tips for always used properties and constants extensions
1 parent 53eb2e3 commit 2db3c26

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/Rules/DeadCode/UnusedPrivateConstantRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function processNode(Node $node, Scope $scope): array
8585
'classStartLine' => $node->getClass()->getStartLine(),
8686
'constantName' => $constantName,
8787
])
88+
->tip(sprintf('See: %s', 'https://phpstan.org/developing-extensions/always-used-class-constants'))
8889
->build();
8990
}
9091

src/Rules/DeadCode/UnusedPrivatePropertyRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public function processNode(Node $node, Scope $scope): array
185185
} else {
186186
$propertyName = sprintf('Property %s::$%s', $scope->getClassReflection()->getDisplayName(), $name);
187187
}
188+
$tip = sprintf('See: %s', 'https://phpstan.org/developing-extensions/always-read-written-properties');
188189
if (!$data['read']) {
189190
if (!$data['written']) {
190191
$errors[] = RuleErrorBuilder::message(sprintf('%s is unused.', $propertyName))
@@ -196,12 +197,13 @@ public function processNode(Node $node, Scope $scope): array
196197
'classStartLine' => $node->getClass()->getStartLine(),
197198
'propertyName' => $name,
198199
])
200+
->tip($tip)
199201
->build();
200202
} else {
201-
$errors[] = RuleErrorBuilder::message(sprintf('%s is never read, only written.', $propertyName))->line($propertyNode->getStartLine())->build();
203+
$errors[] = RuleErrorBuilder::message(sprintf('%s is never read, only written.', $propertyName))->line($propertyNode->getStartLine())->tip($tip)->build();
202204
}
203205
} elseif (!$data['written'] && (!array_key_exists($name, $uninitializedProperties) || !$this->checkUninitializedProperties)) {
204-
$errors[] = RuleErrorBuilder::message(sprintf('%s is never written, only read.', $propertyName))->line($propertyNode->getStartLine())->build();
206+
$errors[] = RuleErrorBuilder::message(sprintf('%s is never written, only read.', $propertyName))->line($propertyNode->getStartLine())->tip($tip)->build();
205207
}
206208
}
207209

tests/PHPStan/Rules/DeadCode/UnusedPrivateConstantRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public function testRule(): void
3838
[
3939
'Constant UnusedPrivateConstant\Foo::BAR_CONST is unused.',
4040
10,
41+
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
4142
],
4243
[
4344
'Constant UnusedPrivateConstant\TestExtension::UNUSED is unused.',
4445
23,
46+
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
4547
],
4648
]);
4749
}

tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,60 +67,75 @@ public function testRule(): void
6767
$this->alwaysWrittenTags = [];
6868
$this->alwaysReadTags = [];
6969

70+
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
71+
7072
$this->analyse([__DIR__ . '/data/unused-private-property.php'], [
7173
[
7274
'Property UnusedPrivateProperty\Foo::$bar is never read, only written.',
7375
10,
76+
$tip,
7477
],
7578
[
7679
'Property UnusedPrivateProperty\Foo::$baz is unused.',
7780
12,
81+
$tip,
7882
],
7983
[
8084
'Property UnusedPrivateProperty\Foo::$lorem is never written, only read.',
8185
14,
86+
$tip,
8287
],
8388
[
8489
'Property UnusedPrivateProperty\Bar::$baz is never written, only read.',
8590
57,
91+
$tip,
8692
],
8793
[
8894
'Static property UnusedPrivateProperty\Baz::$bar is never read, only written.',
8995
86,
96+
$tip,
9097
],
9198
[
9299
'Static property UnusedPrivateProperty\Baz::$baz is unused.',
93100
88,
101+
$tip,
94102
],
95103
[
96104
'Static property UnusedPrivateProperty\Baz::$lorem is never written, only read.',
97105
90,
106+
$tip,
98107
],
99108
[
100109
'Property UnusedPrivateProperty\Lorem::$baz is never read, only written.',
101110
117,
111+
$tip,
102112
],
103113
[
104114
'Property class@anonymous/tests/PHPStan/Rules/DeadCode/data/unused-private-property.php:152::$bar is unused.',
105115
153,
116+
$tip,
106117
],
107118
[
108119
'Property UnusedPrivateProperty\DolorWithAnonymous::$foo is unused.',
109120
148,
121+
$tip,
110122
],
111123
]);
112124
$this->analyse([__DIR__ . '/data/TestExtension.php'], [
113125
[
114126
'Property UnusedPrivateProperty\TestExtension::$unused is unused.',
115127
8,
128+
$tip,
116129
],
117130
[
118131
'Property UnusedPrivateProperty\TestExtension::$read is never written, only read.',
119132
10,
133+
$tip,
120134
],
121135
[
122136
'Property UnusedPrivateProperty\TestExtension::$written is never read, only written.',
123137
12,
138+
$tip,
124139
],
125140
]);
126141
}
@@ -129,14 +144,17 @@ public function testAlwaysUsedTags(): void
129144
{
130145
$this->alwaysWrittenTags = ['@ORM\Column'];
131146
$this->alwaysReadTags = ['@get'];
147+
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
132148
$this->analyse([__DIR__ . '/data/private-property-with-tags.php'], [
133149
[
134150
'Property PrivatePropertyWithTags\Foo::$title is never read, only written.',
135151
13,
152+
$tip,
136153
],
137154
[
138155
'Property PrivatePropertyWithTags\Foo::$text is never written, only read.',
139156
18,
157+
$tip,
140158
],
141159
]);
142160
}
@@ -155,10 +173,12 @@ public function testBug3636(): void
155173
}
156174
$this->alwaysWrittenTags = [];
157175
$this->alwaysReadTags = [];
176+
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
158177
$this->analyse([__DIR__ . '/data/bug-3636.php'], [
159178
[
160179
'Property Bug3636\Bar::$date is never written, only read.',
161180
22,
181+
$tip,
162182
],
163183
]);
164184
}
@@ -171,10 +191,12 @@ public function testPromotedProperties(): void
171191

172192
$this->alwaysWrittenTags = [];
173193
$this->alwaysReadTags = ['@get'];
194+
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
174195
$this->analyse([__DIR__ . '/data/unused-private-promoted-property.php'], [
175196
[
176197
'Property UnusedPrivatePromotedProperty\Foo::$lorem is never read, only written.',
177198
12,
199+
$tip,
178200
],
179201
]);
180202
}

0 commit comments

Comments
 (0)