Skip to content

Commit

Permalink
Support more attribute targets: properties, class constants, params
Browse files Browse the repository at this point in the history
Close #224
  • Loading branch information
spaze committed Dec 6, 2023
1 parent f0a6f69 commit 0f5c4e1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Usages/AttributeUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use Spaze\PHPStan\Rules\Disallowed\DisallowedAttribute;
Expand Down Expand Up @@ -72,6 +75,12 @@ public function processNode(Node $node, Scope $scope): array
$this->addAttrs(array_values($node->attrGroups));
} elseif ($node instanceof FunctionLike) {
$this->addAttrs(array_values($node->getAttrGroups()));
} elseif ($node instanceof Property) {
$this->addAttrs(array_values($node->attrGroups));
} elseif ($node instanceof ClassConst) {
$this->addAttrs(array_values($node->attrGroups));
} elseif ($node instanceof Param) {
$this->addAttrs(array_values($node->attrGroups));
} else {
return [];
}
Expand Down
34 changes: 33 additions & 1 deletion tests/Usages/AttributeUsagesAllowParamsMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,25 @@ public function testRule(): void
// on this line:
8,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
12,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
15,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
18,
],
[
'Attribute Attributes\AttributeClass is forbidden.',
30,
40,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
41,
],
]);
$this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], [
Expand All @@ -85,10 +101,26 @@ public function testRule(): void
'Attribute Attributes\AttributeEntity is forbidden.',
12,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
15,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
18,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
22,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
28,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
41,
],
]);
}

Expand Down
18 changes: 17 additions & 1 deletion tests/Usages/AttributeUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,25 @@ public function testRule(): void
// on this line:
8,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
12,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
15,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
18,
],
[
'Attribute Attributes\AttributeClass is forbidden.',
30,
40,
],
[
'Attribute Attributes\AttributeEntity is forbidden.',
41,
],
]);
$this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], []);
Expand Down
14 changes: 12 additions & 2 deletions tests/src/disallowed-allow/ClassWithAttributesAllow.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
class ClassWithAttributesAllow
{

#[AttributeEntity] // allowed by path in all tests
private const MAYO = true;

#[AttributeEntity] // allowed by path in all tests
public $cheddar = 'plz';

#[AttributeEntity] // disallowed
public static $pepper = 'ofc';


#[\Attributes\AttributeEntity(repositoryClass: \Attributes\UserRepository::class, readOnly: false)] // allowed by path in AttributeUsagesTest, disallowed in AttributeUsagesAllowParamsMultipleTest because $repositoryClass has other value
public function hasAvocado(): bool
{
Expand All @@ -27,8 +37,8 @@ public function hasKetchup(): bool
}


#[AttributeClass()] // allowed by path in all tests
public function hasPineapple(): bool
#[AttributeClass()] // both the method and the param attributes allowed by path in all tests
public function hasPineapple(#[AttributeEntity] bool $really): bool
{
}

Expand Down
14 changes: 12 additions & 2 deletions tests/src/disallowed/ClassWithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
class ClassWithAttributes
{

#[AttributeEntity] // disallowed
private const MAYO = true;

#[AttributeEntity] // disallowed
public $cheddar = 'plz';

#[AttributeEntity] // disallowed
public static $pepper = 'ofc';


#[AttributeEntity(repositoryClass: UserRepository::class, readOnly: false)] // disallowed, $repositoryClass present with any value
public function hasAvocado(): bool
{
Expand All @@ -27,8 +37,8 @@ public function hasKetchup(): bool
}


#[AttributeClass()] // disallowed
public function hasPineapple(): bool
#[AttributeClass()] // both the method and the param attributes disallowed
public function hasPineapple(#[AttributeEntity] bool $really): bool
{
}

Expand Down

0 comments on commit 0f5c4e1

Please sign in to comment.