1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type\Composer\Psr; |
6: | |
7: | use PHPStan\BetterReflection\Identifier\Identifier; |
8: | |
9: | use function array_map; |
10: | use function array_merge; |
11: | use function array_unique; |
12: | use function array_values; |
13: | use function rtrim; |
14: | use function str_replace; |
15: | use function str_starts_with; |
16: | |
17: | final class Psr0Mapping implements PsrAutoloaderMapping |
18: | { |
19: | |
20: | private array $mappings = []; |
21: | |
22: | private function __construct() |
23: | { |
24: | } |
25: | |
26: | |
27: | public static function fromArrayMappings(array $mappings): self |
28: | { |
29: | $instance = new self(); |
30: | |
31: | $instance->mappings = array_map( |
32: | static function (array $directories): array { |
33: | return array_map(static fn (string $directory): string => rtrim($directory, '/'), $directories); |
34: | }, |
35: | $mappings, |
36: | ); |
37: | |
38: | return $instance; |
39: | } |
40: | |
41: | |
42: | public function resolvePossibleFilePaths(Identifier $identifier): array |
43: | { |
44: | if (! $identifier->isClass()) { |
45: | return []; |
46: | } |
47: | |
48: | $className = $identifier->getName(); |
49: | |
50: | foreach ($this->mappings as $prefix => $paths) { |
51: | if ($prefix === '') { |
52: | continue; |
53: | } |
54: | |
55: | if (strpos($className, $prefix) === 0) { |
56: | return array_map( |
57: | static fn (string $path): string => $path . '/' . str_replace(['\\', '_'], '/', $className) . '.php', |
58: | $paths, |
59: | ); |
60: | } |
61: | } |
62: | |
63: | return []; |
64: | } |
65: | |
66: | |
67: | public function directories(): array |
68: | { |
69: | return array_values(array_unique(array_merge([], ...array_values($this->mappings)))); |
70: | } |
71: | } |
72: | |