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 CFileBasedContent 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 CFileBasedContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CFileBasedContent |
||
9 | { |
||
10 | use \Anax\TConfigure, |
||
11 | \Anax\DI\TInjectionAware, |
||
12 | TFBCBreadcrumb, |
||
13 | TFBCLoadAdditionalContent, |
||
14 | TFBCUtilities; |
||
15 | |||
16 | |||
17 | |||
18 | /** |
||
19 | * All routes. |
||
20 | */ |
||
21 | private $index = null; |
||
22 | |||
23 | /** |
||
24 | * All authors. |
||
25 | */ |
||
26 | private $author = null; |
||
27 | |||
28 | /** |
||
29 | * All categories. |
||
30 | */ |
||
31 | private $category = null; |
||
32 | |||
33 | /** |
||
34 | * All routes having meta. |
||
35 | */ |
||
36 | private $meta = null; |
||
37 | |||
38 | /** |
||
39 | * Use cache or recreate each time. |
||
40 | */ |
||
41 | private $ignoreCache = false; |
||
42 | |||
43 | /** |
||
44 | * File name pattern, all files must match this pattern and the first |
||
45 | * numbered part is optional, the second part becomes the route. |
||
46 | */ |
||
47 | private $filenamePattern = "#^(\d*)_*([^\.]+)\.md$#"; |
||
48 | |||
49 | /** |
||
50 | * Internal routes that is marked as internal content routes and not |
||
51 | * exposed as public routes. |
||
52 | */ |
||
53 | private $internalRouteDirPattern = [ |
||
54 | "#block/#", |
||
55 | ]; |
||
56 | |||
57 | private $internalRouteFilePattern = [ |
||
58 | "#^block[_-]{1}#", |
||
59 | "#^_#", |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * Routes that should be used in toc. |
||
64 | */ |
||
65 | private $allowedInTocPattern = "([\d]+_(\w)+)"; |
||
66 | |||
67 | |||
68 | |||
69 | /** |
||
70 | * Set default values from configuration. |
||
71 | * |
||
72 | * @return this. |
||
|
|||
73 | */ |
||
74 | public function setDefaultsFromConfiguration() |
||
82 | |||
83 | |||
84 | |||
85 | /** |
||
86 | * Should the cache be used or ignored. |
||
87 | * |
||
88 | * @param boolean $use true to use the cache or false to ignore the cache |
||
89 | * |
||
90 | * @return this. |
||
91 | */ |
||
92 | public function useCache($use) |
||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * Create the index of all content into an array. |
||
103 | * |
||
104 | * @param string $type of index to load. |
||
105 | * |
||
106 | * @return void. |
||
107 | */ |
||
108 | private function load($type) |
||
127 | |||
128 | |||
129 | |||
130 | |||
131 | // = Create and manage index ================================== |
||
132 | |||
133 | /** |
||
134 | * Generate an index from the directory structure. |
||
135 | * |
||
136 | * @return array as index for all content files. |
||
137 | */ |
||
138 | private function createIndex() |
||
178 | |||
179 | |||
180 | |||
181 | /** |
||
182 | * Check if a filename is to be marked as an internal route.. |
||
183 | * |
||
184 | * @param string $filepath as the basepath (routepart) to the file. |
||
185 | * |
||
186 | * @return boolean true if the route content is internal, else false |
||
187 | */ |
||
188 | private function isInternalRoute($filepath) |
||
205 | |||
206 | |||
207 | |||
208 | /** |
||
209 | * Check if filepath should be used as part of toc. |
||
210 | * |
||
211 | * @param string $filepath as the basepath (routepart) to the file. |
||
212 | * |
||
213 | * @return boolean true if the route content shoul dbe in toc, else false |
||
214 | */ |
||
215 | private function allowInToc($filepath) |
||
219 | |||
220 | |||
221 | |||
222 | // = Create and manage meta ================================== |
||
223 | |||
224 | /** |
||
225 | * Generate an index for meta files. |
||
226 | * |
||
227 | * @return array as meta index. |
||
228 | */ |
||
229 | private function createMeta() |
||
258 | |||
259 | |||
260 | |||
261 | /** |
||
262 | * Get a reference to meta data for specific route. |
||
263 | * |
||
264 | * @param string $route current route used to access page. |
||
265 | * |
||
266 | * @return array as table of content. |
||
267 | */ |
||
268 | private function getMetaForRoute($route) |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * Create a table of content for routes at particular level. |
||
280 | * |
||
281 | * @param string $route base route to use. |
||
282 | * |
||
283 | * @return array as the toc. |
||
284 | */ |
||
285 | private function createBaseRouteToc($route) |
||
311 | |||
312 | |||
313 | |||
314 | // = Deal with authors ==================================== |
||
315 | |||
316 | /** |
||
317 | * Generate a lookup index for authors that maps into the meta entry |
||
318 | * for the author. |
||
319 | * |
||
320 | * @return void. |
||
321 | */ |
||
322 | private function createAuthor() |
||
345 | |||
346 | |||
347 | |||
348 | /** |
||
349 | * Load details for the author. |
||
350 | * |
||
351 | * @param array|string $author with details on the author(s). |
||
352 | * |
||
353 | * @return array with more details on the authors(s). |
||
354 | */ |
||
355 | View Code Duplication | private function loadAuthorDetails($author) |
|
379 | |||
380 | |||
381 | |||
382 | // = Deal with categories ==================================== |
||
383 | |||
384 | /** |
||
385 | * Generate a lookup index for categories that maps into the meta entry |
||
386 | * for the category. |
||
387 | * |
||
388 | * @return void. |
||
389 | */ |
||
390 | private function createCategory() |
||
408 | |||
409 | |||
410 | |||
411 | /** |
||
412 | * Find next and previous links of current content. |
||
413 | * |
||
414 | * @param array|string $author with details on the category(s). |
||
415 | * |
||
416 | * @return array with more details on the category(s). |
||
417 | */ |
||
418 | View Code Duplication | private function loadCategoryDetails($category) |
|
442 | |||
443 | |||
444 | |||
445 | |||
446 | // == Used by meta and breadcrumb (to get title) =========================== |
||
447 | // TODO REFACTOR THIS? |
||
448 | // Support getting only frontmatter. |
||
449 | // Merge with function that retrieves whole filtered since getting |
||
450 | // frontmatter will involve full parsing of document. |
||
451 | // Title is retrieved from the HTML code. |
||
452 | // Also do cacheing of each retrieved and parsed document |
||
453 | // in this cycle, to gather code that loads and parses a individual |
||
454 | // document. |
||
455 | |||
456 | /** |
||
457 | * Get the frontmatter of a document. |
||
458 | * |
||
459 | * @param string $file to get frontmatter from. |
||
460 | * |
||
461 | * @return array as frontmatter. |
||
462 | */ |
||
463 | private function getFrontmatter($file) |
||
475 | |||
476 | |||
477 | |||
478 | // == Look up route in index =================================== |
||
479 | |||
480 | /** |
||
481 | * Map the route to the correct key in the index. |
||
482 | * |
||
483 | * @param string $route current route used to access page. |
||
484 | * |
||
485 | * @return string as key or false if no match. |
||
486 | */ |
||
487 | private function mapRoute2IndexKey($route) |
||
501 | |||
502 | |||
503 | |||
504 | /** |
||
505 | * Map the route to the correct entry in the index. |
||
506 | * |
||
507 | * @param string $route current route used to access page. |
||
508 | * |
||
509 | * @return array as the matched route. |
||
510 | */ |
||
511 | private function mapRoute2Index($route) |
||
521 | |||
522 | |||
523 | |||
524 | // = Get view data by merging from meta and current frontmatter ========= |
||
525 | |||
526 | /** |
||
527 | * Get view by mergin information from meta and frontmatter. |
||
528 | * |
||
529 | * @param string $route current route used to access page. |
||
530 | * @param array $frontmatter for the content. |
||
531 | * @param string $key for the view to retrive. |
||
532 | * @param string $distinct how to merge the array. |
||
533 | * |
||
534 | * @return array with data to add as view. |
||
535 | */ |
||
536 | private function getView($route, $frontmatter, $key, $distinct = true) |
||
557 | |||
558 | |||
559 | |||
560 | /** |
||
561 | * Get details on extra views. |
||
562 | * |
||
563 | * @param string $route current route used to access page. |
||
564 | * @param array $frontmatter for the content. |
||
565 | * |
||
566 | * @return array with page data to send to view. |
||
567 | */ |
||
568 | private function getViews($route, $frontmatter) |
||
594 | |||
595 | |||
596 | |||
597 | // == Create and load content =================================== |
||
598 | |||
599 | /** |
||
600 | * Map url to content, even internal content, if such mapping can be done. |
||
601 | * |
||
602 | * @param string $route route to look up. |
||
603 | * |
||
604 | * @return object with content and filtered version. |
||
605 | */ |
||
606 | private function createContentForInternalRoute($route) |
||
635 | |||
636 | |||
637 | |||
638 | /** |
||
639 | * Look up the route in the index and use that to retrieve the filtered |
||
640 | * content. |
||
641 | * |
||
642 | * @param string $route to look up. |
||
643 | * |
||
644 | * @return array with content and filtered version. |
||
645 | */ |
||
646 | private function mapRoute2Content($route) |
||
654 | |||
655 | |||
656 | |||
657 | /** |
||
658 | * Load content file and frontmatter, this is the first time we process |
||
659 | * the content. |
||
660 | * |
||
661 | * @param string $key to index with details on the route. |
||
662 | * |
||
663 | * @throws NotFoundException when mapping can not be done. |
||
664 | * |
||
665 | * @return void. |
||
666 | */ |
||
667 | private function loadFileContentPhaseOne($key) |
||
688 | |||
689 | |||
690 | |||
691 | // == Process content phase 2 =================================== |
||
692 | // TODO REFACTOR THIS? |
||
693 | |||
694 | /** |
||
695 | * Look up the route in the index and use that to retrieve the filtered |
||
696 | * content. |
||
697 | * |
||
698 | * @param array &$content to process. |
||
699 | * @param object &$filtered to use for settings. |
||
700 | * |
||
701 | * @return array with content and filtered version. |
||
702 | */ |
||
703 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
755 | |||
756 | |||
757 | |||
758 | // == Public methods ============================================ |
||
759 | |||
760 | /** |
||
761 | * Map url to content, even internal content, if such mapping can be done. |
||
762 | * |
||
763 | * @param string $route optional route to look up. |
||
764 | * |
||
765 | * @return object with content and filtered version. |
||
766 | */ |
||
767 | public function contentForInternalRoute($route = null) |
||
786 | |||
787 | |||
788 | |||
789 | /** |
||
790 | * Map url to content if such mapping can be done, exclude internal routes. |
||
791 | * |
||
792 | * @param string $route optional route to look up. |
||
793 | * |
||
794 | * @return object with content and filtered version. |
||
795 | */ |
||
796 | public function contentForRoute($route = null) |
||
806 | } |
||
807 |
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.