-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAvoidFeatureSetAttributeInRectorRule.php
More file actions
100 lines (79 loc) · 2.96 KB
/
AvoidFeatureSetAttributeInRectorRule.php
File metadata and controls
100 lines (79 loc) · 2.96 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
98
99
100
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules\Rector;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use Rector\Rector\AbstractRector;
use Symplify\PHPStanRules\Enum\RuleIdentifier\RectorRuleIdentifier;
use Symplify\PHPStanRules\Helper\NamingHelper;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\Rector\AvoidFeatureSetAttributeInRectorRule\AvoidFeatureSetAttributeInRectorRuleTest
*
* @implements Rule<InClassNode>
*/
final class AvoidFeatureSetAttributeInRectorRule implements Rule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Instead of using Rector rule to setAttribute("%s") to be used later, create a service extending "DecoratingNodeVisitorInterface". This ensures attribute decoration and node changes are in 2 separated steps.';
/**
* @var string[]
*/
private const ALLOWED_ATTRIBUTES = ['kind', 'origNode', 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'];
public function getNodeType(): string
{
return InClassNode::class;
}
/**
* @param InClassNode $node
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return [];
}
if (! $classReflection->is(AbstractRector::class)) {
return [];
}
$classLike = $node->getOriginalNode();
$nodeFinder = new NodeFinder();
/** @var MethodCall[] $methodCalls */
$methodCalls = $nodeFinder->findInstanceOf($classLike, MethodCall::class);
$ruleErrors = [];
foreach ($methodCalls as $methodCall) {
if (! NamingHelper::isName($methodCall->name, 'setAttribute')) {
continue;
}
$attributeName = $this->resolveAttributeKeyValue($methodCall, $scope);
if (! is_string($attributeName)) {
continue;
}
if (in_array($attributeName, self::ALLOWED_ATTRIBUTES, true)) {
continue;
}
$ruleError = RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $attributeName))
->identifier(RectorRuleIdentifier::AVOID_FEATURE_SET_ATTRIBUTE_IN_RECTOR)
->build();
$ruleErrors[] = $ruleError;
}
return $ruleErrors;
}
private function resolveAttributeKeyValue(MethodCall $methodCall, Scope $scope): ?string
{
$firstArg = $methodCall->getArgs()[0];
$attributeNameType = $scope->getType($firstArg->value);
if (! $attributeNameType instanceof ConstantStringType) {
return null;
}
return $attributeNameType->getValue();
}
}