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 | * Flag: Don't search the contents of mapped directories for matching paths. |
||
| 49 | * |
||
| 50 | * @internal |
||
| 51 | */ |
||
| 52 | const NO_SEARCH_FILESYSTEM = 2; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Flag: Don't filter out references that don't exist on the filesystem. |
||
| 56 | * |
||
| 57 | * @internal |
||
| 58 | */ |
||
| 59 | const NO_CHECK_FILE_EXISTS = 4; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Flag: Include the references for mapped ancestor paths /a of a path /a/b |
||
| 63 | * |
||
| 64 | * @internal |
||
| 65 | */ |
||
| 66 | const INCLUDE_ANCESTORS = 8; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Flag: Include the references for mapped nested paths /a/b of a path /a |
||
| 70 | * |
||
| 71 | * @internal |
||
| 72 | */ |
||
| 73 | const INCLUDE_NESTED = 16; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new repository. |
||
| 77 | * |
||
| 78 | * @param string $path The path to the JSON file. If relative, it |
||
| 79 | * must be relative to the base directory. |
||
| 80 | * @param string $baseDirectory The base directory of the store. Paths |
||
| 81 | * inside that directory are stored as relative |
||
| 82 | * paths. Paths outside that directory are |
||
| 83 | * stored as absolute paths. |
||
| 84 | * @param bool $validateJson Whether to validate the JSON file against |
||
| 85 | * the schema. Slow but spots problems. |
||
| 86 | */ |
||
| 87 | 98 | public function __construct($path, $baseDirectory, $validateJson = false) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | 8 | public function getStack($path) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 80 | protected function appendToChangeStream(PuliResource $resource) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | 80 | protected function insertReference($path, $reference) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | 7 | protected function removeReferences($glob) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | 55 | protected function getReferencesForPath($path) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | 26 | View Code Duplication | protected function getReferencesForGlob($glob, $flags = 0) |
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | 32 | protected function getReferencesForRegex($staticPrefix, $regex, $flags = 0, $maxDepth = 0) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | 16 | View Code Duplication | protected function getReferencesInDirectory($path, $flags = 0) |
| 319 | |||
| 320 | /** |
||
| 321 | * Flattens a two-level reference array into a one-level array. |
||
| 322 | * |
||
| 323 | * For each entry on the first level, only the first entry of the second |
||
| 324 | * level is included in the result. |
||
| 325 | * |
||
| 326 | * Each reference returned by this method can be: |
||
| 327 | * |
||
| 328 | * * `null` |
||
| 329 | * * a link starting with `@` |
||
| 330 | * * an absolute filesystem path |
||
| 331 | * |
||
| 332 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 333 | * |
||
| 334 | * @param array $references A two-level reference array as returned by |
||
| 335 | * {@link searchReferences()}. |
||
| 336 | * |
||
| 337 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 338 | * as keys. |
||
| 339 | */ |
||
| 340 | 55 | private function flatten(array $references) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Flattens a two-level reference array into a one-level array and filters |
||
| 355 | * out any references that don't match the given regular expression. |
||
| 356 | * |
||
| 357 | * This method takes a two-level reference array as returned by |
||
| 358 | * {@link searchReferences()}. The references are scanned for Puli paths |
||
| 359 | * matching the given regular expression. Those matches are returned. |
||
| 360 | * |
||
| 361 | * If a matching path refers to more than one reference, the first reference |
||
| 362 | * is returned in the resulting array. |
||
| 363 | * |
||
| 364 | * If `$listDirectories` is set to `true`, all references that contain |
||
| 365 | * directory paths are traversed recursively and scanned for more paths |
||
| 366 | * matching the regular expression. This recursive traversal can be limited |
||
| 367 | * by passing a `$maxDepth` (see {@link getPathDepth()}). |
||
| 368 | * |
||
| 369 | * Each reference returned by this method can be: |
||
| 370 | * |
||
| 371 | * * `null` |
||
| 372 | * * a link starting with `@` |
||
| 373 | * * an absolute filesystem path |
||
| 374 | * |
||
| 375 | * The keys of the returned array are Puli paths. Their order is undefined. |
||
| 376 | * |
||
| 377 | * @param array $references A two-level reference array as returned by |
||
| 378 | * {@link searchReferences()}. |
||
| 379 | * @param string $regex A regular expression used to filter Puli paths. |
||
| 380 | * @param int $flags A bitwise combination of the flag constants in |
||
| 381 | * this class. |
||
| 382 | * @param int $maxDepth The maximum path depth when searching the |
||
| 383 | * contents of directory references. If 0, the |
||
| 384 | * depth is unlimited. |
||
| 385 | * |
||
| 386 | * @return string[]|null[] A one-level array of references with Puli paths |
||
| 387 | * as keys. |
||
| 388 | */ |
||
| 389 | 32 | private function flattenWithFilter(array $references, $regex, $flags = 0, $maxDepth = 0) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Filters the JSON file for all references relevant to a given search path. |
||
| 460 | * |
||
| 461 | * The JSON is scanned starting with the longest mapped Puli path. |
||
| 462 | * |
||
| 463 | * If the search path is "/a/b", the result includes: |
||
| 464 | * |
||
| 465 | * * The references of the mapped path "/a/b". |
||
| 466 | * * The references of any mapped super path "/a" with the sub-path "/b" |
||
| 467 | * appended. |
||
| 468 | * |
||
| 469 | * If the argument `$includeNested` is set to `true`, the result |
||
| 470 | * additionally includes: |
||
| 471 | * |
||
| 472 | * * The references of any mapped sub path "/a/b/c". |
||
| 473 | * |
||
| 474 | * This is useful if you want to look for the children of "/a/b" or scan |
||
| 475 | * all descendants for paths matching a given pattern. |
||
| 476 | * |
||
| 477 | * The result of this method is an array with two levels: |
||
| 478 | * |
||
| 479 | * * The first level has Puli paths as keys. |
||
| 480 | * * The second level contains all references for that path, where the |
||
| 481 | * first reference has the highest, the last reference the lowest |
||
| 482 | * priority. The keys of the second level are integers. There may be |
||
| 483 | * holes between any two keys. |
||
| 484 | * |
||
| 485 | * The references of the second level contain: |
||
| 486 | * |
||
| 487 | * * `null` values for virtual resources |
||
| 488 | * * strings starting with "@" for links |
||
| 489 | * * absolute filesystem paths for filesystem resources |
||
| 490 | * |
||
| 491 | * @param string $searchPath The path to search. |
||
| 492 | * @param int $flags A bitwise combination of the flag constants in |
||
| 493 | * this class. |
||
| 494 | * |
||
| 495 | * @return array An array with two levels. |
||
| 496 | */ |
||
| 497 | 86 | private function searchReferences($searchPath, $flags = 0) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Follows any link in a list of references. |
||
| 673 | * |
||
| 674 | * This method takes all the given references, checks for links starting |
||
| 675 | * with "@" and recursively expands those links to their target references. |
||
| 676 | * The target references may be `null` or absolute filesystem paths. |
||
| 677 | * |
||
| 678 | * Null values are returned unchanged. |
||
| 679 | * |
||
| 680 | * Absolute filesystem paths are returned unchanged. |
||
| 681 | * |
||
| 682 | * @param string[]|null[] $references The references. |
||
| 683 | * @param int $flags A bitwise combination of the flag |
||
| 684 | * constants in this class. |
||
| 685 | * |
||
| 686 | * @return string[]|null[] The references with all links replaced by their |
||
| 687 | * target references. If any link pointed to more |
||
| 688 | * than one target reference, the returned array |
||
| 689 | * is larger than the passed array (unless the |
||
| 690 | * argument `$stopOnFirst` was set to `true`). |
||
| 691 | */ |
||
| 692 | 56 | private function followLinks(array $references, $flags = 0) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Appends nested paths to references and filters out the existing ones. |
||
| 732 | * |
||
| 733 | * This method takes all the given references, appends the nested path to |
||
| 734 | * each of them and then filters out the results that actually exist on the |
||
| 735 | * filesystem. |
||
| 736 | * |
||
| 737 | * Null references are filtered out. |
||
| 738 | * |
||
| 739 | * Link references should be followed with {@link followLinks()} before |
||
| 740 | * calling this method. |
||
| 741 | * |
||
| 742 | * @param string[]|null[] $references The references. |
||
| 743 | * @param string $nestedPath The nested path to append without |
||
| 744 | * leading slash ("/"). |
||
| 745 | * @param int $flags A bitwise combination of the flag |
||
| 746 | * constants in this class. |
||
| 747 | * |
||
| 748 | * @return string[] The references with the nested path appended. Each |
||
| 749 | * reference is guaranteed to exist on the filesystem. |
||
| 750 | */ |
||
| 751 | 53 | private function appendPathAndFilterExisting(array $references, $nestedPath, $flags = 0) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Resolves a list of references stored in the JSON. |
||
| 778 | * |
||
| 779 | * Each reference passed in can be: |
||
| 780 | * |
||
| 781 | * * `null` |
||
| 782 | * * a link starting with `@` |
||
| 783 | * * a filesystem path relative to the base directory |
||
| 784 | * * an absolute filesystem path |
||
| 785 | * |
||
| 786 | * Each reference returned by this method can be: |
||
| 787 | * |
||
| 788 | * * `null` |
||
| 789 | * * a link starting with `@` |
||
| 790 | * * an absolute filesystem path |
||
| 791 | * |
||
| 792 | * Additionally, the results are guaranteed to be an array. If the |
||
| 793 | * argument `$stopOnFirst` is set, that array has a maximum size of 1. |
||
| 794 | * |
||
| 795 | * @param mixed $references The reference(s). |
||
| 796 | * @param int $flags A bitwise combination of the flag constants in |
||
| 797 | * this class. |
||
| 798 | * |
||
| 799 | * @return string[]|null[] The resolved references. |
||
| 800 | */ |
||
| 801 | 86 | private function resolveReferences($references, $flags = 0) |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Returns the depth of a Puli path. |
||
| 828 | * |
||
| 829 | * The depth is used in order to limit the recursion when recursively |
||
| 830 | * iterating directories. |
||
| 831 | * |
||
| 832 | * The depth starts at 0 for the root: |
||
| 833 | * |
||
| 834 | * / 0 |
||
| 835 | * /webmozart 1 |
||
| 836 | * /webmozart/puli 2 |
||
| 837 | * ... |
||
| 838 | * |
||
| 839 | * @param string $path A Puli path. |
||
| 840 | * |
||
| 841 | * @return int The depth starting with 0 for the root node. |
||
| 842 | */ |
||
| 843 | 16 | private function getPathDepth($path) |
|
| 851 | |||
| 852 | /** |
||
| 853 | * Inserts a path at the beginning of the order list of a mapped path. |
||
| 854 | * |
||
| 855 | * @param string $path The path of the mapping where to prepend. |
||
| 856 | * @param string $prependedPath The path of the mapping to prepend. |
||
| 857 | */ |
||
| 858 | 6 | private function prependOrderEntry($path, $prependedPath) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Initializes a path with the order of the closest parent path. |
||
| 876 | * |
||
| 877 | * @param string $path The path to initialize. |
||
| 878 | * @param array $parentReferences The defined references for parent paths, |
||
| 879 | * with long paths /a/b sorted before short |
||
| 880 | * paths /a. |
||
| 881 | */ |
||
| 882 | 8 | private function initWithParentOrder($path, array $parentReferences) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * Initializes the order of a path with the default order. |
||
| 897 | * |
||
| 898 | * This is necessary if we want to insert a non-default order entry for |
||
| 899 | * the first time. |
||
| 900 | * |
||
| 901 | * @param string $path The path to initialize. |
||
| 902 | * @param string $insertedPath The path that is being inserted. |
||
| 903 | * @param array $references The references for each defined path mapping |
||
| 904 | * in the path chain. |
||
| 905 | */ |
||
| 906 | 6 | private function initWithDefaultOrder($path, $insertedPath, $references) |
|
| 939 | } |
||
| 940 |
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.