55namespace staabm \PHPStanDba \Rules ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Expr \CallLike ;
89use PhpParser \Node \Expr \MethodCall ;
10+ use PhpParser \Node \Expr \StaticCall ;
11+ use PhpParser \Node \Identifier ;
12+ use PhpParser \Node \Name ;
913use PHPStan \Analyser \Scope ;
1014use PHPStan \Reflection \ReflectionProvider ;
1115use PHPStan \Rules \Rule ;
1519use 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