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 | 511 | 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() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the root directory of a path. |
||
| 262 | * |
||
| 263 | * The result is a canonical path. |
||
| 264 | * |
||
| 265 | * @param string $path A path string. |
||
| 266 | * |
||
| 267 | * @return string The canonical root directory. Returns an empty string if |
||
| 268 | * the given path is relative or empty. |
||
| 269 | * |
||
| 270 | * @since 1.0 Added method. |
||
| 271 | * @since 2.0 Method now fails if $path is not a string. |
||
| 272 | */ |
||
| 273 | 18 | public static function getRoot($path) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Returns the file name from a file path. |
||
| 314 | * |
||
| 315 | * @param string $path The path string. |
||
| 316 | * |
||
| 317 | * @return string The file name. |
||
| 318 | * |
||
| 319 | * @since 1.1 Added method. |
||
| 320 | * @since 2.0 Method now fails if $path is not a string. |
||
| 321 | */ |
||
| 322 | 8 | public static function getFilename($path) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns the file name without the extension from a file path. |
||
| 335 | * |
||
| 336 | * @param string $path The path string. |
||
| 337 | * @param string|null $extension If specified, only that extension is cut |
||
| 338 | * off (may contain leading dot). |
||
| 339 | * |
||
| 340 | * @return string The file name without extension. |
||
| 341 | * |
||
| 342 | * @since 1.1 Added method. |
||
| 343 | * @since 2.0 Method now fails if $path or $extension have invalid types. |
||
| 344 | */ |
||
| 345 | 20 | public static function getFilenameWithoutExtension($path, $extension = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Returns the extension from a file path. |
||
| 364 | * |
||
| 365 | * @param string $path The path string. |
||
| 366 | * @param bool $forceLowerCase Forces the extension to be lower-case |
||
| 367 | * (requires mbstring extension for correct |
||
| 368 | * multi-byte character handling in extension). |
||
| 369 | * |
||
| 370 | * @return string The extension of the file path (without leading dot). |
||
| 371 | * |
||
| 372 | * @since 1.1 Added method. |
||
| 373 | * @since 2.0 Method now fails if $path is not a string. |
||
| 374 | */ |
||
| 375 | 51 | public static function getExtension($path, $forceLowerCase = false) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns whether the path has an extension. |
||
| 394 | * |
||
| 395 | * @param string $path The path string. |
||
| 396 | * @param string|array|null $extensions If null or not provided, checks if |
||
| 397 | * an extension exists, otherwise |
||
| 398 | * checks for the specified extension |
||
| 399 | * or array of extensions (with or |
||
| 400 | * without leading dot). |
||
| 401 | * @param bool $ignoreCase Whether to ignore case-sensitivity |
||
| 402 | * (requires mbstring extension for |
||
| 403 | * correct multi-byte character |
||
| 404 | * handling in the extension). |
||
| 405 | * |
||
| 406 | * @return bool Returns `true` if the path has an (or the specified) |
||
| 407 | * extension and `false` otherwise. |
||
| 408 | * |
||
| 409 | * @since 1.1 Added method. |
||
| 410 | * @since 2.0 Method now fails if $path or $extensions have invalid types. |
||
| 411 | */ |
||
| 412 | 30 | public static function hasExtension($path, $extensions = null, $ignoreCase = false) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Changes the extension of a path string. |
||
| 443 | * |
||
| 444 | * @param string $path The path string with filename.ext to change. |
||
| 445 | * @param string $extension New extension (with or without leading dot). |
||
| 446 | * |
||
| 447 | * @return string The path string with new file extension. |
||
| 448 | * |
||
| 449 | * @since 1.1 Added method. |
||
| 450 | * @since 2.0 Method now fails if $path or $extension is not a string. |
||
| 451 | */ |
||
| 452 | 14 | public static function changeExtension($path, $extension) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Returns whether a path is absolute. |
||
| 478 | * |
||
| 479 | * @param string $path A path string. |
||
| 480 | * |
||
| 481 | * @return bool Returns true if the path is absolute, false if it is |
||
| 482 | * relative or empty. |
||
| 483 | * |
||
| 484 | * @since 1.0 Added method. |
||
| 485 | * @since 2.0 Method now fails if $path is not a string. |
||
| 486 | */ |
||
| 487 | 111 | public static function isAbsolute($path) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Returns whether a path is relative. |
||
| 523 | * |
||
| 524 | * @param string $path A path string. |
||
| 525 | * |
||
| 526 | * @return bool Returns true if the path is relative or empty, false if |
||
| 527 | * it is absolute. |
||
| 528 | * |
||
| 529 | * @since 1.0 Added method. |
||
| 530 | * @since 2.0 Method now fails if $path is not a string. |
||
| 531 | */ |
||
| 532 | 16 | public static function isRelative($path) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Turns a relative path into an absolute path. |
||
| 539 | * |
||
| 540 | * Usually, the relative path is appended to the given base path. Dot |
||
| 541 | * segments ("." and "..") are removed/collapsed and all slashes turned |
||
| 542 | * into forward slashes. |
||
| 543 | * |
||
| 544 | * ```php |
||
| 545 | * echo Path::makeAbsolute("../style.css", "/webmozart/puli/css"); |
||
| 546 | * // => /webmozart/puli/style.css |
||
| 547 | * ``` |
||
| 548 | * |
||
| 549 | * If an absolute path is passed, that path is returned unless its root |
||
| 550 | * directory is different than the one of the base path. In that case, an |
||
| 551 | * exception is thrown. |
||
| 552 | * |
||
| 553 | * ```php |
||
| 554 | * Path::makeAbsolute("/style.css", "/webmozart/puli/css"); |
||
| 555 | * // => /style.css |
||
| 556 | * |
||
| 557 | * Path::makeAbsolute("C:/style.css", "C:/webmozart/puli/css"); |
||
| 558 | * // => C:/style.css |
||
| 559 | * |
||
| 560 | * Path::makeAbsolute("C:/style.css", "/webmozart/puli/css"); |
||
| 561 | * // => C:/style.css |
||
| 562 | * ``` |
||
| 563 | * |
||
| 564 | * If the base path is not an absolute path, an exception is thrown. |
||
| 565 | * |
||
| 566 | * The result is a canonical path. |
||
| 567 | * |
||
| 568 | * @param string $path A path to make absolute. |
||
| 569 | * @param string $basePath An absolute base path. |
||
| 570 | * |
||
| 571 | * @return string An absolute path in canonical form. |
||
| 572 | * |
||
| 573 | * @throws InvalidArgumentException If the base path is not absolute. |
||
| 574 | * |
||
| 575 | * @since 1.0 Added method. |
||
| 576 | * @since 2.0 Method now fails if $path or $basePath is not a string. |
||
| 577 | * @since 2.2.2 Method does not fail anymore of $path and $basePath are |
||
| 578 | * absolute, but on different partitions. |
||
| 579 | */ |
||
| 580 | 82 | public static function makeAbsolute($path, $basePath) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Turns a path into a relative path. |
||
| 607 | * |
||
| 608 | * The relative path is created relative to the given base path: |
||
| 609 | * |
||
| 610 | * ```php |
||
| 611 | * echo Path::makeRelative("/webmozart/style.css", "/webmozart/puli"); |
||
| 612 | * // => ../style.css |
||
| 613 | * ``` |
||
| 614 | * |
||
| 615 | * If a relative path is passed and the base path is absolute, the relative |
||
| 616 | * path is returned unchanged: |
||
| 617 | * |
||
| 618 | * ```php |
||
| 619 | * Path::makeRelative("style.css", "/webmozart/puli/css"); |
||
| 620 | * // => style.css |
||
| 621 | * ``` |
||
| 622 | * |
||
| 623 | * If both paths are relative, the relative path is created with the |
||
| 624 | * assumption that both paths are relative to the same directory: |
||
| 625 | * |
||
| 626 | * ```php |
||
| 627 | * Path::makeRelative("style.css", "webmozart/puli/css"); |
||
| 628 | * // => ../../../style.css |
||
| 629 | * ``` |
||
| 630 | * |
||
| 631 | * If both paths are absolute, their root directory must be the same, |
||
| 632 | * otherwise an exception is thrown: |
||
| 633 | * |
||
| 634 | * ```php |
||
| 635 | * Path::makeRelative("C:/webmozart/style.css", "/webmozart/puli"); |
||
| 636 | * // InvalidArgumentException |
||
| 637 | * ``` |
||
| 638 | * |
||
| 639 | * If the passed path is absolute, but the base path is not, an exception |
||
| 640 | * is thrown as well: |
||
| 641 | * |
||
| 642 | * ```php |
||
| 643 | * Path::makeRelative("/webmozart/style.css", "webmozart/puli"); |
||
| 644 | * // InvalidArgumentException |
||
| 645 | * ``` |
||
| 646 | * |
||
| 647 | * If the base path is not an absolute path, an exception is thrown. |
||
| 648 | * |
||
| 649 | * The result is a canonical path. |
||
| 650 | * |
||
| 651 | * @param string $path A path to make relative. |
||
| 652 | * @param string $basePath A base path. |
||
| 653 | * |
||
| 654 | * @return string A relative path in canonical form. |
||
| 655 | * |
||
| 656 | * @throws InvalidArgumentException If the base path is not absolute or if |
||
| 657 | * the given path has a different root |
||
| 658 | * than the base path. |
||
| 659 | * |
||
| 660 | * @since 1.0 Added method. |
||
| 661 | * @since 2.0 Method now fails if $path or $basePath is not a string. |
||
| 662 | */ |
||
| 663 | 97 | public static function makeRelative($path, $basePath) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Returns whether the given path is on the local filesystem. |
||
| 738 | * |
||
| 739 | * @param string $path A path string. |
||
| 740 | * |
||
| 741 | * @return bool Returns true if the path is local, false for a URL. |
||
| 742 | * |
||
| 743 | * @since 1.0 Added method. |
||
| 744 | * @since 2.0 Method now fails if $path is not a string. |
||
| 745 | */ |
||
| 746 | 6 | public static function isLocal($path) |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Returns the longest common base path of a set of paths. |
||
| 755 | * |
||
| 756 | * Dot segments ("." and "..") are removed/collapsed and all slashes turned |
||
| 757 | * into forward slashes. |
||
| 758 | * |
||
| 759 | * ```php |
||
| 760 | * $basePath = Path::getLongestCommonBasePath(array( |
||
| 761 | * '/webmozart/css/style.css', |
||
| 762 | * '/webmozart/css/..' |
||
| 763 | * )); |
||
| 764 | * // => /webmozart |
||
| 765 | * ``` |
||
| 766 | * |
||
| 767 | * The root is returned if no common base path can be found: |
||
| 768 | * |
||
| 769 | * ```php |
||
| 770 | * $basePath = Path::getLongestCommonBasePath(array( |
||
| 771 | * '/webmozart/css/style.css', |
||
| 772 | * '/puli/css/..' |
||
| 773 | * )); |
||
| 774 | * // => / |
||
| 775 | * ``` |
||
| 776 | * |
||
| 777 | * If the paths are located on different Windows partitions, `null` is |
||
| 778 | * returned. |
||
| 779 | * |
||
| 780 | * ```php |
||
| 781 | * $basePath = Path::getLongestCommonBasePath(array( |
||
| 782 | * 'C:/webmozart/css/style.css', |
||
| 783 | * 'D:/webmozart/css/..' |
||
| 784 | * )); |
||
| 785 | * // => null |
||
| 786 | * ``` |
||
| 787 | * |
||
| 788 | * @param array $paths A list of paths. |
||
| 789 | * |
||
| 790 | * @return string|null The longest common base path in canonical form or |
||
| 791 | * `null` if the paths are on different Windows |
||
| 792 | * partitions. |
||
| 793 | * |
||
| 794 | * @since 1.0 Added method. |
||
| 795 | * @since 2.0 Method now fails if $paths are not strings. |
||
| 796 | */ |
||
| 797 | 81 | public static function getLongestCommonBasePath(array $paths) |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Joins two or more path strings. |
||
| 838 | * |
||
| 839 | * The result is a canonical path. |
||
| 840 | * |
||
| 841 | * @param string[]|string $paths Path parts as parameters or array. |
||
| 842 | * |
||
| 843 | * @return string The joint path. |
||
| 844 | * |
||
| 845 | * @since 2.0 Added method. |
||
| 846 | */ |
||
| 847 | 61 | public static function join($paths) |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Returns whether a path is a base path of another path. |
||
| 891 | * |
||
| 892 | * Dot segments ("." and "..") are removed/collapsed and all slashes turned |
||
| 893 | * into forward slashes. |
||
| 894 | * |
||
| 895 | * ```php |
||
| 896 | * Path::isBasePath('/webmozart', '/webmozart/css'); |
||
| 897 | * // => true |
||
| 898 | * |
||
| 899 | * Path::isBasePath('/webmozart', '/webmozart'); |
||
| 900 | * // => true |
||
| 901 | * |
||
| 902 | * Path::isBasePath('/webmozart', '/webmozart/..'); |
||
| 903 | * // => false |
||
| 904 | * |
||
| 905 | * Path::isBasePath('/webmozart', '/puli'); |
||
| 906 | * // => false |
||
| 907 | * ``` |
||
| 908 | * |
||
| 909 | * @param string $basePath The base path to test. |
||
| 910 | * @param string $ofPath The other path. |
||
| 911 | * |
||
| 912 | * @return bool Whether the base path is a base path of the other path. |
||
| 913 | * |
||
| 914 | * @since 1.0 Added method. |
||
| 915 | * @since 2.0 Method now fails if $basePath or $ofPath is not a string. |
||
| 916 | */ |
||
| 917 | 61 | public static function isBasePath($basePath, $ofPath) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Splits a part into its root directory and the remainder. |
||
| 934 | * |
||
| 935 | * If the path has no root directory, an empty root directory will be |
||
| 936 | * returned. |
||
| 937 | * |
||
| 938 | * If the root directory is a Windows style partition, the resulting root |
||
| 939 | * will always contain a trailing slash. |
||
| 940 | * |
||
| 941 | * list ($root, $path) = Path::split("C:/webmozart") |
||
| 942 | * // => array("C:/", "webmozart") |
||
| 943 | * |
||
| 944 | * list ($root, $path) = Path::split("C:") |
||
| 945 | * // => array("C:/", "") |
||
| 946 | * |
||
| 947 | * @param string $path The canonical path to split. |
||
| 948 | * |
||
| 949 | * @return string[] An array with the root directory and the remaining |
||
| 950 | * relative path. |
||
| 951 | */ |
||
| 952 | 408 | private static function split($path) |
|
| 986 | |||
| 987 | /** |
||
| 988 | * Converts string to lower-case (multi-byte safe if mbstring is installed). |
||
| 989 | * |
||
| 990 | * @param string $str The string |
||
| 991 | * |
||
| 992 | * @return string Lower case string |
||
| 993 | */ |
||
| 994 | 6 | private static function toLower($str) |
|
| 1002 | |||
| 1003 | private function __construct() |
||
| 1006 | } |
||
| 1007 |