| Total Complexity | 6 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Coverage | 94.12% |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | final class PathHelper |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * PHP implementation of {@see realpath()} method returns `false` when file or directory does not exist. |
||
| 11 | * This method prevents this behavior. |
||
| 12 | * |
||
| 13 | * @param string $path |
||
| 14 | * |
||
| 15 | * @return string |
||
| 16 | */ |
||
| 17 | 12 | public static function realpath(string $path): string |
|
| 18 | { |
||
| 19 | 12 | $parts = explode('/', self::normalize($path)); |
|
| 20 | 12 | $out = []; |
|
| 21 | 12 | foreach ($parts as $part) { |
|
| 22 | 12 | if ($part === '.') { |
|
| 23 | continue; |
||
| 24 | } |
||
| 25 | 12 | if ($part === '..') { |
|
| 26 | 8 | array_pop($out); |
|
| 27 | 8 | continue; |
|
| 28 | } |
||
| 29 | 12 | $out[] = $part; |
|
| 30 | } |
||
| 31 | |||
| 32 | 12 | return implode('/', $out); |
|
| 33 | } |
||
| 34 | |||
| 35 | 20 | public static function normalize(string $path): string |
|
| 44 | } |
||
| 45 | } |
||
| 46 |