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 | public function __construct($path, $baseDirectory, $validateJson = false) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function getStack($path) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | protected function appendToChangeStream(PuliResource $resource) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | protected function insertReference($path, $reference) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | protected function removeReferences($glob) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | protected function getReferencesForPath($path) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | View Code Duplication | protected function getReferencesForGlob($glob, $stopOnFirst = false, $traverseDirectories = true) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | protected function getReferencesForRegex($staticPrefix, $regex, $stopOnFirst = false, $traverseDirectories = true, $maxDepth = 0) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | View Code Duplication | protected function getReferencesInDirectory($path, $stopOnFirst = false) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Flattens a two-level reference array into a one-level array. |
||
| 332 | * |
||
| 333 | * For each entry on the first level, only the first entry of the second |
||
| 334 | * level is included in the result. |
||
| 335 | * |
||
| 336 | * Each reference returned by this method can be: |
||
| 337 | * |
||
| 338 | * * `null` |
||
| 339 | * * a link starting with `@` |
||
| 340 | * * an absolute filesystem path |
||
| 341 | * |
||
| 342 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 343 | * |
||
| 344 | * @param array $references A two-level reference array as returned by |
||
| 345 | * {@link searchReferences()}. |
||
| 346 | * |
||
| 347 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 348 | * as keys. |
||
| 349 | */ |
||
| 350 | private function flatten(array $references) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Flattens a two-level reference array into a one-level array and filters |
||
| 365 | * out any references that don't match the given regular expression. |
||
| 366 | * |
||
| 367 | * This method takes a two-level reference array as returned by |
||
| 368 | * {@link searchReferences()}. The references are scanned for Puli paths |
||
| 369 | * matching the given regular expression. Those matches are returned. |
||
| 370 | * |
||
| 371 | * If a matching path refers to more than one reference, the first reference |
||
| 372 | * is returned in the resulting array. |
||
| 373 | * |
||
| 374 | * If `$listDirectories` is set to `true`, all references that contain |
||
| 375 | * directory paths are traversed recursively and scanned for more paths |
||
| 376 | * matching the regular expression. This recursive traversal can be limited |
||
| 377 | * by passing a `$maxDepth` (see {@link getPathDepth()}). |
||
| 378 | * |
||
| 379 | * Each reference returned by this method can be: |
||
| 380 | * |
||
| 381 | * * `null` |
||
| 382 | * * a link starting with `@` |
||
| 383 | * * an absolute filesystem path |
||
| 384 | * |
||
| 385 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 386 | * |
||
| 387 | * @param array $references A two-level reference array as |
||
| 388 | * returned by {@link searchReferences()}. |
||
| 389 | * @param string $regex A regular expression used to filter |
||
| 390 | * Puli paths. |
||
| 391 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 392 | * result. |
||
| 393 | * @param bool $traverseDirectories Whether to search the contents of |
||
| 394 | * directory references for more matches. |
||
| 395 | * @param int $maxDepth The maximum path depth when searching |
||
| 396 | * the contents of directory references. |
||
| 397 | * If 0, the depth is unlimited. |
||
| 398 | * |
||
| 399 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 400 | * as keys. |
||
| 401 | */ |
||
| 402 | private function flattenWithFilter(array $references, $regex, $stopOnFirst = false, $traverseDirectories = false, $maxDepth = 0) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Filters the JSON file for all references relevant to a given search path. |
||
| 473 | * |
||
| 474 | * The JSON is scanned starting with the longest mapped Puli path. |
||
| 475 | * |
||
| 476 | * If the search path is "/a/b", the result includes: |
||
| 477 | * |
||
| 478 | * * The references of the mapped path "/a/b". |
||
| 479 | * * The references of any mapped super path "/a" with the sub-path "/b" |
||
| 480 | * appended. |
||
| 481 | * |
||
| 482 | * If the argument `$includeNested` is set to `true`, the result |
||
| 483 | * additionally includes: |
||
| 484 | * |
||
| 485 | * * The references of any mapped sub path "/a/b/c". |
||
| 486 | * |
||
| 487 | * This is useful if you want to look for the children of "/a/b" or scan |
||
| 488 | * all descendants for paths matching a given pattern. |
||
| 489 | * |
||
| 490 | * The result of this method is an array with two levels: |
||
| 491 | * |
||
| 492 | * * The first level has Puli paths as keys. |
||
| 493 | * * The second level contains all references for that path, where the |
||
| 494 | * first reference has the highest, the last reference the lowest |
||
| 495 | * priority. The keys of the second level are integers. There may be |
||
| 496 | * holes between any two keys. |
||
| 497 | * |
||
| 498 | * The references of the second level contain: |
||
| 499 | * |
||
| 500 | * * `null` values for virtual resources |
||
| 501 | * * strings starting with "@" for links |
||
| 502 | * * absolute filesystem paths for filesystem resources |
||
| 503 | * |
||
| 504 | * @param string $searchPath The path to search. |
||
| 505 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 506 | * @param bool $checkFilesystem Whether to check directories of ancestor |
||
| 507 | * references for the searched path. |
||
| 508 | * @param bool $includeNested Whether to include the references of path |
||
| 509 | * mappings for nested paths. |
||
| 510 | * @param bool $includeAncestors Whether to include the references of path |
||
| 511 | * mappings for ancestor paths. |
||
| 512 | * |
||
| 513 | * @return array An array with two levels. |
||
| 514 | */ |
||
| 515 | private function searchReferences($searchPath, $stopOnFirst = false, $checkFilesystem = true, $includeNested = false, $includeAncestors = false) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Follows any link in a list of references. |
||
| 691 | * |
||
| 692 | * This method takes all the given references, checks for links starting |
||
| 693 | * with "@" and recursively expands those links to their target references. |
||
| 694 | * The target references may be `null` or absolute filesystem paths. |
||
| 695 | * |
||
| 696 | * Null values are returned unchanged. |
||
| 697 | * |
||
| 698 | * Absolute filesystem paths are returned unchanged. |
||
| 699 | * |
||
| 700 | * @param string[]|null[] $references The references. |
||
| 701 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 702 | * result. |
||
| 703 | * |
||
| 704 | * @return string[]|null[] The references with all links replaced by their |
||
| 705 | * target references. If any link pointed to more |
||
| 706 | * than one target reference, the returned array |
||
| 707 | * is larger than the passed array (unless the |
||
| 708 | * argument `$stopOnFirst` was set to `true`). |
||
| 709 | */ |
||
| 710 | private function followLinks(array $references, $stopOnFirst = false) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Appends nested paths to references and filters out the existing ones. |
||
| 750 | * |
||
| 751 | * This method takes all the given references, appends the nested path to |
||
| 752 | * each of them and then filters out the results that actually exist on the |
||
| 753 | * filesystem. |
||
| 754 | * |
||
| 755 | * Null references are filtered out. |
||
| 756 | * |
||
| 757 | * Link references should be followed with {@link followLinks()} before |
||
| 758 | * calling this method. |
||
| 759 | * |
||
| 760 | * @param string[]|null[] $references The references. |
||
| 761 | * @param string $nestedPath The nested path to append without |
||
| 762 | * leading slash ("/"). |
||
| 763 | * @param bool $stopOnFirst Whether to stop after finding a first |
||
| 764 | * result. |
||
| 765 | * |
||
| 766 | * @return string[] The references with the nested path appended. Each |
||
| 767 | * reference is guaranteed to exist on the filesystem. |
||
| 768 | */ |
||
| 769 | private function appendPathAndFilterExisting(array $references, $nestedPath, $stopOnFirst = false) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Resolves a list of references stored in the JSON. |
||
| 796 | * |
||
| 797 | * Each reference passed in can be: |
||
| 798 | * |
||
| 799 | * * `null` |
||
| 800 | * * a link starting with `@` |
||
| 801 | * * a filesystem path relative to the base directory |
||
| 802 | * * an absolute filesystem path |
||
| 803 | * |
||
| 804 | * Each reference returned by this method can be: |
||
| 805 | * |
||
| 806 | * * `null` |
||
| 807 | * * a link starting with `@` |
||
| 808 | * * an absolute filesystem path |
||
| 809 | * |
||
| 810 | * Additionally, the results are guaranteed to be an array. If the |
||
| 811 | * argument `$stopOnFirst` is set, that array has a maximum size of 1. |
||
| 812 | * |
||
| 813 | * @param mixed $references The reference(s). |
||
| 814 | * @param bool $stopOnFirst Whether to stop after finding a first result. |
||
| 815 | * |
||
| 816 | * @return string[]|null[] The resolved references. |
||
| 817 | */ |
||
| 818 | private function resolveReferences($references, $stopOnFirst = false, $checkFilesystem = true) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Returns the depth of a Puli path. |
||
| 845 | * |
||
| 846 | * The depth is used in order to limit the recursion when recursively |
||
| 847 | * iterating directories. |
||
| 848 | * |
||
| 849 | * The depth starts at 0 for the root: |
||
| 850 | * |
||
| 851 | * / 0 |
||
| 852 | * /webmozart 1 |
||
| 853 | * /webmozart/puli 2 |
||
| 854 | * ... |
||
| 855 | * |
||
| 856 | * @param string $path A Puli path. |
||
| 857 | * |
||
| 858 | * @return int The depth starting with 0 for the root node. |
||
| 859 | */ |
||
| 860 | private function getPathDepth($path) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Inserts a path at the beginning of the order list of a mapped path. |
||
| 871 | * |
||
| 872 | * @param string $insertedPath The path of the mapping to insert. |
||
| 873 | * @param string $mappedPath The path of the mapping where to insert. |
||
| 874 | */ |
||
| 875 | private function insertOrderEntry($insertedPath, $mappedPath) |
||
| 890 | } |
||
| 891 |
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.