-
Notifications
You must be signed in to change notification settings - Fork 3
/
DisallowedAttributesRector.php
62 lines (52 loc) · 1.58 KB
/
DisallowedAttributesRector.php
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
<?php
namespace Worksome\CodingStyle\Rector\Generic;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class DisallowedAttributesRector extends AbstractRector implements ConfigurableRectorInterface
{
/** @var array<class-string> */
private array $disallowedAttributes = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove attributes which are not allowed.',
[
new CodeSample(
<<<PHP
#[NotAllowed]
class MyClass {}
PHP,
'class MyClass {}',
),
]
);
}
/** {@inheritdoc} */
public function getNodeTypes(): array
{
return [Node\AttributeGroup::class];
}
/** {@inheritdoc} */
public function configure(array $configuration): void
{
$this->disallowedAttributes = $configuration;
}
/** @param Node\AttributeGroup $node */
public function refactor(Node $node)
{
foreach ($node->attrs as $key => $attribute) {
if (! $this->isNames($attribute, $this->disallowedAttributes)) {
continue;
}
unset($node->attrs[$key]);
}
if ($node->attrs === []) {
return NodeTraverser::REMOVE_NODE;
}
return null;
}
}