Conditions | 3 |
Paths | 4 |
Total Lines | 23 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Tests | 7 |
CRAP Score | 3.243 |
Changes | 0 |
1 | <?php |
||
10 | 1 | public static function normalizePath($path, $separator = '\\/') |
|
11 | { |
||
12 | // Remove any kind of unicode whitespace |
||
13 | 1 | $normalized = preg_replace('#\p{C}+|^\./#u', '', $path); |
|
14 | |||
15 | // Path remove self referring paths ("/./"). |
||
16 | 1 | $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized); |
|
17 | |||
18 | // Regex for resolving relative paths |
||
19 | 1 | $regex = '#\/*[^/\.]+/\.\.#Uu'; |
|
20 | |||
21 | 1 | while (preg_match($regex, $normalized)) { |
|
22 | $normalized = preg_replace($regex, '', $normalized); |
||
23 | } |
||
24 | |||
25 | 1 | if (preg_match('#/\.{2}|\.{2}/#', $normalized)) { |
|
26 | throw new InvalidPathException( |
||
27 | sprintf("Path is outside of the defined root, path: '%s', resolved: '%s'.", $path, $normalized) |
||
28 | ); |
||
29 | } |
||
30 | |||
31 | 1 | return rtrim($normalized, $separator); |
|
32 | } |
||
33 | |||
51 | } |