Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Path often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Path, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class Path |
||
31 | { |
||
32 | /** |
||
33 | * The number of buffer entries that triggers a cleanup operation. |
||
34 | */ |
||
35 | const CLEANUP_THRESHOLD = 1250; |
||
36 | |||
37 | /** |
||
38 | * The buffer size after the cleanup operation. |
||
39 | */ |
||
40 | const CLEANUP_SIZE = 1000; |
||
41 | |||
42 | /** |
||
43 | * Buffers input/output of {@link canonicalize()}. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private static $buffer = array(); |
||
48 | |||
49 | /** |
||
50 | * The size of the buffer. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | private static $bufferSize = 0; |
||
55 | |||
56 | /** |
||
57 | * Canonicalizes the given path. |
||
58 | * |
||
59 | * During normalization, all slashes are replaced by forward slashes ("/"). |
||
60 | * Furthermore, all "." and ".." segments are removed as far as possible. |
||
61 | * ".." segments at the beginning of relative paths are not removed. |
||
62 | * |
||
63 | * ```php |
||
64 | * echo Path::canonicalize("\webmozart\puli\..\css\style.css"); |
||
65 | * // => /webmozart/css/style.css |
||
66 | * |
||
67 | * echo Path::canonicalize("../css/./style.css"); |
||
68 | * // => ../css/style.css |
||
69 | * ``` |
||
70 | * |
||
71 | * This method is able to deal with both UNIX and Windows paths. |
||
72 | * |
||
73 | * @param string $path A path string. |
||
74 | * |
||
75 | * @return string The canonical path. |
||
76 | * |
||
77 | * @since 1.0 Added method. |
||
78 | * @since 2.0 Method now fails if $path is not a string. |
||
79 | * @since 2.1 Added support for `~`. |
||
80 | */ |
||
81 | 512 | public static function canonicalize($path) |
|
141 | |||
142 | /** |
||
143 | * Normalizes the given path. |
||
144 | * |
||
145 | * During normalization, all slashes are replaced by forward slashes ("/"). |
||
146 | * Contrary to {@link canonicalize()}, this method does not remove invalid |
||
147 | * or dot path segments. Consequently, it is much more efficient and should |
||
148 | * be used whenever the given path is known to be a valid, absolute system |
||
149 | * path. |
||
150 | * |
||
151 | * This method is able to deal with both UNIX and Windows paths. |
||
152 | * |
||
153 | * @param string $path A path string. |
||
154 | * |
||
155 | * @return string The normalized path. |
||
156 | * |
||
157 | * @since 2.2 Added method. |
||
158 | */ |
||
159 | 2 | public static function normalize($path) |
|
165 | |||
166 | /** |
||
167 | * Returns the directory part of the path. |
||
168 | * |
||
169 | * This method is similar to PHP's dirname(), but handles various cases |
||
170 | * where dirname() returns a weird result: |
||
171 | * |
||
172 | * - dirname() does not accept backslashes on UNIX |
||
173 | * - dirname("C:/webmozart") returns "C:", not "C:/" |
||
174 | * - dirname("C:/") returns ".", not "C:/" |
||
175 | * - dirname("C:") returns ".", not "C:/" |
||
176 | * - dirname("webmozart") returns ".", not "" |
||
177 | * - dirname() does not canonicalize the result |
||
178 | * |
||
179 | * This method fixes these shortcomings and behaves like dirname() |
||
180 | * otherwise. |
||
181 | * |
||
182 | * The result is a canonical path. |
||
183 | * |
||
184 | * @param string $path A path string. |
||
185 | * |
||
186 | * @return string The canonical directory part. Returns the root directory |
||
187 | * if the root directory is passed. Returns an empty string |
||
188 | * if a relative path is passed that contains no slashes. |
||
189 | * Returns an empty string if an empty string is passed. |
||
190 | * |
||
191 | * @since 1.0 Added method. |
||
192 | * @since 2.0 Method now fails if $path is not a string. |
||
193 | */ |
||
194 | 41 | public static function getDirectory($path) |
|
226 | |||
227 | /** |
||
228 | * Returns canonical path of the user's home directory. |
||
229 | * |
||
230 | * Supported operating systems: |
||
231 | * |
||
232 | * - UNIX |
||
233 | * - Windows8 and upper |
||
234 | * |
||
235 | * If your operation system or environment isn't supported, an exception is thrown. |
||
236 | * |
||
237 | * The result is a canonical path. |
||
238 | * |
||
239 | * @return string The canonical home directory |
||
240 | * |
||
241 | * @throws RuntimeException If your operation system or environment isn't supported |
||
242 | * |
||
243 | * @since 2.1 Added method. |
||
244 | */ |
||
245 | 13 | public static function getHomeDirectory() |
|
263 | |||
264 | /** |
||
265 | * Returns the root directory of a path. |
||
266 | * |
||
267 | * The result is a canonical path. |
||
268 | * |
||
269 | * @param string $path A path string. |
||
270 | * |
||
271 | * @return string The canonical root directory. Returns an empty string if |
||
272 | * the given path is relative or empty. |
||
273 | * |
||
274 | * @since 1.0 Added method. |
||
275 | * @since 2.0 Method now fails if $path is not a string. |
||
276 | */ |
||
277 | 18 | public static function getRoot($path) |
|
315 | |||
316 | /** |
||
317 | * Returns the file name from a file path. |
||
318 | * |
||
319 | * @param string $path The path string. |
||
320 | * |
||
321 | * @return string The file name. |
||
322 | * |
||
323 | * @since 1.1 Added method. |
||
324 | * @since 2.0 Method now fails if $path is not a string. |
||
325 | */ |
||
326 | 8 | public static function getFilename($path) |
|
336 | |||
337 | /** |
||
338 | * Returns the file name without the extension from a file path. |
||
339 | * |
||
340 | * @param string $path The path string. |
||
341 | * @param string|null $extension If specified, only that extension is cut |
||
342 | * off (may contain leading dot). |
||
343 | * |
||
344 | * @return string The file name without extension. |
||
345 | * |
||
346 | * @since 1.1 Added method. |
||
347 | * @since 2.0 Method now fails if $path or $extension have invalid types. |
||
348 | */ |
||
349 | 20 | public static function getFilenameWithoutExtension($path, $extension = null) |
|
365 | |||
366 | /** |
||
367 | * Returns the extension from a file path. |
||
368 | * |
||
369 | * @param string $path The path string. |
||
370 | * @param bool $forceLowerCase Forces the extension to be lower-case |
||
371 | * (requires mbstring extension for correct |
||
372 | * multi-byte character handling in extension). |
||
373 | * |
||
374 | * @return string The extension of the file path (without leading dot). |
||
375 | * |
||
376 | * @since 1.1 Added method. |
||
377 | * @since 2.0 Method now fails if $path is not a string. |
||
378 | */ |
||
379 | 51 | public static function getExtension($path, $forceLowerCase = false) |
|
395 | |||
396 | /** |
||
397 | * Returns whether the path has an extension. |
||
398 | * |
||
399 | * @param string $path The path string. |
||
400 | * @param string|array|null $extensions If null or not provided, checks if |
||
401 | * an extension exists, otherwise |
||
402 | * checks for the specified extension |
||
403 | * or array of extensions (with or |
||
404 | * without leading dot). |
||
405 | * @param bool $ignoreCase Whether to ignore case-sensitivity |
||
406 | * (requires mbstring extension for |
||
407 | * correct multi-byte character |
||
408 | * handling in the extension). |
||
409 | * |
||
410 | * @return bool Returns `true` if the path has an (or the specified) |
||
411 | * extension and `false` otherwise. |
||
412 | * |
||
413 | * @since 1.1 Added method. |
||
414 | * @since 2.0 Method now fails if $path or $extensions have invalid types. |
||
415 | */ |
||
416 | 30 | public static function hasExtension($path, $extensions = null, $ignoreCase = false) |
|
444 | |||
445 | /** |
||
446 | * Changes the extension of a path string. |
||
447 | * |
||
448 | * @param string $path The path string with filename.ext to change. |
||
449 | * @param string $extension New extension (with or without leading dot). |
||
450 | * |
||
451 | * @return string The path string with new file extension. |
||
452 | * |
||
453 | * @since 1.1 Added method. |
||
454 | * @since 2.0 Method now fails if $path or $extension is not a string. |
||
455 | */ |
||
456 | 14 | public static function changeExtension($path, $extension) |
|
479 | |||
480 | /** |
||
481 | * Returns whether a path is absolute. |
||
482 | * |
||
483 | * @param string $path A path string. |
||
484 | * |
||
485 | * @return bool Returns true if the path is absolute, false if it is |
||
486 | * relative or empty. |
||
487 | * |
||
488 | * @since 1.0 Added method. |
||
489 | * @since 2.0 Method now fails if $path is not a string. |
||
490 | */ |
||
491 | 111 | public static function isAbsolute($path) |
|
524 | |||
525 | /** |
||
526 | * Returns whether a path is relative. |
||
527 | * |
||
528 | * @param string $path A path string. |
||
529 | * |
||
530 | * @return bool Returns true if the path is relative or empty, false if |
||
531 | * it is absolute. |
||
532 | * |
||
533 | * @since 1.0 Added method. |
||
534 | * @since 2.0 Method now fails if $path is not a string. |
||
535 | */ |
||
536 | 16 | public static function isRelative($path) |
|
540 | |||
541 | /** |
||
542 | * Turns a relative path into an absolute path. |
||
543 | * |
||
544 | * Usually, the relative path is appended to the given base path. Dot |
||
545 | * segments ("." and "..") are removed/collapsed and all slashes turned |
||
546 | * into forward slashes. |
||
547 | * |
||
548 | * ```php |
||
549 | * echo Path::makeAbsolute("../style.css", "/webmozart/puli/css"); |
||
550 | * // => /webmozart/puli/style.css |
||
551 | * ``` |
||
552 | * |
||
553 | * If an absolute path is passed, that path is returned unless its root |
||
554 | * directory is different than the one of the base path. In that case, an |
||
555 | * exception is thrown. |
||
556 | * |
||
557 | * ```php |
||
558 | * Path::makeAbsolute("/style.css", "/webmozart/puli/css"); |
||
559 | * // => /style.css |
||
560 | * |
||
561 | * Path::makeAbsolute("C:/style.css", "C:/webmozart/puli/css"); |
||
562 | * // => C:/style.css |
||
563 | * |
||
564 | * Path::makeAbsolute("C:/style.css", "/webmozart/puli/css"); |
||
565 | * // InvalidArgumentException |
||
566 | * ``` |
||
567 | * |
||
568 | * If the base path is not an absolute path, an exception is thrown. |
||
569 | * |
||
570 | * The result is a canonical path. |
||
571 | * |
||
572 | * @param string $path A path to make absolute. |
||
573 | * @param string $basePath An absolute base path. |
||
574 | * |
||
575 | * @return string An absolute path in canonical form. |
||
576 | * |
||
577 | * @throws InvalidArgumentException If the base path is not absolute or if |
||
578 | * the given path is an absolute path with |
||
579 | * a different root than the base path. |
||
580 | * |
||
581 | * @since 1.0 Added method. |
||
582 | * @since 2.0 Method now fails if $path or $basePath is not a string. |
||
583 | * @since 2.2.2 Method does not fail anymore of $path and $basePath are |
||
584 | * absolute, but on different partitions. |
||
585 | */ |
||
586 | 82 | public static function makeAbsolute($path, $basePath) |
|
610 | |||
611 | /** |
||
612 | * Turns a path into a relative path. |
||
613 | * |
||
614 | * The relative path is created relative to the given base path: |
||
615 | * |
||
616 | * ```php |
||
617 | * echo Path::makeRelative("/webmozart/style.css", "/webmozart/puli"); |
||
618 | * // => ../style.css |
||
619 | * ``` |
||
620 | * |
||
621 | * If a relative path is passed and the base path is absolute, the relative |
||
622 | * path is returned unchanged: |
||
623 | * |
||
624 | * ```php |
||
625 | * Path::makeRelative("style.css", "/webmozart/puli/css"); |
||
626 | * // => style.css |
||
627 | * ``` |
||
628 | * |
||
629 | * If both paths are relative, the relative path is created with the |
||
630 | * assumption that both paths are relative to the same directory: |
||
631 | * |
||
632 | * ```php |
||
633 | * Path::makeRelative("style.css", "webmozart/puli/css"); |
||
634 | * // => ../../../style.css |
||
635 | * ``` |
||
636 | * |
||
637 | * If both paths are absolute, their root directory must be the same, |
||
638 | * otherwise an exception is thrown: |
||
639 | * |
||
640 | * ```php |
||
641 | * Path::makeRelative("C:/webmozart/style.css", "/webmozart/puli"); |
||
642 | * // InvalidArgumentException |
||
643 | * ``` |
||
644 | * |
||
645 | * If the passed path is absolute, but the base path is not, an exception |
||
646 | * is thrown as well: |
||
647 | * |
||
648 | * ```php |
||
649 | * Path::makeRelative("/webmozart/style.css", "webmozart/puli"); |
||
650 | * // InvalidArgumentException |
||
651 | * ``` |
||
652 | * |
||
653 | * If the base path is not an absolute path, an exception is thrown. |
||
654 | * |
||
655 | * The result is a canonical path. |
||
656 | * |
||
657 | * @param string $path A path to make relative. |
||
658 | * @param string $basePath A base path. |
||
659 | * |
||
660 | * @return string A relative path in canonical form. |
||
661 | * |
||
662 | * @throws InvalidArgumentException If the base path is not absolute or if |
||
663 | * the given path has a different root |
||
664 | * than the base path. |
||
665 | * |
||
666 | * @since 1.0 Added method. |
||
667 | * @since 2.0 Method now fails if $path or $basePath is not a string. |
||
668 | */ |
||
669 | 97 | public static function makeRelative($path, $basePath) |
|
741 | |||
742 | /** |
||
743 | * Returns whether the given path is on the local filesystem. |
||
744 | * |
||
745 | * @param string $path A path string. |
||
746 | * |
||
747 | * @return bool Returns true if the path is local, false for a URL. |
||
748 | * |
||
749 | * @since 1.0 Added method. |
||
750 | * @since 2.0 Method now fails if $path is not a string. |
||
751 | */ |
||
752 | 6 | public static function isLocal($path) |
|
758 | |||
759 | /** |
||
760 | * Returns the longest common base path of a set of paths. |
||
761 | * |
||
762 | * Dot segments ("." and "..") are removed/collapsed and all slashes turned |
||
763 | * into forward slashes. |
||
764 | * |
||
765 | * ```php |
||
766 | * $basePath = Path::getLongestCommonBasePath(array( |
||
767 | * '/webmozart/css/style.css', |
||
768 | * '/webmozart/css/..' |
||
769 | * )); |
||
770 | * // => /webmozart |
||
771 | * ``` |
||
772 | * |
||
773 | * The root is returned if no common base path can be found: |
||
774 | * |
||
775 | * ```php |
||
776 | * $basePath = Path::getLongestCommonBasePath(array( |
||
777 | * '/webmozart/css/style.css', |
||
778 | * '/puli/css/..' |
||
779 | * )); |
||
780 | * // => / |
||
781 | * ``` |
||
782 | * |
||
783 | * If the paths are located on different Windows partitions, `null` is |
||
784 | * returned. |
||
785 | * |
||
786 | * ```php |
||
787 | * $basePath = Path::getLongestCommonBasePath(array( |
||
788 | * 'C:/webmozart/css/style.css', |
||
789 | * 'D:/webmozart/css/..' |
||
790 | * )); |
||
791 | * // => null |
||
792 | * ``` |
||
793 | * |
||
794 | * @param array $paths A list of paths. |
||
795 | * |
||
796 | * @return string|null The longest common base path in canonical form or |
||
797 | * `null` if the paths are on different Windows |
||
798 | * partitions. |
||
799 | * |
||
800 | * @since 1.0 Added method. |
||
801 | * @since 2.0 Method now fails if $paths are not strings. |
||
802 | */ |
||
803 | 81 | public static function getLongestCommonBasePath(array $paths) |
|
841 | |||
842 | /** |
||
843 | * Joins two or more path strings. |
||
844 | * |
||
845 | * The result is a canonical path. |
||
846 | * |
||
847 | * @param string[]|string $paths Path parts as parameters or array. |
||
848 | * |
||
849 | * @return string The joint path. |
||
850 | * |
||
851 | * @since 2.0 Added method. |
||
852 | */ |
||
853 | 61 | public static function join($paths) |
|
894 | |||
895 | /** |
||
896 | * Returns whether a path is a base path of another path. |
||
897 | * |
||
898 | * Dot segments ("." and "..") are removed/collapsed and all slashes turned |
||
899 | * into forward slashes. |
||
900 | * |
||
901 | * ```php |
||
902 | * Path::isBasePath('/webmozart', '/webmozart/css'); |
||
903 | * // => true |
||
904 | * |
||
905 | * Path::isBasePath('/webmozart', '/webmozart'); |
||
906 | * // => true |
||
907 | * |
||
908 | * Path::isBasePath('/webmozart', '/webmozart/..'); |
||
909 | * // => false |
||
910 | * |
||
911 | * Path::isBasePath('/webmozart', '/puli'); |
||
912 | * // => false |
||
913 | * ``` |
||
914 | * |
||
915 | * @param string $basePath The base path to test. |
||
916 | * @param string $ofPath The other path. |
||
917 | * |
||
918 | * @return bool Whether the base path is a base path of the other path. |
||
919 | * |
||
920 | * @since 1.0 Added method. |
||
921 | * @since 2.0 Method now fails if $basePath or $ofPath is not a string. |
||
922 | */ |
||
923 | 61 | public static function isBasePath($basePath, $ofPath) |
|
937 | |||
938 | /** |
||
939 | * Splits a part into its root directory and the remainder. |
||
940 | * |
||
941 | * If the path has no root directory, an empty root directory will be |
||
942 | * returned. |
||
943 | * |
||
944 | * If the root directory is a Windows style partition, the resulting root |
||
945 | * will always contain a trailing slash. |
||
946 | * |
||
947 | * list ($root, $path) = Path::split("C:/webmozart") |
||
948 | * // => array("C:/", "webmozart") |
||
949 | * |
||
950 | * list ($root, $path) = Path::split("C:") |
||
951 | * // => array("C:/", "") |
||
952 | * |
||
953 | * @param string $path The canonical path to split. |
||
954 | * |
||
955 | * @return string[] An array with the root directory and the remaining |
||
956 | * relative path. |
||
957 | */ |
||
958 | 408 | private static function split($path) |
|
992 | |||
993 | /** |
||
994 | * Converts string to lower-case (multi-byte safe if mbstring is installed). |
||
995 | * |
||
996 | * @param string $str The string |
||
997 | * |
||
998 | * @return string Lower case string |
||
999 | */ |
||
1000 | 6 | private static function toLower($str) |
|
1008 | |||
1009 | private function __construct() |
||
1012 | } |
||
1013 |