File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rules ;
4+
5+ use PhpParser \Node ;
6+ use PhpParser \Node \Expr ;
7+ use PhpParser \Node \Expr \StaticCall ;
8+ use PHPStan \Rules \Rule ;
9+ use PHPStan \Analyser \Scope ;
10+ use PHPStan \Rules \RuleErrorBuilder ;
11+
12+ /**
13+ * @implements Rule<StaticCall>
14+ */
15+ final class CallStaticMethodInClassRule implements Rule
16+ {
17+ public function getNodeType (): string
18+ {
19+ return StaticCall::class;
20+ }
21+
22+ public function processNode (Node $ node , Scope $ scope ): array
23+ {
24+ /** @var StaticCall $node */
25+ if ($ this ->shouldSkip ($ node )) {
26+ return [];
27+ }
28+ if ($ scope ->isInClass ()) {
29+ return [
30+ RuleErrorBuilder::message (
31+ 'Use constructor injection for dependent classes. '
32+ )->build (),
33+ ];
34+ }
35+ return [];
36+ }
37+
38+ private function shouldSkip (StaticCall $ staticCall ): bool
39+ {
40+ if ($ staticCall ->class instanceof Expr) {
41+ return false ;
42+ }
43+ if ($ staticCall ->class ->isSpecialClassName ()) {
44+ return true ;
45+ }
46+ return false ;
47+ }
48+ }
Original file line number Diff line number Diff line change 11rules :
22 - Rules\MyRule
3+ - Rules\CallStaticMethodInClassRule
You can’t perform that action at this time.
0 commit comments