Skip to content

Commit c24bcf4

Browse files
authored
src/Rules/Api/*Rule: cheap checks first (#6206)
1 parent d6f211f commit c24bcf4

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

‎src/Rules/Api/NodeConnectingVisitorAttributesRule.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ public function processNode(Node $node, Scope $scope): array
3434
if ($node->name->toLowerString() !== 'getattribute') {
3535
return [];
3636
}
37-
$calledOnType = $scope->getType($node->var);
38-
if (!(new ObjectType(Node::class))->isSuperTypeOf($calledOnType)->yes()) {
39-
return [];
40-
}
37+
4138
$args = $node->getArgs();
4239
if (!isset($args[0])) {
4340
return [];
4441
}
4542

43+
$calledOnType = $scope->getType($node->var);
44+
if (!(new ObjectType(Node::class))->isSuperTypeOf($calledOnType)->yes()) {
45+
return [];
46+
}
47+
4648
$messages = [];
4749
$argType = $scope->getType($args[0]->value);
4850
foreach ($argType->getConstantStrings() as $constantString) {

‎src/Rules/Api/OldPhpParser4ClassRule.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function getNodeType(): string
4242

4343
public function processNode(Node $node, Scope $scope): array
4444
{
45+
if (!$scope->isInClass()) {
46+
return [];
47+
}
48+
4549
$nameMapping = array_change_key_case(self::NAME_MAPPING);
4650
$lowerName = $node->toLowerString();
4751
if (!array_key_exists($lowerName, $nameMapping)) {
@@ -50,10 +54,6 @@ public function processNode(Node $node, Scope $scope): array
5054

5155
$newName = $nameMapping[$lowerName];
5256

53-
if (!$scope->isInClass()) {
54-
return [];
55-
}
56-
5757
$classReflection = $scope->getClassReflection();
5858
$hasPhpStanInterface = false;
5959
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) {

‎src/Rules/Api/RuntimeReflectionFunctionRule.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function processNode(Node $node, Scope $scope): array
3535
return [];
3636
}
3737

38+
if (!$scope->isInClass()) {
39+
return [];
40+
}
41+
3842
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
3943
return [];
4044
}
@@ -50,10 +54,6 @@ public function processNode(Node $node, Scope $scope): array
5054
return [];
5155
}
5256

53-
if (!$scope->isInClass()) {
54-
return [];
55-
}
56-
5757
$classReflection = $scope->getClassReflection();
5858
$hasPhpStanInterface = false;
5959
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) {

‎src/Rules/Api/RuntimeReflectionInstantiationRule.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public function processNode(Node $node, Scope $scope): array
4545
return [];
4646
}
4747

48+
if (!$scope->isInClass()) {
49+
return [];
50+
}
51+
4852
$className = $scope->resolveName($node->class);
4953
if (!$this->reflectionProvider->hasClass($className)) {
5054
return [];
@@ -69,10 +73,6 @@ public function processNode(Node $node, Scope $scope): array
6973
return [];
7074
}
7175

72-
if (!$scope->isInClass()) {
73-
return [];
74-
}
75-
7676
$scopeClassReflection = $scope->getClassReflection();
7777
$hasPhpStanInterface = false;
7878
foreach (array_keys($scopeClassReflection->getInterfaces()) as $interfaceName) {

0 commit comments

Comments
 (0)