Skip to content

Commit d177206

Browse files
NoDiscard on hooks
1 parent ab3c28b commit d177206

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/Rules/AttributesCheck.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function check(
4040
array $attrGroups,
4141
int $requiredTarget,
4242
string $targetName,
43+
bool $isPropertyHook = false,
4344
): array
4445
{
4546
$errors = [];
@@ -81,6 +82,12 @@ public function check(
8182
->identifier('attribute.target')
8283
->line($attribute->getStartLine())
8384
->build();
85+
} elseif ($isPropertyHook && strtolower($name) === "nodiscard") {
86+
// #[\NoDiscard] cannot be used on property hooks
87+
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class %s cannot be used on property hooks.', $name))
88+
->identifier('attribute.target')
89+
->line($attribute->getStartLine())
90+
->build();
8491
}
8592

8693
if (($flags & Attribute::IS_REPEATABLE) === 0) {

src/Rules/Properties/PropertyHookAttributesRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function processNode(Node $node, Scope $scope): array
3333
$node->getOriginalNode()->attrGroups,
3434
Attribute::TARGET_METHOD,
3535
'method',
36+
true,
3637
);
3738
}
3839

tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function testRule(): void
5555
'Attribute class PropertyHookAttributes\Foo does not have the method target.',
5656
27,
5757
],
58+
[
59+
'Attribute class NoDiscard cannot be used on property hooks.',
60+
63,
61+
],
5862
]);
5963
}
6064

tests/PHPStan/Rules/Properties/data/property-hook-attributes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ class Dolor
5555
}
5656

5757
}
58+
59+
class Sit
60+
{
61+
62+
public int $i {
63+
#[\NoDiscard]
64+
get {
65+
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)