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 | * @return this. |
||
|
|||
90 | */ |
||
91 | public function setDefaultsFromConfiguration() |
||
99 | |||
100 | |||
101 | |||
102 | /** |
||
103 | * Should the cache be used or ignored. |
||
104 | * |
||
105 | * @param boolean $use true to use the cache or false to ignore the cache |
||
106 | * |
||
107 | * @return this. |
||
108 | */ |
||
109 | public function useCache($use) |
||
115 | |||
116 | |||
117 | |||
118 | /** |
||
119 | * Create the index of all content into an array. |
||
120 | * |
||
121 | * @param string $type of index to load. |
||
122 | * |
||
123 | * @return void. |
||
124 | */ |
||
125 | private function load($type) |
||
144 | |||
145 | |||
146 | |||
147 | |||
148 | // = Create and manage index ================================== |
||
149 | |||
150 | /** |
||
151 | * Generate an index from the directory structure. |
||
152 | * |
||
153 | * @return array as index for all content files. |
||
154 | */ |
||
155 | private function createIndex() |
||
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * Check if a filename is to be marked as an internal route.. |
||
200 | * |
||
201 | * @param string $filepath as the basepath (routepart) to the file. |
||
202 | * |
||
203 | * @return boolean true if the route content is internal, else false |
||
204 | */ |
||
205 | private function isInternalRoute($filepath) |
||
222 | |||
223 | |||
224 | |||
225 | /** |
||
226 | * Check if filepath should be used as part of toc. |
||
227 | * |
||
228 | * @param string $filepath as the basepath (routepart) to the file. |
||
229 | * |
||
230 | * @return boolean true if the route content shoul dbe in toc, else false |
||
231 | */ |
||
232 | private function allowInToc($filepath) |
||
236 | |||
237 | |||
238 | |||
239 | // = Create and manage meta ================================== |
||
240 | |||
241 | /** |
||
242 | * Generate an index for meta files. |
||
243 | * |
||
244 | * @return array as meta index. |
||
245 | */ |
||
246 | private function createMeta() |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * Get a reference to meta data for specific route. |
||
280 | * |
||
281 | * @param string $route current route used to access page. |
||
282 | * |
||
283 | * @return array as table of content. |
||
284 | */ |
||
285 | private function getMetaForRoute($route) |
||
292 | |||
293 | |||
294 | |||
295 | /** |
||
296 | * Create a table of content for routes at particular level. |
||
297 | * |
||
298 | * @param string $route base route to use. |
||
299 | * |
||
300 | * @return array as the toc. |
||
301 | */ |
||
302 | private function createBaseRouteToc($route) |
||
328 | |||
329 | |||
330 | |||
331 | // = Deal with authors ==================================== |
||
332 | |||
333 | /** |
||
334 | * Generate a lookup index for authors that maps into the meta entry |
||
335 | * for the author. |
||
336 | * |
||
337 | * @return void. |
||
338 | */ |
||
339 | private function createAuthor() |
||
363 | |||
364 | |||
365 | |||
366 | /** |
||
367 | * Load details for the author. |
||
368 | * |
||
369 | * @param array|string $author with details on the author(s). |
||
370 | * |
||
371 | * @return array with more details on the authors(s). |
||
372 | */ |
||
373 | View Code Duplication | private function loadAuthorDetails($author) |
|
397 | |||
398 | |||
399 | |||
400 | // = Deal with categories ==================================== |
||
401 | |||
402 | /** |
||
403 | * Generate a lookup index for categories that maps into the meta entry |
||
404 | * for the category. |
||
405 | * |
||
406 | * @return void. |
||
407 | */ |
||
408 | private function createCategory() |
||
426 | |||
427 | |||
428 | |||
429 | /** |
||
430 | * Find next and previous links of current content. |
||
431 | * |
||
432 | * @param array|string $author with details on the category(s). |
||
433 | * |
||
434 | * @return array with more details on the category(s). |
||
435 | */ |
||
436 | View Code Duplication | private function loadCategoryDetails($category) |
|
460 | |||
461 | |||
462 | |||
463 | |||
464 | // == Used by meta and breadcrumb (to get title) =========================== |
||
465 | // TODO REFACTOR THIS? |
||
466 | // Support getting only frontmatter. |
||
467 | // Merge with function that retrieves whole filtered since getting |
||
468 | // frontmatter will involve full parsing of document. |
||
469 | // Title is retrieved from the HTML code. |
||
470 | // Also do cacheing of each retrieved and parsed document |
||
471 | // in this cycle, to gather code that loads and parses a individual |
||
472 | // document. |
||
473 | |||
474 | /** |
||
475 | * Get the frontmatter of a document. |
||
476 | * |
||
477 | * @param string $file to get frontmatter from. |
||
478 | * |
||
479 | * @return array as frontmatter. |
||
480 | */ |
||
481 | private function getFrontmatter($file) |
||
493 | |||
494 | |||
495 | |||
496 | // == Look up route in index =================================== |
||
497 | |||
498 | /** |
||
499 | * Check if currrent route is a supported meta route. |
||
500 | * |
||
501 | * @param string $route current route used to access page. |
||
502 | * |
||
503 | * @return string as route. |
||
504 | */ |
||
505 | private function checkForMetaRoute($route) |
||
527 | |||
528 | |||
529 | |||
530 | /** |
||
531 | * Map the route to the correct key in the index. |
||
532 | * |
||
533 | * @param string $route current route used to access page. |
||
534 | * |
||
535 | * @return string as key or false if no match. |
||
536 | */ |
||
537 | private function mapRoute2IndexKey($route) |
||
551 | |||
552 | |||
553 | |||
554 | /** |
||
555 | * Map the route to the correct entry in the index. |
||
556 | * |
||
557 | * @param string $route current route used to access page. |
||
558 | * |
||
559 | * @return array as the matched route. |
||
560 | */ |
||
561 | private function mapRoute2Index($route) |
||
574 | |||
575 | |||
576 | |||
577 | // = Get view data by merging from meta and current frontmatter ========= |
||
578 | |||
579 | /** |
||
580 | * Get view by mergin information from meta and frontmatter. |
||
581 | * |
||
582 | * @param string $route current route used to access page. |
||
583 | * @param array $frontmatter for the content. |
||
584 | * @param string $key for the view to retrive. |
||
585 | * |
||
586 | * @return array with data to add as view. |
||
587 | */ |
||
588 | private function getView($route, $frontmatter, $key) |
||
606 | |||
607 | |||
608 | |||
609 | /** |
||
610 | * Get details on extra views. |
||
611 | * |
||
612 | * @param string $route current route used to access page. |
||
613 | * @param array $frontmatter for the content. |
||
614 | * |
||
615 | * @return array with page data to send to view. |
||
616 | */ |
||
617 | private function getViews($route, $frontmatter) |
||
643 | |||
644 | |||
645 | |||
646 | // == Create and load content =================================== |
||
647 | |||
648 | /** |
||
649 | * Map url to content, even internal content, if such mapping can be done. |
||
650 | * |
||
651 | * @param string $route route to look up. |
||
652 | * |
||
653 | * @return object with content and filtered version. |
||
654 | */ |
||
655 | private function createContentForInternalRoute($route) |
||
688 | |||
689 | |||
690 | |||
691 | /** |
||
692 | * Look up the route in the index and use that to retrieve the filtered |
||
693 | * content. |
||
694 | * |
||
695 | * @param string $route to look up. |
||
696 | * |
||
697 | * @return array with content and filtered version. |
||
698 | */ |
||
699 | private function mapRoute2Content($route) |
||
707 | |||
708 | |||
709 | |||
710 | /** |
||
711 | * Load content file and frontmatter, this is the first time we process |
||
712 | * the content. |
||
713 | * |
||
714 | * @param string $key to index with details on the route. |
||
715 | * |
||
716 | * @throws NotFoundException when mapping can not be done. |
||
717 | * |
||
718 | * @return void. |
||
719 | */ |
||
720 | private function loadFileContentPhaseOne($key) |
||
741 | |||
742 | |||
743 | |||
744 | // == Process content phase 2 =================================== |
||
745 | // TODO REFACTOR THIS? |
||
746 | |||
747 | /** |
||
748 | * Look up the route in the index and use that to retrieve the filtered |
||
749 | * content. |
||
750 | * |
||
751 | * @param array &$content to process. |
||
752 | * @param object &$filtered to use for settings. |
||
753 | * |
||
754 | * @return array with content and filtered version. |
||
755 | */ |
||
756 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
825 | |||
826 | |||
827 | |||
828 | // == Public methods ============================================ |
||
829 | |||
830 | /** |
||
831 | * Map url to content, even internal content, if such mapping can be done. |
||
832 | * |
||
833 | * @param string $route optional route to look up. |
||
834 | * |
||
835 | * @return object with content and filtered version. |
||
836 | */ |
||
837 | public function contentForInternalRoute($route = null) |
||
856 | |||
857 | |||
858 | |||
859 | /** |
||
860 | * Map url to content if such mapping can be done, exclude internal routes. |
||
861 | * |
||
862 | * @param string $route optional route to look up. |
||
863 | * |
||
864 | * @return object with content and filtered version. |
||
865 | */ |
||
866 | public function contentForRoute($route = null) |
||
876 | } |
||
877 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.