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() |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Get a reference to meta data for specific route. |
||
| 262 | * |
||
| 263 | * @param string $route current route used to access page. |
||
| 264 | * |
||
| 265 | * @return array as table of content. |
||
| 266 | */ |
||
| 267 | private function getMetaForRoute($route) |
||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Create a table of content for routes at particular level. |
||
| 279 | * |
||
| 280 | * @param string $route base route to use. |
||
| 281 | * |
||
| 282 | * @return array as the toc. |
||
| 283 | */ |
||
| 284 | private function createBaseRouteToc($route) |
||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | // = Deal with authors ==================================== |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Generate a lookup index for authors that maps into the meta entry |
||
| 312 | * for the author. |
||
| 313 | * |
||
| 314 | * @return void. |
||
| 315 | */ |
||
| 316 | private function createAuthor() |
||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Find next and previous links of current content. |
||
| 342 | * |
||
| 343 | * @param array|string $author with details on the author(s). |
||
| 344 | * |
||
| 345 | * @return array with more details on the authors(s). |
||
| 346 | */ |
||
| 347 | private function loadAuthorDetails($author) |
||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | // == Used by meta and breadcrumb (to get title) =========================== |
||
| 374 | // TODO REFACTOR THIS? |
||
| 375 | // Support getting only frontmatter. |
||
| 376 | // Merge with function that retrieves whole filtered since getting |
||
| 377 | // frontmatter will involve full parsing of document. |
||
| 378 | // Title is retrieved from the HTML code. |
||
| 379 | // Also do cacheing of each retrieved and parsed document |
||
| 380 | // in this cycle, to gather code that loads and parses a individual |
||
| 381 | // document. |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the frontmatter of a document. |
||
| 385 | * |
||
| 386 | * @param string $file to get frontmatter from. |
||
| 387 | * |
||
| 388 | * @return array as frontmatter. |
||
| 389 | */ |
||
| 390 | private function getFrontmatter($file) |
||
| 402 | |||
| 403 | |||
| 404 | |||
| 405 | // = Section X to be labeled ================================== |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Load the content from filtered and parse it step two. |
||
| 409 | * |
||
| 410 | * @param string $file to get content from. |
||
| 411 | * |
||
| 412 | * @return object as filtered content. |
||
| 413 | */ |
||
| 414 | /* |
||
| 415 | private function loadPureContentPhase2($filtered) |
||
| 416 | { |
||
| 417 | $filter = $this->config["textfilter"]; |
||
| 418 | $text = $filtered->text; |
||
| 419 | |||
| 420 | // Get new filtered content |
||
| 421 | $new = $this->di->get("textFilter")->parse($text, $filter); |
||
| 422 | $filtered->text = $new->text; |
||
| 423 | |||
| 424 | // Update all anchor urls to use baseurl, needs info about baseurl |
||
| 425 | // from merged frontmatter |
||
| 426 | //$baseurl = $this->getBaseurl($content["views"]); |
||
| 427 | //$this->addBaseurl2AnchorUrls($filtered, $baseurl); |
||
| 428 | |||
| 429 | return $filtered; |
||
| 430 | } |
||
| 431 | |||
| 432 | */ |
||
| 433 | |||
| 434 | |||
| 435 | // == Look up route in index =================================== |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Map the route to the correct key in the index. |
||
| 439 | * |
||
| 440 | * @param string $route current route used to access page. |
||
| 441 | * |
||
| 442 | * @return string as key or false if no match. |
||
| 443 | */ |
||
| 444 | private function mapRoute2IndexKey($route) |
||
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * Map the route to the correct entry in the index. |
||
| 463 | * |
||
| 464 | * @param string $route current route used to access page. |
||
| 465 | * |
||
| 466 | * @return array as the matched route. |
||
| 467 | */ |
||
| 468 | private function mapRoute2Index($route) |
||
| 478 | |||
| 479 | |||
| 480 | |||
| 481 | // = Get view data by merging from meta and current frontmatter ========= |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get view by mergin information from meta and frontmatter. |
||
| 485 | * |
||
| 486 | * @param string $route current route used to access page. |
||
| 487 | * @param array $frontmatter for the content. |
||
| 488 | * @param string $key for the view to retrive. |
||
| 489 | * @param string $distinct how to merge the array. |
||
| 490 | * |
||
| 491 | * @return array with data to add as view. |
||
| 492 | */ |
||
| 493 | private function getView($route, $frontmatter, $key, $distinct = true) |
||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Get details on extra views. |
||
| 519 | * |
||
| 520 | * @param string $route current route used to access page. |
||
| 521 | * @param array $frontmatter for the content. |
||
| 522 | * |
||
| 523 | * @return array with page data to send to view. |
||
| 524 | */ |
||
| 525 | private function getViews($route, $frontmatter) |
||
| 551 | |||
| 552 | |||
| 553 | |||
| 554 | // == Create and load content =================================== |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Map url to content, even internal content, if such mapping can be done. |
||
| 558 | * |
||
| 559 | * @param string $route route to look up. |
||
| 560 | * |
||
| 561 | * @return object with content and filtered version. |
||
| 562 | */ |
||
| 563 | private function createContentForInternalRoute($route) |
||
| 591 | |||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * Look up the route in the index and use that to retrieve the filtered |
||
| 596 | * content. |
||
| 597 | * |
||
| 598 | * @param string $route to look up. |
||
| 599 | * |
||
| 600 | * @return array with content and filtered version. |
||
| 601 | */ |
||
| 602 | private function mapRoute2Content($route) |
||
| 610 | |||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Load content file and frontmatter, this is the first time we process |
||
| 615 | * the content. |
||
| 616 | * |
||
| 617 | * @param string $key to index with details on the route. |
||
| 618 | * |
||
| 619 | * @throws NotFoundException when mapping can not be done. |
||
| 620 | * |
||
| 621 | * @return void. |
||
| 622 | */ |
||
| 623 | private function loadFileContentPhaseOne($key) |
||
| 644 | |||
| 645 | |||
| 646 | |||
| 647 | // == Process content phase 2 =================================== |
||
| 648 | // TODO REFACTOR THIS? |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Look up the route in the index and use that to retrieve the filtered |
||
| 652 | * content. |
||
| 653 | * |
||
| 654 | * @param array &$content to process. |
||
| 655 | * @param object &$filtered to use for settings. |
||
| 656 | * |
||
| 657 | * @return array with content and filtered version. |
||
| 658 | */ |
||
| 659 | private function processMainContentPhaseTwo(&$content, &$filtered) |
||
| 705 | |||
| 706 | |||
| 707 | |||
| 708 | // == Public methods ============================================ |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Map url to content, even internal content, if such mapping can be done. |
||
| 712 | * |
||
| 713 | * @param string $route optional route to look up. |
||
| 714 | * |
||
| 715 | * @return object with content and filtered version. |
||
| 716 | */ |
||
| 717 | public function contentForInternalRoute($route = null) |
||
| 736 | |||
| 737 | |||
| 738 | |||
| 739 | /** |
||
| 740 | * Map url to content if such mapping can be done, exclude internal routes. |
||
| 741 | * |
||
| 742 | * @param string $route optional route to look up. |
||
| 743 | * |
||
| 744 | * @return object with content and filtered version. |
||
| 745 | */ |
||
| 746 | public function contentForRoute($route = null) |
||
| 756 | } |
||
| 757 |
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.