Skip to content

Commit e4652f6

Browse files
committed
Add support for StaticCall
Fixes #765
1 parent 8751661 commit e4652f6

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

src/Rules/QueryPlanAnalyzerRule.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PhpParser\Node\Expr\CallLike;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\New_;
11+
use PhpParser\Node\Expr\StaticCall;
12+
use PhpParser\Node\Identifier;
13+
use PhpParser\Node\Name;
1114
use PhpParser\Node\Name\FullyQualified;
1215
use PHPStan\Analyser\Scope;
1316
use PHPStan\Reflection\ReflectionProvider;
@@ -56,6 +59,15 @@ public function processNode(Node $callLike, Scope $scope): array
5659
}
5760

5861
$methodReflection = $scope->getMethodReflection($scope->getType($callLike->var), $callLike->name->toString());
62+
} elseif ($callLike instanceof StaticCall) {
63+
if (! $callLike->name instanceof Identifier) {
64+
return [];
65+
}
66+
if (! $callLike->class instanceof Name) {
67+
return [];
68+
}
69+
$classType = $scope->resolveTypeByName($callLike->class);
70+
$methodReflection = $scope->getMethodReflection($classType, $callLike->name->toString());
5971
} elseif ($callLike instanceof New_) {
6072
if (! $callLike->class instanceof FullyQualified) {
6173
return [];

src/Rules/SyntaxErrorInPreparedStatementMethodRule.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PhpParser\Node\Expr\CallLike;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\New_;
11+
use PhpParser\Node\Expr\StaticCall;
12+
use PhpParser\Node\Identifier;
13+
use PhpParser\Node\Name;
1114
use PhpParser\Node\Name\FullyQualified;
1215
use PHPStan\Analyser\Scope;
1316
use PHPStan\Reflection\ReflectionProvider;
@@ -57,6 +60,15 @@ public function processNode(Node $callLike, Scope $scope): array
5760
}
5861

5962
$methodReflection = $scope->getMethodReflection($scope->getType($callLike->var), $callLike->name->toString());
63+
} elseif ($callLike instanceof StaticCall) {
64+
if (! $callLike->name instanceof Identifier) {
65+
return [];
66+
}
67+
if (! $callLike->class instanceof Name) {
68+
return [];
69+
}
70+
$classType = $scope->resolveTypeByName($callLike->class);
71+
$methodReflection = $scope->getMethodReflection($classType, $callLike->name->toString());
6072
} elseif ($callLike instanceof New_) {
6173
if (! $callLike->class instanceof FullyQualified) {
6274
return [];

src/Rules/SyntaxErrorInQueryMethodRule.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace staabm\PHPStanDba\Rules;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\CallLike;
89
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\Name;
913
use PHPStan\Analyser\Scope;
1014
use PHPStan\Reflection\ReflectionProvider;
1115
use PHPStan\Rules\Rule;
@@ -15,7 +19,7 @@
1519
use staabm\PHPStanDba\UnresolvableQueryException;
1620

1721
/**
18-
* @implements Rule<MethodCall>
22+
* @implements Rule<CallLike>
1923
*
2024
* @see SyntaxErrorInQueryMethodRuleTest
2125
*/
@@ -39,16 +43,29 @@ public function __construct(array $classMethods, ReflectionProvider $reflectionP
3943

4044
public function getNodeType(): string
4145
{
42-
return MethodCall::class;
46+
return CallLike::class;
4347
}
4448

45-
public function processNode(Node $node, Scope $scope): array
49+
public function processNode(Node $callLike, Scope $scope): array
4650
{
47-
if (! $node->name instanceof Node\Identifier) {
51+
if ($callLike instanceof MethodCall) {
52+
if (! $callLike->name instanceof Identifier) {
53+
return [];
54+
}
55+
$methodReflection = $scope->getMethodReflection($scope->getType($callLike->var), $callLike->name->toString());
56+
} elseif ($callLike instanceof StaticCall) {
57+
if (! $callLike->name instanceof Identifier) {
58+
return [];
59+
}
60+
if (! $callLike->class instanceof Name) {
61+
return [];
62+
}
63+
$classType = $scope->resolveTypeByName($callLike->class);
64+
$methodReflection = $scope->getMethodReflection($classType, $callLike->name->name);
65+
} else {
4866
return [];
4967
}
5068

51-
$methodReflection = $scope->getMethodReflection($scope->getType($node->var), $node->name->toString());
5269
if (null === $methodReflection) {
5370
return [];
5471
}
@@ -79,7 +96,7 @@ public function processNode(Node $node, Scope $scope): array
7996
return [];
8097
}
8198

82-
$args = $node->getArgs();
99+
$args = $callLike->getArgs();
83100

84101
if (! \array_key_exists($queryArgPosition, $args)) {
85102
return [];
@@ -98,13 +115,13 @@ public function processNode(Node $node, Scope $scope): array
98115
$queryError = $queryReflection->validateQueryString($queryString);
99116
if (null !== $queryError) {
100117
return [
101-
RuleErrorBuilder::message($queryError->asRuleMessage())->identifier('dba.syntaxError')->line($node->getStartLine())->build(),
118+
RuleErrorBuilder::message($queryError->asRuleMessage())->identifier('dba.syntaxError')->line($callLike->getStartLine())->build(),
102119
];
103120
}
104121
}
105122
} catch (UnresolvableQueryException $exception) {
106123
return [
107-
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($node->getStartLine())->build(),
124+
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($callLike->getStartLine())->build(),
108125
];
109126
}
110127

0 commit comments

Comments
 (0)