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 JsonRepository 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 JsonRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 70 | class JsonRepository extends AbstractJsonRepository implements EditableRepository |
||
| 71 | { |
||
| 72 | /** |
||
| 73 | * Flag: Don't search the contents of mapped directories for matching paths. |
||
| 74 | * |
||
| 75 | * @internal |
||
| 76 | */ |
||
| 77 | const NO_SEARCH_FILESYSTEM = 2; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Flag: Don't filter out references that don't exist on the filesystem. |
||
| 81 | * |
||
| 82 | * @internal |
||
| 83 | */ |
||
| 84 | const NO_CHECK_FILE_EXISTS = 4; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Flag: Include the references for mapped ancestor paths /a of a path /a/b. |
||
| 88 | * |
||
| 89 | * @internal |
||
| 90 | */ |
||
| 91 | const INCLUDE_ANCESTORS = 8; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Flag: Include the references for mapped nested paths /a/b of a path /a. |
||
| 95 | * |
||
| 96 | * @internal |
||
| 97 | */ |
||
| 98 | const INCLUDE_NESTED = 16; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Creates a new repository. |
||
| 102 | * |
||
| 103 | * @param string $path The path to the JSON file. If relative, it |
||
| 104 | * must be relative to the base directory. |
||
| 105 | * @param string $baseDirectory The base directory of the store. Paths |
||
| 106 | * inside that directory are stored as relative |
||
| 107 | * paths. Paths outside that directory are |
||
| 108 | * stored as absolute paths. |
||
| 109 | * @param bool $validateJson Whether to validate the JSON file against |
||
| 110 | * the schema. Slow but spots problems. |
||
| 111 | */ |
||
| 112 | 198 | public function __construct($path, $baseDirectory, $validateJson = false) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 32 | public function getVersions($path) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 170 | protected function storeVersion(PuliResource $resource) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 170 | protected function insertReference($path, $reference) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | 22 | protected function removeReferences($glob) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | 114 | protected function getReferencesForPath($path) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | 50 | View Code Duplication | protected function getReferencesForGlob($glob, $flags = 0) |
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | 68 | protected function getReferencesForRegex($staticPrefix, $regex, $flags = 0, $maxDepth = 0) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | 26 | View Code Duplication | protected function getReferencesInDirectory($path, $flags = 0) |
| 345 | |||
| 346 | /** |
||
| 347 | * Flattens a two-level reference array into a one-level array. |
||
| 348 | * |
||
| 349 | * For each entry on the first level, only the first entry of the second |
||
| 350 | * level is included in the result. |
||
| 351 | * |
||
| 352 | * Each reference returned by this method can be: |
||
| 353 | * |
||
| 354 | * * `null` |
||
| 355 | * * a link starting with `@` |
||
| 356 | * * an absolute filesystem path |
||
| 357 | * |
||
| 358 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 359 | * |
||
| 360 | * @param array $references A two-level reference array as returned by |
||
| 361 | * {@link searchReferences()}. |
||
| 362 | * |
||
| 363 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 364 | * as keys. |
||
| 365 | */ |
||
| 366 | 114 | private function flatten(array $references) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Flattens a two-level reference array into a one-level array and filters |
||
| 381 | * out any references that don't match the given regular expression. |
||
| 382 | * |
||
| 383 | * This method takes a two-level reference array as returned by |
||
| 384 | * {@link searchReferences()}. The references are scanned for Puli paths |
||
| 385 | * matching the given regular expression. Those matches are returned. |
||
| 386 | * |
||
| 387 | * If a matching path refers to more than one reference, the first reference |
||
| 388 | * is returned in the resulting array. |
||
| 389 | * |
||
| 390 | * All references that contain directory paths may be traversed recursively and |
||
| 391 | * scanned for more paths matching the regular expression. This recursive |
||
| 392 | * traversal can be limited by passing a `$maxDepth` (see {@link getPathDepth()}). |
||
| 393 | * By default, this `$maxDepth` is equal to zero (no recursive scan). |
||
| 394 | * |
||
| 395 | * The flag `STOP_ON_FIRST` may be used to stop the search at the first result. |
||
| 396 | * |
||
| 397 | * The flag `NO_SEARCH_FILESYSTEM` may be used to check for whether the found |
||
| 398 | * paths actually exist on the filesystem. |
||
| 399 | * |
||
| 400 | * Each reference returned by this method can be: |
||
| 401 | * |
||
| 402 | * * `null` |
||
| 403 | * * a link starting with `@` |
||
| 404 | * * an absolute filesystem path |
||
| 405 | * |
||
| 406 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 407 | * |
||
| 408 | * @param array $references A two-level reference array as returned by |
||
| 409 | * {@link searchReferences()}. |
||
| 410 | * @param string $regex A regular expression used to filter Puli paths. |
||
| 411 | * @param int $flags A bitwise combination of the flag constants in |
||
| 412 | * this class. |
||
| 413 | * @param int $maxDepth The maximum path depth when searching the |
||
| 414 | * contents of directory references. If 0, the |
||
| 415 | * depth is unlimited. |
||
| 416 | * |
||
| 417 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 418 | * as keys. |
||
| 419 | */ |
||
| 420 | 68 | private function flattenWithFilter(array $references, $regex, $flags = 0, $maxDepth = 0) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Filters the JSON file for all references relevant to a given search path. |
||
| 491 | * |
||
| 492 | * The JSON is scanned starting with the longest mapped Puli path. |
||
| 493 | * |
||
| 494 | * If the search path is "/a/b", the result includes: |
||
| 495 | * |
||
| 496 | * * The references of the mapped path "/a/b". |
||
| 497 | * |
||
| 498 | * If the flag `INCLUDE_ANCESTORS` is used, the result additionally |
||
| 499 | * includes: |
||
| 500 | * |
||
| 501 | * * The references of any mapped super path "/a" with the sub-path "/b" |
||
| 502 | * appended. |
||
| 503 | * |
||
| 504 | * If the flag `INCLUDE_NESTED` is used, the result additionally |
||
| 505 | * includes: |
||
| 506 | * |
||
| 507 | * * The references of any mapped sub path "/a/b/c". |
||
| 508 | * |
||
| 509 | * This is useful if you want to look for the children of "/a/b" or scan |
||
| 510 | * all descendants for paths matching a given pattern. |
||
| 511 | * |
||
| 512 | * The result of this method is an array with two levels: |
||
| 513 | * |
||
| 514 | * * The first level has Puli paths as keys. |
||
| 515 | * * The second level contains all references for that path, where the |
||
| 516 | * first reference has the highest, the last reference the lowest |
||
| 517 | * priority. The keys of the second level are integers. There may be |
||
| 518 | * holes between any two keys. |
||
| 519 | * |
||
| 520 | * The references of the second level contain: |
||
| 521 | * |
||
| 522 | * * `null` values for virtual resources |
||
| 523 | * * strings starting with "@" for links |
||
| 524 | * * absolute filesystem paths for filesystem resources |
||
| 525 | * |
||
| 526 | * The flag `STOP_ON_FIRST` may be used to stop the search at the first result. |
||
| 527 | * |
||
| 528 | * The flag `NO_SEARCH_FILESYSTEM` may be used to check for whether the found |
||
| 529 | * paths actually exist on the filesystem. |
||
| 530 | * |
||
| 531 | * @param string $searchPath The path to search. |
||
| 532 | * @param int $flags A bitwise combination of the flag constants in |
||
| 533 | * this class. |
||
| 534 | * |
||
| 535 | * @return array An array with two levels. |
||
| 536 | */ |
||
| 537 | 174 | private function searchReferences($searchPath, $flags = 0) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Follows any link in a list of references. |
||
| 729 | * |
||
| 730 | * This method takes all the given references, checks for links starting |
||
| 731 | * with "@" and recursively expands those links to their target references. |
||
| 732 | * The target references may be `null` or absolute filesystem paths. |
||
| 733 | * |
||
| 734 | * Null values are returned unchanged. |
||
| 735 | * |
||
| 736 | * Absolute filesystem paths are returned unchanged. |
||
| 737 | * |
||
| 738 | * The flag `STOP_ON_FIRST` may be used to stop the search at the first result. |
||
| 739 | * |
||
| 740 | * @param string[]|null[] $references The references. |
||
| 741 | * @param int $flags A bitwise combination of the flag |
||
| 742 | * constants in this class. |
||
| 743 | * |
||
| 744 | * @return string[]|null[] The references with all links replaced by their |
||
| 745 | * target references. If any link pointed to more |
||
| 746 | * than one target reference, the returned array |
||
| 747 | * is larger than the passed array (unless the |
||
| 748 | * argument `$stopOnFirst` was set to `true`). |
||
| 749 | */ |
||
| 750 | 122 | private function followLinks(array $references, $flags = 0) |
|
| 787 | |||
| 788 | /** |
||
| 789 | * Appends nested paths to references and filters out the existing ones. |
||
| 790 | * |
||
| 791 | * This method takes all the given references, appends the nested path to |
||
| 792 | * each of them and then filters out the results that actually exist on the |
||
| 793 | * filesystem. |
||
| 794 | * |
||
| 795 | * Null references are filtered out. |
||
| 796 | * |
||
| 797 | * Link references should be followed with {@link followLinks()} before |
||
| 798 | * calling this method. |
||
| 799 | * |
||
| 800 | * The flag `STOP_ON_FIRST` may be used to stop the search at the first result. |
||
| 801 | * |
||
| 802 | * @param string[]|null[] $references The references. |
||
| 803 | * @param string $nestedPath The nested path to append without |
||
| 804 | * leading slash ("/"). |
||
| 805 | * @param int $flags A bitwise combination of the flag |
||
| 806 | * constants in this class. |
||
| 807 | * |
||
| 808 | * @return string[] The references with the nested path appended. Each |
||
| 809 | * reference is guaranteed to exist on the filesystem. |
||
| 810 | */ |
||
| 811 | 112 | private function appendPathAndFilterExisting(array $references, $nestedPath, $flags = 0) |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Resolves a list of references stored in the JSON. |
||
| 838 | * |
||
| 839 | * Each reference passed in can be: |
||
| 840 | * |
||
| 841 | * * `null` |
||
| 842 | * * a link starting with `@` |
||
| 843 | * * a filesystem path relative to the base directory |
||
| 844 | * * an absolute filesystem path |
||
| 845 | * |
||
| 846 | * Each reference returned by this method can be: |
||
| 847 | * |
||
| 848 | * * `null` |
||
| 849 | * * a link starting with `@` |
||
| 850 | * * an absolute filesystem path |
||
| 851 | * |
||
| 852 | * Additionally, the results are guaranteed to be an array. |
||
| 853 | * |
||
| 854 | * The flag `STOP_ON_FIRST` may be used to stop the search at the first result. |
||
| 855 | * In that case, the results array has a maximum size of 1. |
||
| 856 | * |
||
| 857 | * The flag `NO_SEARCH_FILESYSTEM` may be used to check for whether the found |
||
| 858 | * paths actually exist on the filesystem. |
||
| 859 | * |
||
| 860 | * @param string $path The mapped Puli path. |
||
| 861 | * @param mixed $references The reference(s). |
||
| 862 | * @param int $flags A bitwise combination of the flag constants in |
||
| 863 | * this class. |
||
| 864 | * |
||
| 865 | * @return string[]|null[] The resolved references. |
||
| 866 | */ |
||
| 867 | 174 | private function resolveReferences($path, $references, $flags = 0) |
|
| 905 | |||
| 906 | /** |
||
| 907 | * Returns the depth of a Puli path. |
||
| 908 | * |
||
| 909 | * The depth is used in order to limit the recursion when recursively |
||
| 910 | * iterating directories. |
||
| 911 | * |
||
| 912 | * The depth starts at 0 for the root: |
||
| 913 | * |
||
| 914 | * / 0 |
||
| 915 | * /webmozart 1 |
||
| 916 | * /webmozart/puli 2 |
||
| 917 | * ... |
||
| 918 | * |
||
| 919 | * @param string $path A Puli path. |
||
| 920 | * |
||
| 921 | * @return int The depth starting with 0 for the root node. |
||
| 922 | */ |
||
| 923 | 26 | private function getPathDepth($path) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Inserts a path at the beginning of the order list of a mapped path. |
||
| 934 | * |
||
| 935 | * @param string $path The path of the mapping where to prepend. |
||
| 936 | * @param string $prependedPath The path of the mapping to prepend. |
||
| 937 | */ |
||
| 938 | 10 | private function prependOrderEntry($path, $prependedPath) |
|
| 953 | |||
| 954 | /** |
||
| 955 | * Initializes a path with the order of the closest parent path. |
||
| 956 | * |
||
| 957 | * @param string $path The path to initialize. |
||
| 958 | * @param array $parentReferences The defined references for parent paths, |
||
| 959 | * with long paths /a/b sorted before short |
||
| 960 | * paths /a. |
||
| 961 | */ |
||
| 962 | 14 | private function initWithParentOrder($path, array $parentReferences) |
|
| 974 | |||
| 975 | /** |
||
| 976 | * Initializes the order of a path with the default order. |
||
| 977 | * |
||
| 978 | * This is necessary if we want to insert a non-default order entry for |
||
| 979 | * the first time. |
||
| 980 | * |
||
| 981 | * @param string $path The path to initialize. |
||
| 982 | * @param string $insertedPath The path that is being inserted. |
||
| 983 | * @param array $references The references for each defined path mapping |
||
| 984 | * in the path chain. |
||
| 985 | */ |
||
| 986 | 10 | private function initWithDefaultOrder($path, $insertedPath, $references) |
|
| 1019 | } |
||
| 1020 |
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.