1 | <?php |
||
11 | class Path |
||
12 | { |
||
13 | /** |
||
14 | * Normalizes the provided file system path. |
||
15 | * |
||
16 | * Normalizing file system paths means that all forward and backward |
||
17 | * slashes in the path will be replaced with the system directory separator |
||
18 | * and multiple directory separators will be condensed into one. |
||
19 | * Additionally, all `.` and `..` directory references will be resolved in |
||
20 | * the returned path. |
||
21 | * |
||
22 | * Note that if the normalized path is not an absolute path, the resulting |
||
23 | * path may begin with `..` directory references if it is not possible to |
||
24 | * resolve them simply by using string handling. You should also note that |
||
25 | * if the resulting path would result in an empty string, this method will |
||
26 | * return `.` instead. |
||
27 | * |
||
28 | * If the `$prependDrive` option is enabled, the normalized path will be |
||
29 | * prepended with the drive name on Windows platforms using the current |
||
30 | * working directory, if the path is an absolute path that does not include |
||
31 | * a drive name. |
||
32 | * |
||
33 | * @param string $path File system path to normalize |
||
34 | * @param bool $prependDrive True to prepend drive name to absolute paths |
||
35 | * @return string The normalizes file system path |
||
36 | */ |
||
37 | 12 | public static function normalize($path, $prependDrive = true) |
|
47 | |||
48 | /** |
||
49 | * Joins the provided file systems paths together and normalizes the result. |
||
50 | * |
||
51 | * The paths can be provided either as multiple arguments to this method |
||
52 | * or as an array. The paths will be joined using the system directory |
||
53 | * separator and the result will be normalized similar to the normalization |
||
54 | * method (the drive letter will not be prepended however). |
||
55 | * |
||
56 | * Note that unless the first path in the list is an absolute path, the |
||
57 | * entire resulting path will be treated as a relative path. |
||
58 | * |
||
59 | * @param string[]|string $paths File system paths to join |
||
60 | * @return string The joined file system paths |
||
61 | */ |
||
62 | 45 | public static function join($paths) |
|
78 | |||
79 | /** |
||
80 | * Builds the final path from the root and path parts. |
||
81 | * @param string $root Root path |
||
82 | * @param string[] $parts The path parts |
||
83 | * @return string The final built path |
||
84 | */ |
||
85 | 39 | private static function buildPath($root, array $parts) |
|
93 | |||
94 | /** |
||
95 | * Merges the paths and returns the individual parts. |
||
96 | * @param string[] $paths Array of paths |
||
97 | * @return string[] Parts in the paths merged into a single array |
||
98 | * @throws \InvalidArgumentException If no paths have been provided |
||
99 | */ |
||
100 | 45 | private static function getParts(array $paths) |
|
108 | |||
109 | /** |
||
110 | * Tells if the path is an absolute path. |
||
111 | * @param string $path The file system path to test |
||
112 | * @return bool True if the path is an absolute path, false if not |
||
113 | */ |
||
114 | 42 | private static function isAbsolute($path) |
|
126 | |||
127 | /** |
||
128 | * Resolves parent directory references and removes redundant entries. |
||
129 | * @param string[] $parts List of parts in the the path |
||
130 | * @param bool $absolute Whether the path is an absolute path or not |
||
131 | * @return string[] Resolved list of parts in the path |
||
132 | */ |
||
133 | 42 | private static function resolve(array $parts, $absolute) |
|
147 | |||
148 | /** |
||
149 | * Tells if the part of the path is valid and not empty. |
||
150 | * @param string $path Part of the path to check for redundancy |
||
151 | * @return bool True if the path is valid and not empty, false if not |
||
152 | * @throws \InvalidArgumentException If the path contains invalid characters |
||
153 | */ |
||
154 | 42 | private static function isValidPath($path) |
|
162 | |||
163 | /** |
||
164 | * Resolves the relative parent directory for the path. |
||
165 | * @param string[] $parts Path parts to modify |
||
166 | * @param bool $absolute True if dealing with absolute path, false if not |
||
167 | */ |
||
168 | 12 | private static function resolveParent(& $parts, $absolute) |
|
177 | } |
||
178 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.