-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathPhpAttributeAnalyzer.php
More file actions
97 lines (81 loc) · 2.52 KB
/
PhpAttributeAnalyzer.php
File metadata and controls
97 lines (81 loc) · 2.52 KB
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
<?php
declare(strict_types=1);
namespace Rector\Php80\NodeAnalyzer;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpAttribute\Enum\DocTagNodeState;
final readonly class PhpAttributeAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
) {
}
public function hasPhpAttribute(Property | ClassLike | ClassMethod | Function_ | Param $node, string $attributeClass): bool
{
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if (! $this->nodeNameResolver->isName($attribute->name, $attributeClass)) {
continue;
}
return true;
}
}
return false;
}
/**
* @param string[] $attributeClasses
*/
public function hasPhpAttributes(Property | ClassLike | ClassMethod | Function_ | Param $node, array $attributeClasses): bool
{
foreach ($attributeClasses as $attributeClass) {
if ($this->hasPhpAttribute($node, $attributeClass)) {
return true;
}
}
return false;
}
/**
* @param AttributeGroup[] $attributeGroups
*/
public function hasRemoveArrayState(array $attributeGroups): bool
{
foreach ($attributeGroups as $attributeGroup) {
foreach ($attributeGroup->attrs as $attribute) {
$args = $attribute->args;
if ($this->hasArgWithRemoveArrayValue($args)) {
return true;
}
}
}
return false;
}
/**
* @param Arg[] $args
*/
private function hasArgWithRemoveArrayValue(array $args): bool
{
foreach ($args as $arg) {
if (! $arg->value instanceof Array_) {
continue;
}
foreach ($arg->value->items as $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if ($item->value instanceof String_ && $item->value->value === DocTagNodeState::REMOVE_ARRAY) {
return true;
}
}
}
return false;
}
}