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 |
||
| 45 | class JsonRepository extends AbstractJsonRepository implements EditableRepository |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Creates a new repository. |
||
| 49 | * |
||
| 50 | * @param string $path The path to the JSON file. If relative, it |
||
| 51 | * must be relative to the base directory. |
||
| 52 | * @param string $baseDirectory The base directory of the store. Paths |
||
| 53 | * inside that directory are stored as relative |
||
| 54 | * paths. Paths outside that directory are |
||
| 55 | * stored as absolute paths. |
||
| 56 | * @param bool $validateJson Whether to validate the JSON file against |
||
| 57 | * the schema. Slow but spots problems. |
||
| 58 | */ |
||
| 59 | 98 | public function __construct($path, $baseDirectory, $validateJson = false) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 9 | public function getStack($path) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | 80 | protected function appendToChangeStream(PuliResource $resource) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 80 | protected function insertReference($path, $reference) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 7 | protected function removeReferences($glob) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | 55 | protected function getReferencesForPath($path) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | 26 | View Code Duplication | protected function getReferencesForGlob($glob, $stopOnFirst = false, $traverseDirectories = true) |
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | 32 | protected function getReferencesForRegex($staticPrefix, $regex, $stopOnFirst = false, $traverseDirectories = true, $maxDepth = 0) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | 16 | View Code Duplication | protected function getReferencesInDirectory($path, $stopOnFirst = false) |
| 303 | |||
| 304 | /** |
||
| 305 | * Flattens a two-level reference array into a one-level array. |
||
| 306 | * |
||
| 307 | * For each entry on the first level, only the first entry of the second |
||
| 308 | * level is included in the result. |
||
| 309 | * |
||
| 310 | * Each reference returned by this method can be: |
||
| 311 | * |
||
| 312 | * * `null` |
||
| 313 | * * a link starting with `@` |
||
| 314 | * * an absolute filesystem path |
||
| 315 | * |
||
| 316 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 317 | * |
||
| 318 | * @param array $references A two-level reference array as returned by |
||
| 319 | * {@link searchReferences()}. |
||
| 320 | * |
||
| 321 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 322 | * as keys. |
||
| 323 | */ |
||
| 324 | 55 | private function flatten(array $references) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Flattens a two-level reference array into a one-level array and filters |
||
| 339 | * out any references that don't match the given regular expression. |
||
| 340 | * |
||
| 341 | * This method takes a two-level reference array as returned by |
||
| 342 | * {@link searchReferences()}. The references are scanned for Puli paths |
||
| 343 | * matching the given regular expression. Those matches are returned. |
||
| 344 | * |
||
| 345 | * If a matching path refers to more than one reference, the first reference |
||
| 346 | * is returned in the resulting array. |
||
| 347 | * |
||
| 348 | * If `$listDirectories` is set to `true`, all references that contain |
||
| 349 | * directory paths are traversed recursively and scanned for more paths |
||
| 350 | * matching the regular expression. This recursive traversal can be limited |
||
| 351 | * by passing a `$maxDepth` (see {@link getPathDepth()}). |
||
| 352 | * |
||
| 353 | * Each reference returned by this method can be: |
||
| 354 | * |
||
| 355 | * * `null` |
||
| 356 | * * a link starting with `@` |
||
| 357 | * * an absolute filesystem path |
||
| 358 | * |
||
| 359 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 360 | * |
||
| 361 | * @param array $references A two-level reference array as |
||
| 362 | * returned by {@link searchReferences()}. |
||
| 363 | * @param string $regex A regular expression used to filter |
||
| 364 | * Puli paths. |
||
| 365 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 366 | * result. |
||
| 367 | * @param bool $traverseDirectories Whether to search the contents of |
||
| 368 | * directory references for more matches. |
||
| 369 | * @param int $maxDepth The maximum path depth when searching |
||
| 370 | * the contents of directory references. |
||
| 371 | * If 0, the depth is unlimited. |
||
| 372 | * |
||
| 373 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 374 | * as keys. |
||
| 375 | */ |
||
| 376 | 32 | private function flattenWithFilter(array $references, $regex, $stopOnFirst = false, $traverseDirectories = false, $maxDepth = 0) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Filters the JSON file for all references relevant to a given search path. |
||
| 447 | * |
||
| 448 | * The JSON is scanned starting with the longest mapped Puli path. |
||
| 449 | * |
||
| 450 | * If the search path is "/a/b", the result includes: |
||
| 451 | * |
||
| 452 | * * The references of the mapped path "/a/b". |
||
| 453 | * * The references of any mapped super path "/a" with the sub-path "/b" |
||
| 454 | * appended. |
||
| 455 | * |
||
| 456 | * If the argument `$includeNested` is set to `true`, the result |
||
| 457 | * additionally includes: |
||
| 458 | * |
||
| 459 | * * The references of any mapped sub path "/a/b/c". |
||
| 460 | * |
||
| 461 | * This is useful if you want to look for the children of "/a/b" or scan |
||
| 462 | * all descendants for paths matching a given pattern. |
||
| 463 | * |
||
| 464 | * The result of this method is an array with two levels: |
||
| 465 | * |
||
| 466 | * * The first level has Puli paths as keys. |
||
| 467 | * * The second level contains all references for that path, where the |
||
| 468 | * first reference has the highest, the last reference the lowest |
||
| 469 | * priority. The keys of the second level are integers. There may be |
||
| 470 | * holes between any two keys. |
||
| 471 | * |
||
| 472 | * The references of the second level contain: |
||
| 473 | * |
||
| 474 | * * `null` values for virtual resources |
||
| 475 | * * strings starting with "@" for links |
||
| 476 | * * absolute filesystem paths for filesystem resources |
||
| 477 | * |
||
| 478 | * @param string $searchPath The path to search. |
||
| 479 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 480 | * @param bool $checkFilesystem Whether to check directories of ancestor |
||
| 481 | * references for the searched path. |
||
| 482 | * @param bool $includeNested Whether to include the references of path |
||
| 483 | * mappings for nested paths. |
||
| 484 | * @param bool $includeAncestors Whether to include the references of path |
||
| 485 | * mappings for ancestor paths. |
||
| 486 | * |
||
| 487 | * @return array An array with two levels. |
||
| 488 | */ |
||
| 489 | 86 | private function searchReferences($searchPath, $stopOnFirst = false, $checkFilesystem = true, $includeNested = false, $includeAncestors = false) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Follows any link in a list of references. |
||
| 665 | * |
||
| 666 | * This method takes all the given references, checks for links starting |
||
| 667 | * with "@" and recursively expands those links to their target references. |
||
| 668 | * The target references may be `null` or absolute filesystem paths. |
||
| 669 | * |
||
| 670 | * Null values are returned unchanged. |
||
| 671 | * |
||
| 672 | * Absolute filesystem paths are returned unchanged. |
||
| 673 | * |
||
| 674 | * @param string[]|null[] $references The references. |
||
| 675 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 676 | * result. |
||
| 677 | * |
||
| 678 | * @return string[]|null[] The references with all links replaced by their |
||
| 679 | * target references. If any link pointed to more |
||
| 680 | * than one target reference, the returned array |
||
| 681 | * is larger than the passed array (unless the |
||
| 682 | * argument `$stopOnFirst` was set to `true`). |
||
| 683 | */ |
||
| 684 | 56 | private function followLinks(array $references, $stopOnFirst = false) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Appends nested paths to references and filters out the existing ones. |
||
| 724 | * |
||
| 725 | * This method takes all the given references, appends the nested path to |
||
| 726 | * each of them and then filters out the results that actually exist on the |
||
| 727 | * filesystem. |
||
| 728 | * |
||
| 729 | * Null references are filtered out. |
||
| 730 | * |
||
| 731 | * Link references should be followed with {@link followLinks()} before |
||
| 732 | * calling this method. |
||
| 733 | * |
||
| 734 | * @param string[]|null[] $references The references. |
||
| 735 | * @param string $nestedPath The nested path to append without |
||
| 736 | * leading slash ("/"). |
||
| 737 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 738 | * result. |
||
| 739 | * |
||
| 740 | * @return string[] The references with the nested path appended. Each |
||
| 741 | * reference is guaranteed to exist on the filesystem. |
||
| 742 | */ |
||
| 743 | 53 | private function appendPathAndFilterExisting(array $references, $nestedPath, $stopOnFirst = false) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Resolves a list of references stored in the JSON. |
||
| 770 | * |
||
| 771 | * Each reference passed in can be: |
||
| 772 | * |
||
| 773 | * * `null` |
||
| 774 | * * a link starting with `@` |
||
| 775 | * * a filesystem path relative to the base directory |
||
| 776 | * * an absolute filesystem path |
||
| 777 | * |
||
| 778 | * Each reference returned by this method can be: |
||
| 779 | * |
||
| 780 | * * `null` |
||
| 781 | * * a link starting with `@` |
||
| 782 | * * an absolute filesystem path |
||
| 783 | * |
||
| 784 | * Additionally, the results are guaranteed to be an array. If the |
||
| 785 | * argument `$stopOnFirst` is set, that array has a maximum size of 1. |
||
| 786 | * |
||
| 787 | * @param mixed $references The reference(s). |
||
| 788 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 789 | * @param bool $checkFilesystem whether to filter out references that don't |
||
| 790 | * exist on the filesystem. |
||
| 791 | * |
||
| 792 | * @return string[]|null[] The resolved references. |
||
| 793 | */ |
||
| 794 | 86 | private function resolveReferences($references, $stopOnFirst = false, $checkFilesystem = true) |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Returns the depth of a Puli path. |
||
| 821 | * |
||
| 822 | * The depth is used in order to limit the recursion when recursively |
||
| 823 | * iterating directories. |
||
| 824 | * |
||
| 825 | * The depth starts at 0 for the root: |
||
| 826 | * |
||
| 827 | * / 0 |
||
| 828 | * /webmozart 1 |
||
| 829 | * /webmozart/puli 2 |
||
| 830 | * ... |
||
| 831 | * |
||
| 832 | * @param string $path A Puli path. |
||
| 833 | * |
||
| 834 | * @return int The depth starting with 0 for the root node. |
||
| 835 | */ |
||
| 836 | 16 | private function getPathDepth($path) |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Inserts a path at the beginning of the order list of a mapped path. |
||
| 847 | * |
||
| 848 | * @param string $path The path of the mapping where to prepend. |
||
| 849 | * @param string $prependedPath The path of the mapping to prepend. |
||
| 850 | */ |
||
| 851 | 6 | private function prependOrderEntry($path, $prependedPath) |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Initializes a path with the order of the closest parent path. |
||
| 869 | * |
||
| 870 | * @param string $path The path to initialize. |
||
| 871 | * @param array $parentReferences The defined references for parent paths, |
||
| 872 | * with long paths /a/b sorted before short |
||
| 873 | * paths /a. |
||
| 874 | */ |
||
| 875 | 8 | private function initWithParentOrder($path, array $parentReferences) |
|
| 887 | |||
| 888 | /** |
||
| 889 | * Initializes the order of a path with the default order. |
||
| 890 | * |
||
| 891 | * This is necessary if we want to insert a non-default order entry for |
||
| 892 | * the first time. |
||
| 893 | * |
||
| 894 | * @param string $path The path to initialize. |
||
| 895 | * @param string $insertedPath The path that is being inserted. |
||
| 896 | * @param array $references The references for each defined path mapping |
||
| 897 | * in the path chain. |
||
| 898 | */ |
||
| 899 | 6 | private function initWithDefaultOrder($path, $insertedPath, $references) |
|
| 932 | } |
||
| 933 |
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.