| Total Complexity | 6 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| 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 | * @return string |
||
| 15 | */ |
||
| 16 | public static function realpath(string $path): string |
||
| 17 | { |
||
| 18 | $parts = explode('/', self::normalize($path)); |
||
| 19 | $out = []; |
||
| 20 | foreach ($parts as $part) { |
||
| 21 | if ($part === '.') { |
||
| 22 | continue; |
||
| 23 | } |
||
| 24 | if ($part === '..') { |
||
| 25 | array_pop($out); |
||
| 26 | continue; |
||
| 27 | } |
||
| 28 | $out[] = $part; |
||
| 29 | } |
||
| 30 | |||
| 31 | return implode('/', $out); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function normalize(string $path): string |
||
| 43 | } |
||
| 44 | } |
||
| 45 |