Skip to content

Commit 773abc1

Browse files
committed
feat: create custom rule
1 parent 38a9b0d commit 773abc1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
rules:
22
- Rules\MyRule
3+
- Rules\CallStaticMethodInClassRule

0 commit comments

Comments
 (0)