-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathPreferDirectIsNameRule.php
More file actions
76 lines (60 loc) · 1.77 KB
/
PreferDirectIsNameRule.php
File metadata and controls
76 lines (60 loc) · 1.77 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
<?php
declare(strict_types=1);
namespace Rector\Utils\PHPStan\Rule;
/**
* @todo outsource to symplify/phpstan-rules later
*/
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Rector\Rector\AbstractRector;
final class PreferDirectIsNameRule implements Rule
{
public const ERROR_MESSAGE = 'Use direct $this->isName() call instead of fetching NodeNameResolver service';
public function getNodeType(): string
{
return MethodCall::class;
}
/**
* @param MethodCall $node
*/
public function processNode(Node $node, Scope $scope): array
{
if ($node->isFirstClassCallable()) {
return [];
}
if (! $node->name instanceof Identifier) {
return [];
}
if (! in_array($node->name, ['isName', 'isNames', 'getName'])) {
return [];
}
if (! $scope->isInClass()) {
return [];
}
$classReflection = $scope->getClassReflection();
// skip self
if ($classReflection->getName() === AbstractRector::class) {
return [];
}
// check rector rules only
if (! $classReflection->is(AbstractRector::class)) {
return [];
}
// check child Rectors only
if ($classReflection->isAbstract()) {
return [];
}
if (! $node->var instanceof PropertyFetch) {
return [];
}
$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('rector.preferDirectIsName')
->build();
return [$identifierRuleError];
}
}