3
3
namespace PHPStan \Rules \Keywords ;
4
4
5
5
use PhpParser \Node ;
6
- use PhpParser \Node \Expr \BinaryOp \Concat ;
7
- use PhpParser \Node \Expr \ClassConstFetch ;
8
- use PhpParser \Node \Expr \ConstFetch ;
9
6
use PhpParser \Node \Expr \Include_ ;
10
- use PhpParser \Node \Identifier ;
11
- use PhpParser \Node \Name ;
12
- use PhpParser \Node \Scalar \MagicConst \Dir ;
13
- use PhpParser \Node \Scalar \String_ ;
14
7
use PHPStan \Analyser \Scope ;
15
- use PHPStan \Reflection \ReflectionProvider ;
16
8
use PHPStan \Rules \Rule ;
17
9
use PHPStan \Rules \RuleErrorBuilder ;
18
- use function constant ;
19
- use function defined ;
20
- use function dirname ;
21
10
use function is_string ;
22
11
use function sprintf ;
23
12
26
15
*/
27
16
final class RequireFileExistsRule implements Rule
28
17
{
29
-
30
- public function __construct (private ReflectionProvider $ reflectionProvider )
31
- {
32
- }
33
-
34
18
public function getNodeType (): string
35
19
{
36
20
return Include_::class;
@@ -39,7 +23,7 @@ public function getNodeType(): string
39
23
public function processNode (Node $ node , Scope $ scope ): array
40
24
{
41
25
if ($ this ->shouldProcessNode ($ node )) {
42
- $ filePath = $ this ->resolveFilePath ($ node-> expr , $ scope );
26
+ $ filePath = $ this ->resolveFilePath ($ node , $ scope );
43
27
if (is_string ($ filePath ) && !is_file ($ filePath )) {
44
28
return [
45
29
$ this ->getErrorMessage ($ node , $ filePath )->build ()
@@ -73,67 +57,11 @@ private function getErrorMessage(Include_ $node, string $filePath): RuleErrorBui
73
57
);
74
58
}
75
59
76
- private function resolveFilePath (Node $ node , Scope $ scope ): ?string
77
- {
78
- if ($ node instanceof String_) {
79
- return $ node ->value ;
80
- }
81
-
82
- if ($ node instanceof Dir) {
83
- return dirname ($ scope ->getFile ());
84
- }
85
-
86
- if ($ node instanceof ClassConstFetch) {
87
- return $ this ->resolveClassConstant ($ node );
88
- }
89
-
90
- if ($ node instanceof ConstFetch) {
91
- return $ this ->resolveConstant ($ node );
92
- }
93
-
94
- if ($ node instanceof Concat) {
95
- $ left = $ this ->resolveFilePath ($ node ->left , $ scope );
96
- $ right = $ this ->resolveFilePath ($ node ->right , $ scope );
97
- if ($ left !== null && $ right !== null ) {
98
- return $ left . $ right ;
99
- }
100
- }
101
-
102
- return null ;
103
- }
104
-
105
- private function resolveClassConstant (ClassConstFetch $ node ): ?string
60
+ private function resolveFilePath (Include_ $ node , Scope $ scope ): ?string
106
61
{
107
- if ($ node ->class instanceof Name && $ node ->name instanceof Identifier) {
108
- $ className = (string ) $ node ->class ;
109
- $ constantName = $ node ->name ->toString ();
62
+ $ type = $ scope ->getType ($ node ->expr );
63
+ $ paths = $ type ->getConstantStrings ();
110
64
111
- if ($ this ->reflectionProvider ->hasClass ($ className )) {
112
- $ classReflection = $ this ->reflectionProvider ->getClass ($ className );
113
- if ($ classReflection ->hasConstant ($ constantName )) {
114
- $ constantReflection = $ classReflection ->getConstant ($ constantName );
115
- $ constantValue = $ constantReflection ->getValue ();
116
- if (is_string ($ constantValue )) {
117
- return $ constantValue ;
118
- }
119
- }
120
- }
121
- }
122
- return null ;
65
+ return $ paths ? $ paths [0 ]->getValue () : null ;
123
66
}
124
-
125
- private function resolveConstant (ConstFetch $ node ): ?string
126
- {
127
- if ($ node ->name instanceof Name) {
128
- $ constantName = (string ) $ node ->name ;
129
- if (defined ($ constantName )) {
130
- $ constantValue = constant ($ constantName );
131
- if (is_string ($ constantValue )) {
132
- return $ constantValue ;
133
- }
134
- }
135
- }
136
- return null ;
137
- }
138
-
139
67
}
0 commit comments