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 FileBasedContent 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 FileBasedContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class FileBasedContent implements ContainerInjectableInterface |
||
12 | { |
||
13 | use ContainerInjectableTrait, |
||
14 | FBCBreadcrumbTrait, |
||
15 | FBCLoadAdditionalContentTrait, |
||
16 | FBCUtilitiesTrait; |
||
17 | |||
18 | |||
19 | |||
20 | /** |
||
21 | * All routes. |
||
22 | */ |
||
23 | private $index = null; |
||
24 | |||
25 | /** |
||
26 | * All authors. |
||
27 | */ |
||
28 | private $author = null; |
||
29 | |||
30 | /** |
||
31 | * All categories. |
||
32 | */ |
||
33 | private $category = null; |
||
34 | |||
35 | /** |
||
36 | * All routes having meta. |
||
37 | */ |
||
38 | private $meta = null; |
||
39 | |||
40 | /** |
||
41 | * This is the base route. |
||
42 | */ |
||
43 | private $baseRoute = null; |
||
44 | |||
45 | /** |
||
46 | * This is the extendede meta route, if any. |
||
47 | */ |
||
48 | private $metaRoute = null; |
||
49 | |||
50 | /** |
||
51 | * This is the current page, to supply pagination, if used. |
||
52 | */ |
||
53 | private $currentPage = null; |
||
54 | |||
55 | /** |
||
56 | * Use cache or recreate each time. |
||
57 | */ |
||
58 | private $ignoreCache = false; |
||
59 | |||
60 | /** |
||
61 | * File name pattern, all files must match this pattern and the first |
||
62 | * numbered part is optional, the second part becomes the route. |
||
63 | */ |
||
64 | private $filenamePattern = "#^(\d*)_*([^\.]+)\.md$#"; |
||
65 | |||
66 | /** |
||
67 | * Internal routes that is marked as internal content routes and not |
||
68 | * exposed as public routes. |
||
69 | */ |
||
70 | private $internalRouteDirPattern = [ |
||
71 | "#block/#", |
||
72 | ]; |
||
73 | |||
74 | private $internalRouteFilePattern = [ |
||
75 | "#^block[_-]{1}#", |
||
76 | "#^_#", |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * Routes that should be used in toc. |
||
81 | */ |
||
82 | private $allowedInTocPattern = "([\d]+_(\w)+)"; |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Set default values from configuration. |
||
88 | * |
||
89 | * @param array $config the configuration to use. |
||
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | 1 | public function configure(array $config) : void |
|
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * Set default values from configuration. |
||
103 | * |
||
104 | * @return this. |
||
105 | */ |
||
106 | 1 | private function setDefaultsFromConfiguration() |
|
114 | |||
115 | |||
116 | |||
117 | /** |
||
118 | * Should the cache be used or ignored. |
||
119 | * |
||
120 | * @param boolean $use true to use the cache or false to ignore the cache |
||
121 | * |
||
122 | * @return this. |
||
123 | */ |
||
124 | public function useCache($use) |
||
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * Create the index of all content into an array. |
||
135 | * |
||
136 | * @param string $type of index to load. |
||
137 | * |
||
138 | * @return void. |
||
139 | */ |
||
140 | private function load($type) |
||
159 | |||
160 | |||
161 | |||
162 | |||
163 | // = Create and manage index ================================== |
||
164 | |||
165 | /** |
||
166 | * Generate an index from the directory structure. |
||
167 | * |
||
168 | * @return array as index for all content files. |
||
169 | */ |
||
170 | private function createIndex() |
||
210 | |||
211 | |||
212 | |||
213 | /** |
||
214 | * Check if a filename is to be marked as an internal route.. |
||
215 | * |
||
216 | * @param string $filepath as the basepath (routepart) to the file. |
||
217 | * |
||
218 | * @return boolean true if the route content is internal, else false |
||
219 | */ |
||
220 | private function isInternalRoute($filepath) |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * Check if filepath should be used as part of toc. |
||
242 | * |
||
243 | * @param string $filepath as the basepath (routepart) to the file. |
||
244 | * |
||
245 | * @return boolean true if the route content shoul dbe in toc, else false |
||
246 | */ |
||
247 | private function allowInToc($filepath) |
||
251 | |||
252 | |||
253 | |||
254 | // = Create and manage meta ================================== |
||
255 | |||
256 | /** |
||
257 | * Generate an index for meta files. |
||
258 | * |
||
259 | * @return array as meta index. |
||
260 | */ |
||
261 | private function createMeta() |
||
290 | |||
291 | |||
292 | |||
293 | /** |
||
294 | * Get a reference to meta data for specific route. |
||
295 | * |
||
296 | * @param string $route current route used to access page. |
||
297 | * |
||
298 | * @return array as table of content. |
||
299 | */ |
||
300 | private function getMetaForRoute($route) |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * Create a table of content for routes at particular level. |
||
312 | * |
||
313 | * @param string $route base route to use. |
||
314 | * |
||
315 | * @return array as the toc. |
||
316 | */ |
||
317 | private function createBaseRouteToc($route) |
||
343 | |||
344 | |||
345 | |||
346 | // = Deal with authors ==================================== |
||
347 | |||
348 | /** |
||
349 | * Generate a lookup index for authors that maps into the meta entry |
||
350 | * for the author. |
||
351 | * |
||
352 | * @return void. |
||
353 | */ |
||
354 | private function createAuthor() |
||
378 | |||
379 | |||
380 | |||
381 | /** |
||
382 | * Load details for the author. |
||
383 | * |
||
384 | * @param array|string $author with details on the author(s). |
||
385 | * |
||
386 | * @return array with more details on the authors(s). |
||
387 | */ |
||
388 | View Code Duplication | private function loadAuthorDetails($author) |
|
412 | |||
413 | |||
414 | |||
415 | // = Deal with categories ==================================== |
||
416 | |||
417 | /** |
||
418 | * Generate a lookup index for categories that maps into the meta entry |
||
419 | * for the category. |
||
420 | * |
||
421 | * @return void. |
||
422 | */ |
||
423 | private function createCategory() |
||
441 | |||
442 | |||
443 | |||
444 | /** |
||
445 | * Find next and previous links of current content. |
||
446 | * |
||
447 | * @param array|string $author with details on the category(s). |
||
448 | * |
||
449 | * @return array with more details on the category(s). |
||
450 | */ |
||
451 | View Code Duplication | private function loadCategoryDetails($category) |
|
475 | |||
476 | |||
477 | |||
478 | |||
479 | // == Used by meta and breadcrumb (to get title) =========================== |
||
480 | // TODO REFACTOR THIS? |
||
481 | // Support getting only frontmatter. |
||
482 | // Merge with function that retrieves whole filtered since getting |
||
483 | // frontmatter will involve full parsing of document. |
||
484 | // Title is retrieved from the HTML code. |
||
485 | // Also do cacheing of each retrieved and parsed document |
||
486 | // in this cycle, to gather code that loads and parses a individual |
||
487 | // document. |
||
488 | |||
489 | /** |
||
490 | * Get the frontmatter of a document. |
||
491 | * |
||
492 | * @param string $file to get frontmatter from. |
||
493 | * |
||
494 | * @return array as frontmatter. |
||
495 | */ |
||
496 | private function getFrontmatter($file) |
||
508 | |||
509 | |||
510 | |||
511 | // == Look up route in index =================================== |
||
512 | |||
513 | /** |
||
514 | * Check if currrent route is a supported meta route. |
||
515 | * |
||
516 | * @param string $route current route used to access page. |
||
517 | * |
||
518 | * @return string as route. |
||
519 | */ |
||
520 | private function checkForMetaRoute($route) |
||
542 | |||
543 | |||
544 | |||
545 | /** |
||
546 | * Map the route to the correct key in the index. |
||
547 | * |
||
548 | * @param string $route current route used to access page. |
||
549 | * |
||
550 | * @return string as key or false if no match. |
||
551 | */ |
||
552 | private function mapRoute2IndexKey($route) |
||
566 | |||
567 | |||
568 | |||
569 | /** |
||
570 | * Map the route to the correct entry in the index. |
||
571 | * |
||
572 | * @param string $route current route used to access page. |
||
573 | * |
||
574 | * @return array as the matched route. |
||
575 | */ |
||
576 | private function mapRoute2Index($route) |
||
589 | |||
590 | |||
591 | |||
592 | // = Get view data by merging from meta and current frontmatter ========= |
||
593 | |||
594 | /** |
||
595 | * Get view by mergin information from meta and frontmatter. |
||
596 | * |
||
597 | * @param string $route current route used to access page. |
||
598 | * @param array $frontmatter for the content. |
||
599 | * @param string $key for the view to retrive. |
||
600 | * |
||
601 | * @return array with data to add as view. |
||
602 | */ |
||
603 | private function getView($route, $frontmatter, $key) |
||
621 | |||
622 | |||
623 | |||
624 | /** |
||
625 | * Get details on extra views. |
||
626 | * |
||
627 | * @param string $route current route used to access page. |
||
628 | * @param array $frontmatter for the content. |
||
629 | * |
||
630 | * @return array with page data to send to view. |
||
631 | */ |
||
632 | private function getViews($route, $frontmatter) |
||
658 | |||
659 | |||
660 | |||
661 | // == Create and load content =================================== |
||
662 | |||
663 | /** |
||
664 | * Map url to content, even internal content, if such mapping can be done. |
||
665 | * |
||
666 | * @param string $route route to look up. |
||
667 | * |
||
668 | * @return object with content and filtered version. |
||
669 | */ |
||
670 | private function createContentForInternalRoute($route) |
||
703 | |||
704 | |||
705 | |||
706 | /** |
||
707 | * Look up the route in the index and use that to retrieve the filtered |
||
708 | * content. |
||
709 | * |
||
710 | * @param string $route to look up. |
||
711 | * |
||
712 | * @return array with content and filtered version. |
||
713 | */ |
||
714 | private function mapRoute2Content($route) |
||
722 | |||
723 | |||
724 | |||
725 | /** |
||
726 | * Load content file and frontmatter, this is the first time we process |
||
727 | * the content. |
||
728 | * |
||
729 | * @param string $key to index with details on the route. |
||
730 | * |
||
731 | * @throws NotFoundException when mapping can not be done. |
||
732 | * |
||
733 | * @return void. |
||
734 | */ |
||
735 | private function loadFileContentPhaseOne($key) |
||
756 | |||
757 | |||
758 | |||
759 | // == Process content phase 2 =================================== |
||
760 | // TODO REFACTOR THIS? |
||
761 | |||
762 | /** |
||
763 | * Look up the route in the index and use that to retrieve the filtered |
||
764 | * content. |
||
765 | * |
||
766 | * @param array &$content to process. |
||
767 | * @param object &$filtered to use for settings. |
||
768 | * |
||
769 | * @return array with content and filtered version. |
||
770 | */ |
||
771 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
840 | |||
841 | |||
842 | |||
843 | // == Public methods ============================================ |
||
844 | |||
845 | /** |
||
846 | * Map url to content, even internal content, if such mapping can be done. |
||
847 | * |
||
848 | * @param string $route optional route to look up. |
||
849 | * |
||
850 | * @return object with content and filtered version. |
||
851 | */ |
||
852 | public function contentForInternalRoute($route = null) |
||
872 | |||
873 | |||
874 | |||
875 | /** |
||
876 | * Map url to content if such mapping can be done, exclude internal routes. |
||
877 | * |
||
878 | * @param string $route optional route to look up. |
||
879 | * |
||
880 | * @return object with content and filtered version. |
||
881 | */ |
||
882 | public function contentForRoute($route = null) |
||
892 | } |
||
893 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: