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() |
||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Load details for the author. |
||
| 365 | * |
||
| 366 | * @param array|string $author with details on the author(s). |
||
| 367 | * |
||
| 368 | * @return array with more details on the authors(s). |
||
| 369 | */ |
||
| 370 | View Code Duplication | private function loadAuthorDetails($author) |
|
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | // = Deal with categories ==================================== |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Generate a lookup index for categories that maps into the meta entry |
||
| 401 | * for the category. |
||
| 402 | * |
||
| 403 | * @return void. |
||
| 404 | */ |
||
| 405 | private function createCategory() |
||
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Find next and previous links of current content. |
||
| 428 | * |
||
| 429 | * @param array|string $author with details on the category(s). |
||
| 430 | * |
||
| 431 | * @return array with more details on the category(s). |
||
| 432 | */ |
||
| 433 | View Code Duplication | private function loadCategoryDetails($category) |
|
| 457 | |||
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | // == Used by meta and breadcrumb (to get title) =========================== |
||
| 462 | // TODO REFACTOR THIS? |
||
| 463 | // Support getting only frontmatter. |
||
| 464 | // Merge with function that retrieves whole filtered since getting |
||
| 465 | // frontmatter will involve full parsing of document. |
||
| 466 | // Title is retrieved from the HTML code. |
||
| 467 | // Also do cacheing of each retrieved and parsed document |
||
| 468 | // in this cycle, to gather code that loads and parses a individual |
||
| 469 | // document. |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the frontmatter of a document. |
||
| 473 | * |
||
| 474 | * @param string $file to get frontmatter from. |
||
| 475 | * |
||
| 476 | * @return array as frontmatter. |
||
| 477 | */ |
||
| 478 | private function getFrontmatter($file) |
||
| 490 | |||
| 491 | |||
| 492 | |||
| 493 | // == Look up route in index =================================== |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check if currrent route is a supported meta route. |
||
| 497 | * |
||
| 498 | * @param string $route current route used to access page. |
||
| 499 | * |
||
| 500 | * @return string as route. |
||
| 501 | */ |
||
| 502 | private function checkForMetaRoute($route) |
||
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Map the route to the correct key in the index. |
||
| 529 | * |
||
| 530 | * @param string $route current route used to access page. |
||
| 531 | * |
||
| 532 | * @return string as key or false if no match. |
||
| 533 | */ |
||
| 534 | private function mapRoute2IndexKey($route) |
||
| 548 | |||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * Map the route to the correct entry in the index. |
||
| 553 | * |
||
| 554 | * @param string $route current route used to access page. |
||
| 555 | * |
||
| 556 | * @return array as the matched route. |
||
| 557 | */ |
||
| 558 | private function mapRoute2Index($route) |
||
| 568 | |||
| 569 | |||
| 570 | |||
| 571 | // = Get view data by merging from meta and current frontmatter ========= |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get view by mergin information from meta and frontmatter. |
||
| 575 | * |
||
| 576 | * @param string $route current route used to access page. |
||
| 577 | * @param array $frontmatter for the content. |
||
| 578 | * @param string $key for the view to retrive. |
||
| 579 | * |
||
| 580 | * @return array with data to add as view. |
||
| 581 | */ |
||
| 582 | private function getView($route, $frontmatter, $key) |
||
| 600 | |||
| 601 | |||
| 602 | |||
| 603 | /** |
||
| 604 | * Get details on extra views. |
||
| 605 | * |
||
| 606 | * @param string $route current route used to access page. |
||
| 607 | * @param array $frontmatter for the content. |
||
| 608 | * |
||
| 609 | * @return array with page data to send to view. |
||
| 610 | */ |
||
| 611 | private function getViews($route, $frontmatter) |
||
| 637 | |||
| 638 | |||
| 639 | |||
| 640 | // == Create and load content =================================== |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Map url to content, even internal content, if such mapping can be done. |
||
| 644 | * |
||
| 645 | * @param string $route route to look up. |
||
| 646 | * |
||
| 647 | * @return object with content and filtered version. |
||
| 648 | */ |
||
| 649 | private function createContentForInternalRoute($route) |
||
| 682 | |||
| 683 | |||
| 684 | |||
| 685 | /** |
||
| 686 | * Look up the route in the index and use that to retrieve the filtered |
||
| 687 | * content. |
||
| 688 | * |
||
| 689 | * @param string $route to look up. |
||
| 690 | * |
||
| 691 | * @return array with content and filtered version. |
||
| 692 | */ |
||
| 693 | private function mapRoute2Content($route) |
||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * Load content file and frontmatter, this is the first time we process |
||
| 706 | * the content. |
||
| 707 | * |
||
| 708 | * @param string $key to index with details on the route. |
||
| 709 | * |
||
| 710 | * @throws NotFoundException when mapping can not be done. |
||
| 711 | * |
||
| 712 | * @return void. |
||
| 713 | */ |
||
| 714 | private function loadFileContentPhaseOne($key) |
||
| 735 | |||
| 736 | |||
| 737 | |||
| 738 | // == Process content phase 2 =================================== |
||
| 739 | // TODO REFACTOR THIS? |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Look up the route in the index and use that to retrieve the filtered |
||
| 743 | * content. |
||
| 744 | * |
||
| 745 | * @param array &$content to process. |
||
| 746 | * @param object &$filtered to use for settings. |
||
| 747 | * |
||
| 748 | * @return array with content and filtered version. |
||
| 749 | */ |
||
| 750 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
| 802 | |||
| 803 | |||
| 804 | |||
| 805 | // == Public methods ============================================ |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Map url to content, even internal content, if such mapping can be done. |
||
| 809 | * |
||
| 810 | * @param string $route optional route to look up. |
||
| 811 | * |
||
| 812 | * @return object with content and filtered version. |
||
| 813 | */ |
||
| 814 | public function contentForInternalRoute($route = null) |
||
| 833 | |||
| 834 | |||
| 835 | |||
| 836 | /** |
||
| 837 | * Map url to content if such mapping can be done, exclude internal routes. |
||
| 838 | * |
||
| 839 | * @param string $route optional route to look up. |
||
| 840 | * |
||
| 841 | * @return object with content and filtered version. |
||
| 842 | */ |
||
| 843 | public function contentForRoute($route = null) |
||
| 853 | } |
||
| 854 |
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.