Skip to content

Commit

Permalink
Tips for always used properties and constants extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 17, 2021
1 parent 53eb2e3 commit 2db3c26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Rules/DeadCode/UnusedPrivateConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function processNode(Node $node, Scope $scope): array
'classStartLine' => $node->getClass()->getStartLine(),
'constantName' => $constantName,
])
->tip(sprintf('See: %s', 'https://phpstan.org/developing-extensions/always-used-class-constants'))
->build();
}

Expand Down
6 changes: 4 additions & 2 deletions src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function processNode(Node $node, Scope $scope): array
} 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))
Expand All @@ -196,12 +197,13 @@ public function processNode(Node $node, Scope $scope): array
'classStartLine' => $node->getClass()->getStartLine(),
'propertyName' => $name,
])
->tip($tip)
->build();
} else {
$errors[] = RuleErrorBuilder::message(sprintf('%s is never read, only written.', $propertyName))->line($propertyNode->getStartLine())->build();
$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())->build();
$errors[] = RuleErrorBuilder::message(sprintf('%s is never written, only read.', $propertyName))->line($propertyNode->getStartLine())->tip($tip)->build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public function testRule(): void
[
'Constant UnusedPrivateConstant\Foo::BAR_CONST is unused.',
10,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
[
'Constant UnusedPrivateConstant\TestExtension::UNUSED is unused.',
23,
'See: https://phpstan.org/developing-extensions/always-used-class-constants',
],
]);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,60 +67,75 @@ public function testRule(): void
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];

$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';

$this->analyse([__DIR__ . '/data/unused-private-property.php'], [
[
'Property UnusedPrivateProperty\Foo::$bar is never read, only written.',
10,
$tip,
],
[
'Property UnusedPrivateProperty\Foo::$baz is unused.',
12,
$tip,
],
[
'Property UnusedPrivateProperty\Foo::$lorem is never written, only read.',
14,
$tip,
],
[
'Property UnusedPrivateProperty\Bar::$baz is never written, only read.',
57,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$bar is never read, only written.',
86,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$baz is unused.',
88,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$lorem is never written, only read.',
90,
$tip,
],
[
'Property UnusedPrivateProperty\Lorem::$baz is never read, only written.',
117,
$tip,
],
[
'Property class@anonymous/tests/PHPStan/Rules/DeadCode/data/unused-private-property.php:152::$bar is unused.',
153,
$tip,
],
[
'Property UnusedPrivateProperty\DolorWithAnonymous::$foo is unused.',
148,
$tip,
],
]);
$this->analyse([__DIR__ . '/data/TestExtension.php'], [
[
'Property UnusedPrivateProperty\TestExtension::$unused is unused.',
8,
$tip,
],
[
'Property UnusedPrivateProperty\TestExtension::$read is never written, only read.',
10,
$tip,
],
[
'Property UnusedPrivateProperty\TestExtension::$written is never read, only written.',
12,
$tip,
],
]);
}
Expand All @@ -129,14 +144,17 @@ public function testAlwaysUsedTags(): void
{
$this->alwaysWrittenTags = ['@ORM\Column'];
$this->alwaysReadTags = ['@get'];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/private-property-with-tags.php'], [
[
'Property PrivatePropertyWithTags\Foo::$title is never read, only written.',
13,
$tip,
],
[
'Property PrivatePropertyWithTags\Foo::$text is never written, only read.',
18,
$tip,
],
]);
}
Expand All @@ -155,10 +173,12 @@ public function testBug3636(): void
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/bug-3636.php'], [
[
'Property Bug3636\Bar::$date is never written, only read.',
22,
$tip,
],
]);
}
Expand All @@ -171,10 +191,12 @@ public function testPromotedProperties(): void

$this->alwaysWrittenTags = [];
$this->alwaysReadTags = ['@get'];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/unused-private-promoted-property.php'], [
[
'Property UnusedPrivatePromotedProperty\Foo::$lorem is never read, only written.',
12,
$tip,
],
]);
}
Expand Down

0 comments on commit 2db3c26

Please sign in to comment.