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 | * This is the base route. |
||
| 40 | */ |
||
| 41 | private $baseRoute = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * This is the extendede meta route, if any. |
||
| 45 | */ |
||
| 46 | private $metaRoute = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This is the current page, to supply pagination, if used. |
||
| 50 | */ |
||
| 51 | private $currentPage = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Use cache or recreate each time. |
||
| 55 | */ |
||
| 56 | private $ignoreCache = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * File name pattern, all files must match this pattern and the first |
||
| 60 | * numbered part is optional, the second part becomes the route. |
||
| 61 | */ |
||
| 62 | private $filenamePattern = "#^(\d*)_*([^\.]+)\.md$#"; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Internal routes that is marked as internal content routes and not |
||
| 66 | * exposed as public routes. |
||
| 67 | */ |
||
| 68 | private $internalRouteDirPattern = [ |
||
| 69 | "#block/#", |
||
| 70 | ]; |
||
| 71 | |||
| 72 | private $internalRouteFilePattern = [ |
||
| 73 | "#^block[_-]{1}#", |
||
| 74 | "#^_#", |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Routes that should be used in toc. |
||
| 79 | */ |
||
| 80 | private $allowedInTocPattern = "([\d]+_(\w)+)"; |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Set default values from configuration. |
||
| 86 | * |
||
| 87 | * @return this. |
||
|
|
|||
| 88 | */ |
||
| 89 | public function setDefaultsFromConfiguration() |
||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Should the cache be used or ignored. |
||
| 102 | * |
||
| 103 | * @param boolean $use true to use the cache or false to ignore the cache |
||
| 104 | * |
||
| 105 | * @return this. |
||
| 106 | */ |
||
| 107 | public function useCache($use) |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Create the index of all content into an array. |
||
| 118 | * |
||
| 119 | * @param string $type of index to load. |
||
| 120 | * |
||
| 121 | * @return void. |
||
| 122 | */ |
||
| 123 | private function load($type) |
||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | // = Create and manage index ================================== |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Generate an index from the directory structure. |
||
| 150 | * |
||
| 151 | * @return array as index for all content files. |
||
| 152 | */ |
||
| 153 | private function createIndex() |
||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Check if a filename is to be marked as an internal route.. |
||
| 198 | * |
||
| 199 | * @param string $filepath as the basepath (routepart) to the file. |
||
| 200 | * |
||
| 201 | * @return boolean true if the route content is internal, else false |
||
| 202 | */ |
||
| 203 | private function isInternalRoute($filepath) |
||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if filepath should be used as part of toc. |
||
| 225 | * |
||
| 226 | * @param string $filepath as the basepath (routepart) to the file. |
||
| 227 | * |
||
| 228 | * @return boolean true if the route content shoul dbe in toc, else false |
||
| 229 | */ |
||
| 230 | private function allowInToc($filepath) |
||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | // = Create and manage meta ================================== |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Generate an index for meta files. |
||
| 241 | * |
||
| 242 | * @return array as meta index. |
||
| 243 | */ |
||
| 244 | private function createMeta() |
||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Get a reference to meta data for specific route. |
||
| 278 | * |
||
| 279 | * @param string $route current route used to access page. |
||
| 280 | * |
||
| 281 | * @return array as table of content. |
||
| 282 | */ |
||
| 283 | private function getMetaForRoute($route) |
||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Create a table of content for routes at particular level. |
||
| 295 | * |
||
| 296 | * @param string $route base route to use. |
||
| 297 | * |
||
| 298 | * @return array as the toc. |
||
| 299 | */ |
||
| 300 | private function createBaseRouteToc($route) |
||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | // = Deal with authors ==================================== |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Generate a lookup index for authors that maps into the meta entry |
||
| 333 | * for the author. |
||
| 334 | * |
||
| 335 | * @return void. |
||
| 336 | */ |
||
| 337 | private function createAuthor() |
||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Load details for the author. |
||
| 366 | * |
||
| 367 | * @param array|string $author with details on the author(s). |
||
| 368 | * |
||
| 369 | * @return array with more details on the authors(s). |
||
| 370 | */ |
||
| 371 | View Code Duplication | private function loadAuthorDetails($author) |
|
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | // = Deal with categories ==================================== |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Generate a lookup index for categories that maps into the meta entry |
||
| 402 | * for the category. |
||
| 403 | * |
||
| 404 | * @return void. |
||
| 405 | */ |
||
| 406 | private function createCategory() |
||
| 424 | |||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * Find next and previous links of current content. |
||
| 429 | * |
||
| 430 | * @param array|string $author with details on the category(s). |
||
| 431 | * |
||
| 432 | * @return array with more details on the category(s). |
||
| 433 | */ |
||
| 434 | View Code Duplication | private function loadCategoryDetails($category) |
|
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | |||
| 462 | // == Used by meta and breadcrumb (to get title) =========================== |
||
| 463 | // TODO REFACTOR THIS? |
||
| 464 | // Support getting only frontmatter. |
||
| 465 | // Merge with function that retrieves whole filtered since getting |
||
| 466 | // frontmatter will involve full parsing of document. |
||
| 467 | // Title is retrieved from the HTML code. |
||
| 468 | // Also do cacheing of each retrieved and parsed document |
||
| 469 | // in this cycle, to gather code that loads and parses a individual |
||
| 470 | // document. |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the frontmatter of a document. |
||
| 474 | * |
||
| 475 | * @param string $file to get frontmatter from. |
||
| 476 | * |
||
| 477 | * @return array as frontmatter. |
||
| 478 | */ |
||
| 479 | private function getFrontmatter($file) |
||
| 491 | |||
| 492 | |||
| 493 | |||
| 494 | // == Look up route in index =================================== |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Check if currrent route is a supported meta route. |
||
| 498 | * |
||
| 499 | * @param string $route current route used to access page. |
||
| 500 | * |
||
| 501 | * @return string as route. |
||
| 502 | */ |
||
| 503 | private function checkForMetaRoute($route) |
||
| 525 | |||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Map the route to the correct key in the index. |
||
| 530 | * |
||
| 531 | * @param string $route current route used to access page. |
||
| 532 | * |
||
| 533 | * @return string as key or false if no match. |
||
| 534 | */ |
||
| 535 | private function mapRoute2IndexKey($route) |
||
| 549 | |||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * Map the route to the correct entry in the index. |
||
| 554 | * |
||
| 555 | * @param string $route current route used to access page. |
||
| 556 | * |
||
| 557 | * @return array as the matched route. |
||
| 558 | */ |
||
| 559 | private function mapRoute2Index($route) |
||
| 569 | |||
| 570 | |||
| 571 | |||
| 572 | // = Get view data by merging from meta and current frontmatter ========= |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get view by mergin information from meta and frontmatter. |
||
| 576 | * |
||
| 577 | * @param string $route current route used to access page. |
||
| 578 | * @param array $frontmatter for the content. |
||
| 579 | * @param string $key for the view to retrive. |
||
| 580 | * |
||
| 581 | * @return array with data to add as view. |
||
| 582 | */ |
||
| 583 | private function getView($route, $frontmatter, $key) |
||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * Get details on extra views. |
||
| 606 | * |
||
| 607 | * @param string $route current route used to access page. |
||
| 608 | * @param array $frontmatter for the content. |
||
| 609 | * |
||
| 610 | * @return array with page data to send to view. |
||
| 611 | */ |
||
| 612 | private function getViews($route, $frontmatter) |
||
| 638 | |||
| 639 | |||
| 640 | |||
| 641 | // == Create and load content =================================== |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Map url to content, even internal content, if such mapping can be done. |
||
| 645 | * |
||
| 646 | * @param string $route route to look up. |
||
| 647 | * |
||
| 648 | * @return object with content and filtered version. |
||
| 649 | */ |
||
| 650 | private function createContentForInternalRoute($route) |
||
| 683 | |||
| 684 | |||
| 685 | |||
| 686 | /** |
||
| 687 | * Look up the route in the index and use that to retrieve the filtered |
||
| 688 | * content. |
||
| 689 | * |
||
| 690 | * @param string $route to look up. |
||
| 691 | * |
||
| 692 | * @return array with content and filtered version. |
||
| 693 | */ |
||
| 694 | private function mapRoute2Content($route) |
||
| 702 | |||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * Load content file and frontmatter, this is the first time we process |
||
| 707 | * the content. |
||
| 708 | * |
||
| 709 | * @param string $key to index with details on the route. |
||
| 710 | * |
||
| 711 | * @throws NotFoundException when mapping can not be done. |
||
| 712 | * |
||
| 713 | * @return void. |
||
| 714 | */ |
||
| 715 | private function loadFileContentPhaseOne($key) |
||
| 736 | |||
| 737 | |||
| 738 | |||
| 739 | // == Process content phase 2 =================================== |
||
| 740 | // TODO REFACTOR THIS? |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Look up the route in the index and use that to retrieve the filtered |
||
| 744 | * content. |
||
| 745 | * |
||
| 746 | * @param array &$content to process. |
||
| 747 | * @param object &$filtered to use for settings. |
||
| 748 | * |
||
| 749 | * @return array with content and filtered version. |
||
| 750 | */ |
||
| 751 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
| 815 | |||
| 816 | |||
| 817 | |||
| 818 | // == Public methods ============================================ |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Map url to content, even internal content, if such mapping can be done. |
||
| 822 | * |
||
| 823 | * @param string $route optional route to look up. |
||
| 824 | * |
||
| 825 | * @return object with content and filtered version. |
||
| 826 | */ |
||
| 827 | public function contentForInternalRoute($route = null) |
||
| 846 | |||
| 847 | |||
| 848 | |||
| 849 | /** |
||
| 850 | * Map url to content if such mapping can be done, exclude internal routes. |
||
| 851 | * |
||
| 852 | * @param string $route optional route to look up. |
||
| 853 | * |
||
| 854 | * @return object with content and filtered version. |
||
| 855 | */ |
||
| 856 | public function contentForRoute($route = null) |
||
| 866 | } |
||
| 867 |
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.