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 | * @var bool |
||
| 49 | */ |
||
| 50 | private $versioning; |
||
|
|
|||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates a new repository. |
||
| 54 | * |
||
| 55 | * @param string $path The path to the JSON file. If relative, it |
||
| 56 | * must be relative to the base directory. |
||
| 57 | * @param string $baseDirectory The base directory of the store. Paths |
||
| 58 | * inside that directory are stored as relative |
||
| 59 | * paths. Paths outside that directory are |
||
| 60 | * stored as absolute paths. |
||
| 61 | * @param bool $validateJson Whether to validate the JSON file against |
||
| 62 | * the schema. Slow but spots problems. |
||
| 63 | */ |
||
| 64 | 95 | public function __construct($path, $baseDirectory, $validateJson = false) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 5 | public function getStack($path) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 77 | protected function appendToChangeStream(PuliResource $resource) |
|
| 102 | { |
||
| 103 | 77 | $path = $resource->getPath(); |
|
| 104 | 77 | $trimmedPath = rtrim($path, '/'); |
|
| 105 | |||
| 106 | // If a mapping exists for a sub-path of this resource |
||
| 107 | // (e.g. $path = /a, mapped sub-path = /a/b) |
||
| 108 | // we need to record the order, since by default sub-paths are |
||
| 109 | // preferred over super paths |
||
| 110 | |||
| 111 | 77 | $subReferences = $this->getReferencesForRegex( |
|
| 112 | 77 | $trimmedPath.'/', |
|
| 113 | 77 | '~^'.preg_quote($trimmedPath, '~').'/.+~', |
|
| 114 | // Get all mappings, don't stop on first |
||
| 115 | 77 | false, |
|
| 116 | // Don't traverse directories. We're just interested in JSON mappings. |
||
| 117 | false |
||
| 118 | 77 | ); |
|
| 119 | |||
| 120 | 77 | if (empty($subReferences)) { |
|
| 121 | 77 | return; |
|
| 122 | } |
||
| 123 | |||
| 124 | 3 | foreach ($subReferences as $currentPath => $currentReferences) { |
|
| 125 | // Copy the order of an ancestor, if defined |
||
| 126 | 3 | if (!isset($this->json['_order'][$currentPath])) { |
|
| 127 | 3 | $parentPath = $currentPath; |
|
| 128 | |||
| 129 | do { |
||
| 130 | 3 | $parentPath = Path::getDirectory($parentPath); |
|
| 131 | |||
| 132 | 3 | if (isset($this->json['_order'][$parentPath])) { |
|
| 133 | 1 | $this->json['_order'][$currentPath] = $this->json['_order'][$parentPath]; |
|
| 134 | 1 | break; |
|
| 135 | } |
||
| 136 | 3 | } while ('/' !== $parentPath); |
|
| 137 | |||
| 138 | $currentEntry = array( |
||
| 139 | 3 | 'path' => $currentPath, |
|
| 140 | 3 | 'references' => count($currentReferences), |
|
| 141 | 3 | ); |
|
| 142 | |||
| 143 | 3 | if (!isset($this->json['_order'][$currentPath])) { |
|
| 144 | // If no ancestor has a defined order, start with the $subPath |
||
| 145 | 3 | $this->json['_order'][$currentPath] = array($currentEntry); |
|
| 146 | 3 | } else { |
|
| 147 | // Else insert the $subPath before the ancestor paths |
||
| 148 | 1 | array_unshift($this->json['_order'][$currentPath], $currentEntry); |
|
| 149 | } |
||
| 150 | 3 | } |
|
| 151 | |||
| 152 | 3 | $lastEntry = reset($this->json['_order'][$currentPath]); |
|
| 153 | |||
| 154 | 3 | if ($path === $lastEntry['path']) { |
|
| 155 | // If the first entry matches the new one, add the reference |
||
| 156 | // of the current resource to the limit |
||
| 157 | 1 | ++$lastEntry['references']; |
|
| 158 | 1 | } else { |
|
| 159 | // Else add a new entry at the beginning |
||
| 160 | $newEntry = array( |
||
| 161 | 3 | 'path' => $path, |
|
| 162 | 3 | 'references' => 1, |
|
| 163 | 3 | ); |
|
| 164 | |||
| 165 | 3 | array_unshift($this->json['_order'][$currentPath], $newEntry); |
|
| 166 | } |
||
| 167 | 3 | } |
|
| 168 | 3 | } |
|
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | 77 | protected function insertReference($path, $reference) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | 7 | protected function removeReferences($glob) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | 50 | protected function getReferencesForPath($path) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | 26 | View Code Duplication | protected function getReferencesForGlob($glob, $stopOnFirst = false, $traverseDirectories = true) |
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | 82 | protected function getReferencesForRegex($staticPrefix, $regex, $stopOnFirst = false, $traverseDirectories = true, $maxDepth = 0) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | 16 | View Code Duplication | protected function getReferencesInDirectory($path, $stopOnFirst = false) |
| 297 | |||
| 298 | /** |
||
| 299 | * Flattens a two-level reference array into a one-level array. |
||
| 300 | * |
||
| 301 | * For each entry on the first level, only the first entry of the second |
||
| 302 | * level is included in the result. |
||
| 303 | * |
||
| 304 | * Each reference returned by this method can be: |
||
| 305 | * |
||
| 306 | * * `null` |
||
| 307 | * * a link starting with `@` |
||
| 308 | * * an absolute filesystem path |
||
| 309 | * |
||
| 310 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 311 | * |
||
| 312 | * @param array $references A two-level reference array as returned by |
||
| 313 | * {@link searchReferences()}. |
||
| 314 | * |
||
| 315 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 316 | * as keys. |
||
| 317 | */ |
||
| 318 | 50 | private function flatten(array $references) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Flattens a two-level reference array into a one-level array and filters |
||
| 333 | * out any references that don't match the given regular expression. |
||
| 334 | * |
||
| 335 | * This method takes a two-level reference array as returned by |
||
| 336 | * {@link searchReferences()}. The references are scanned for Puli paths |
||
| 337 | * matching the given regular expression. Those matches are returned. |
||
| 338 | * |
||
| 339 | * If a matching path refers to more than one reference, the first reference |
||
| 340 | * is returned in the resulting array. |
||
| 341 | * |
||
| 342 | * If `$listDirectories` is set to `true`, all references that contain |
||
| 343 | * directory paths are traversed recursively and scanned for more paths |
||
| 344 | * matching the regular expression. This recursive traversal can be limited |
||
| 345 | * by passing a `$maxDepth` (see {@link getPathDepth()}). |
||
| 346 | * |
||
| 347 | * Each reference returned by this method can be: |
||
| 348 | * |
||
| 349 | * * `null` |
||
| 350 | * * a link starting with `@` |
||
| 351 | * * an absolute filesystem path |
||
| 352 | * |
||
| 353 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 354 | * |
||
| 355 | * @param array $references A two-level reference array as |
||
| 356 | * returned by {@link searchReferences()}. |
||
| 357 | * @param string $regex A regular expression used to filter |
||
| 358 | * Puli paths. |
||
| 359 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 360 | * result. |
||
| 361 | * @param bool $traverseDirectories Whether to search the contents of |
||
| 362 | * directory references for more matches. |
||
| 363 | * @param int $maxDepth The maximum path depth when searching |
||
| 364 | * the contents of directory references. |
||
| 365 | * If 0, the depth is unlimited. |
||
| 366 | * |
||
| 367 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 368 | * as keys. |
||
| 369 | */ |
||
| 370 | 82 | private function flattenWithFilter(array $references, $regex, $stopOnFirst = false, $traverseDirectories = false, $maxDepth = 0) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Filters the JSON file for all references relevant to a given search path. |
||
| 441 | * |
||
| 442 | * The JSON is scanned starting with the longest mapped Puli path. |
||
| 443 | * |
||
| 444 | * If the search path is "/a/b", the result includes: |
||
| 445 | * |
||
| 446 | * * The references of the mapped path "/a/b". |
||
| 447 | * * The references of any mapped super path "/a" with the sub-path "/b" |
||
| 448 | * appended. |
||
| 449 | * |
||
| 450 | * If the argument `$includeNested` is set to `true`, the result |
||
| 451 | * additionally includes: |
||
| 452 | * |
||
| 453 | * * The references of any mapped sub path "/a/b/c". |
||
| 454 | * |
||
| 455 | * This is useful if you want to look for the children of "/a/b" or scan |
||
| 456 | * all descendants for paths matching a given pattern. |
||
| 457 | * |
||
| 458 | * The result of this method is an array with two levels: |
||
| 459 | * |
||
| 460 | * * The first level has Puli paths as keys. |
||
| 461 | * * The second level contains all references for that path, where the |
||
| 462 | * first reference has the highest, the last reference the lowest |
||
| 463 | * priority. The keys of the second level are integers. There may be |
||
| 464 | * holes between any two keys. |
||
| 465 | * |
||
| 466 | * The references of the second level contain: |
||
| 467 | * |
||
| 468 | * * `null` values for virtual resources |
||
| 469 | * * strings starting with "@" for links |
||
| 470 | * * absolute filesystem paths for filesystem resources |
||
| 471 | * |
||
| 472 | * @param string $searchPath The path to search. |
||
| 473 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 474 | * @param bool $includeNested Whether to include the references of path |
||
| 475 | * mappings for nested paths. |
||
| 476 | * |
||
| 477 | * @return array An array with two levels. |
||
| 478 | */ |
||
| 479 | 83 | private function searchReferences($searchPath, $stopOnFirst = false, $includeNested = false) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Follows any link in a list of references. |
||
| 635 | * |
||
| 636 | * This method takes all the given references, checks for links starting |
||
| 637 | * with "@" and recursively expands those links to their target references. |
||
| 638 | * The target references may be `null` or absolute filesystem paths. |
||
| 639 | * |
||
| 640 | * Null values are returned unchanged. |
||
| 641 | * |
||
| 642 | * Absolute filesystem paths are returned unchanged. |
||
| 643 | * |
||
| 644 | * @param string[]|null[] $references The references. |
||
| 645 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 646 | * result. |
||
| 647 | * |
||
| 648 | * @return string[]|null[] The references with all links replaced by their |
||
| 649 | * target references. If any link pointed to more |
||
| 650 | * than one target reference, the returned array |
||
| 651 | * is larger than the passed array (unless the |
||
| 652 | * argument `$stopOnFirst` was set to `true`). |
||
| 653 | */ |
||
| 654 | 63 | private function followLinks(array $references, $stopOnFirst = false) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Appends nested paths to references and filters out the existing ones. |
||
| 694 | * |
||
| 695 | * This method takes all the given references, appends the nested path to |
||
| 696 | * each of them and then filters out the results that actually exist on the |
||
| 697 | * filesystem. |
||
| 698 | * |
||
| 699 | * Null references are filtered out. |
||
| 700 | * |
||
| 701 | * Link references should be followed with {@link followLinks()} before |
||
| 702 | * calling this method. |
||
| 703 | * |
||
| 704 | * @param string[]|null[] $references The references. |
||
| 705 | * @param string $nestedPath The nested path to append without |
||
| 706 | * leading slash ("/"). |
||
| 707 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 708 | * result. |
||
| 709 | * |
||
| 710 | * @return string[] The references with the nested path appended. Each |
||
| 711 | * reference is guaranteed to exist on the filesystem. |
||
| 712 | */ |
||
| 713 | 61 | private function appendPathAndFilterExisting(array $references, $nestedPath, $stopOnFirst = false) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Resolves a list of references stored in the JSON. |
||
| 740 | * |
||
| 741 | * Each reference passed in can be: |
||
| 742 | * |
||
| 743 | * * `null` |
||
| 744 | * * a link starting with `@` |
||
| 745 | * * a filesystem path relative to the base directory |
||
| 746 | * * an absolute filesystem path |
||
| 747 | * |
||
| 748 | * Each reference returned by this method can be: |
||
| 749 | * |
||
| 750 | * * `null` |
||
| 751 | * * a link starting with `@` |
||
| 752 | * * an absolute filesystem path |
||
| 753 | * |
||
| 754 | * Additionally, the results are guaranteed to be an array. If the |
||
| 755 | * argument `$stopOnFirst` is set, that array has a maximum size of 1. |
||
| 756 | * |
||
| 757 | * @param mixed $references The reference(s). |
||
| 758 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 759 | * |
||
| 760 | * @return string[]|null[] The resolved references. |
||
| 761 | */ |
||
| 762 | 83 | private function resolveReferences($references, $stopOnFirst = false) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * Returns the depth of a Puli path. |
||
| 789 | * |
||
| 790 | * The depth is used in order to limit the recursion when recursively |
||
| 791 | * iterating directories. |
||
| 792 | * |
||
| 793 | * The depth starts at 0 for the root: |
||
| 794 | * |
||
| 795 | * / 0 |
||
| 796 | * /webmozart 1 |
||
| 797 | * /webmozart/puli 2 |
||
| 798 | * ... |
||
| 799 | * |
||
| 800 | * @param string $path A Puli path. |
||
| 801 | * |
||
| 802 | * @return int The depth starting with 0 for the root node. |
||
| 803 | */ |
||
| 804 | 16 | private function getPathDepth($path) |
|
| 812 | } |
||
| 813 |
This check marks private properties in classes that are never used. Those properties can be removed.