3
3
namespace ShipMonk \PHPStan \DeadCode \Excluder ;
4
4
5
5
use Composer \Autoload \ClassLoader ;
6
+ use LogicException ;
6
7
use PhpParser \Node ;
7
8
use PHPStan \Analyser \Scope ;
8
9
use PHPStan \Reflection \ReflectionProvider ;
9
10
use ShipMonk \PHPStan \DeadCode \Graph \ClassMemberUsage ;
10
11
use function array_filter ;
11
12
use function array_keys ;
12
- use function array_values ;
13
13
use function count ;
14
14
use function dirname ;
15
15
use function file_get_contents ;
@@ -32,7 +32,7 @@ class TestsUsageExcluder implements MemberUsageExcluder
32
32
/**
33
33
* @var list<string>
34
34
*/
35
- private array $ devPaths ;
35
+ private array $ devPaths = [] ;
36
36
37
37
private bool $ enabled ;
38
38
@@ -46,8 +46,15 @@ public function __construct(
46
46
)
47
47
{
48
48
$ this ->reflectionProvider = $ reflectionProvider ;
49
- $ this ->devPaths = $ devPaths ?? $ this ->autodetectComposerDevPaths ();
50
49
$ this ->enabled = $ enabled ;
50
+
51
+ if ($ devPaths !== null ) {
52
+ foreach ($ devPaths as $ devPath ) {
53
+ $ this ->devPaths [] = $ this ->realpath ($ devPath );
54
+ }
55
+ } else {
56
+ $ this ->devPaths = $ this ->autodetectComposerDevPaths ();
57
+ }
51
58
}
52
59
53
60
public function getIdentifier (): string
@@ -61,7 +68,7 @@ public function shouldExclude(ClassMemberUsage $usage, Node $node, Scope $scope)
61
68
return false ;
62
69
}
63
70
64
- return $ this ->isWithinDevPaths ($ scope ->getFile ())
71
+ return $ this ->isWithinDevPaths ($ this -> realpath ( $ scope ->getFile () ))
65
72
&& !$ this ->isWithinDevPaths ($ this ->getDeclarationFile ($ usage ->getMemberRef ()->getClassName ()));
66
73
}
67
74
@@ -90,7 +97,13 @@ private function getDeclarationFile(?string $className): ?string
90
97
return null ;
91
98
}
92
99
93
- return $ this ->reflectionProvider ->getClass ($ className )->getFileName ();
100
+ $ filePath = $ this ->reflectionProvider ->getClass ($ className )->getFileName ();
101
+
102
+ if ($ filePath === null ) {
103
+ return null ;
104
+ }
105
+
106
+ return $ this ->realpath ($ filePath );
94
107
}
95
108
96
109
/**
@@ -181,17 +194,28 @@ private function extractAutoloadPaths(string $basePath, array $autoload): array
181
194
}
182
195
183
196
foreach ($ globPaths as $ globPath ) {
184
- $ result [] = realpath ($ globPath );
197
+ $ result [] = $ this -> realpath ($ globPath );
185
198
}
186
199
187
200
continue ;
188
201
}
189
202
190
- $ result [] = realpath ($ absolutePath );
203
+ $ result [] = $ this -> realpath ($ absolutePath );
191
204
}
192
205
}
193
206
194
- return array_values (array_filter ($ result , static fn ($ path ): bool => $ path !== false ));
207
+ return $ result ;
208
+ }
209
+
210
+ private function realpath (string $ path ): string
211
+ {
212
+ $ realPath = realpath ($ path );
213
+
214
+ if ($ realPath === false ) {
215
+ throw new LogicException ("Unable to realpath ' $ path' " );
216
+ }
217
+
218
+ return $ realPath ;
195
219
}
196
220
197
221
}
0 commit comments