fabiang /
exception-generator
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Fabiang\ExceptionGenerator\Resolver; |
||
| 6 | |||
| 7 | use function array_diff; |
||
| 8 | use function array_reverse; |
||
| 9 | use function count; |
||
| 10 | use function current; |
||
| 11 | use function explode; |
||
| 12 | use function file_get_contents; |
||
| 13 | use function implode; |
||
| 14 | use function json_decode; |
||
| 15 | use function key; |
||
| 16 | use function ltrim; |
||
| 17 | use function preg_replace; |
||
| 18 | use function rtrim; |
||
| 19 | |||
| 20 | class ComposerResolver implements ResolverInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * {@inheritDoc} |
||
| 24 | */ |
||
| 25 | 11 | public function resolve(string $path, array $loopedDirectories): string|false |
|
| 26 | { |
||
| 27 | 11 | $namespace = false; |
|
| 28 | 11 | $jsonFile = file_get_contents($path); |
|
| 29 | 11 | $json = json_decode($jsonFile, true); |
|
| 30 | |||
| 31 | 11 | if (null !== $json && isset($json['autoload'])) { |
|
| 32 | 6 | $autoload = $json['autoload']; |
|
| 33 | 6 | if (isset($autoload['psr-4'])) { |
|
| 34 | 3 | $namespaces = $autoload['psr-4']; |
|
| 35 | 3 | $namespace = key($namespaces); |
|
| 36 | 3 | $path = current($namespaces); |
|
| 37 | 3 | } elseif (isset($autoload['psr-0'])) { |
|
| 38 | 1 | $namespaces = $autoload['psr-0']; |
|
| 39 | 1 | $namespace = key($namespaces); |
|
| 40 | 1 | $path = current($namespaces); |
|
| 41 | } |
||
| 42 | |||
| 43 | 6 | if (false !== $namespace) { |
|
| 44 | 4 | $namespace = rtrim(preg_replace('/\s+/', '', $namespace), '\\'); |
|
| 45 | |||
| 46 | 4 | $namespaceDiff = array_reverse(array_diff($loopedDirectories, explode('/', $path))); |
|
| 47 | 4 | $namespaceDiff = array_diff($namespaceDiff, explode('\\', $namespace)); |
|
| 48 | |||
| 49 | 4 | if (count($namespaceDiff) > 0) { |
|
| 50 | 1 | $namespace .= '\\' . implode('\\', $namespaceDiff); |
|
| 51 | } |
||
| 52 | 4 | $namespace = ltrim($namespace, '\\'); |
|
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | 11 | return $namespace; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 57 | } |
||
| 58 | } |
||
| 59 |