Skip to content

Commit febeb01

Browse files
authored
TestsExcluder: normalize provided paths (#149)
1 parent c218837 commit febeb01

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/Excluder/TestsUsageExcluder.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace ShipMonk\PHPStan\DeadCode\Excluder;
44

55
use Composer\Autoload\ClassLoader;
6+
use LogicException;
67
use PhpParser\Node;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\Reflection\ReflectionProvider;
910
use ShipMonk\PHPStan\DeadCode\Graph\ClassMemberUsage;
1011
use function array_filter;
1112
use function array_keys;
12-
use function array_values;
1313
use function count;
1414
use function dirname;
1515
use function file_get_contents;
@@ -32,7 +32,7 @@ class TestsUsageExcluder implements MemberUsageExcluder
3232
/**
3333
* @var list<string>
3434
*/
35-
private array $devPaths;
35+
private array $devPaths = [];
3636

3737
private bool $enabled;
3838

@@ -46,8 +46,15 @@ public function __construct(
4646
)
4747
{
4848
$this->reflectionProvider = $reflectionProvider;
49-
$this->devPaths = $devPaths ?? $this->autodetectComposerDevPaths();
5049
$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+
}
5158
}
5259

5360
public function getIdentifier(): string
@@ -61,7 +68,7 @@ public function shouldExclude(ClassMemberUsage $usage, Node $node, Scope $scope)
6168
return false;
6269
}
6370

64-
return $this->isWithinDevPaths($scope->getFile())
71+
return $this->isWithinDevPaths($this->realpath($scope->getFile()))
6572
&& !$this->isWithinDevPaths($this->getDeclarationFile($usage->getMemberRef()->getClassName()));
6673
}
6774

@@ -90,7 +97,13 @@ private function getDeclarationFile(?string $className): ?string
9097
return null;
9198
}
9299

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);
94107
}
95108

96109
/**
@@ -181,17 +194,28 @@ private function extractAutoloadPaths(string $basePath, array $autoload): array
181194
}
182195

183196
foreach ($globPaths as $globPath) {
184-
$result[] = realpath($globPath);
197+
$result[] = $this->realpath($globPath);
185198
}
186199

187200
continue;
188201
}
189202

190-
$result[] = realpath($absolutePath);
203+
$result[] = $this->realpath($absolutePath);
191204
}
192205
}
193206

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;
195219
}
196220

197221
}

tests/Rule/DeadCodeRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ private function getMemberUsageExcluders(): array
493493
new TestsUsageExcluder(
494494
self::createReflectionProvider(),
495495
true,
496-
[__DIR__ . '/data/excluders/tests/tests'],
496+
[__DIR__ . '/data/excluders/../excluders/tests/tests'], // tests path normalization
497497
),
498498
new class implements MemberUsageExcluder
499499
{

0 commit comments

Comments
 (0)