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 | |||
| 13 | |||
| 14 | |||
| 15 | /**  | 
            ||
| 16 | * Properties.  | 
            ||
| 17 | */  | 
            ||
| 18 | private $index = null;  | 
            ||
| 19 | private $meta = null;  | 
            ||
| 20 | private $ignoreCache = false;  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * File name pattern, all files must match this pattern and the first  | 
            ||
| 24 | * numbered part is optional, the second part becomes the route.  | 
            ||
| 25 | */  | 
            ||
| 26 | private $filenamePattern = "#^(\d*)_*([^\.]+)\.md$#";  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Internal routes that is marked as internal content routes and not  | 
            ||
| 30 | * exposed as public routes.  | 
            ||
| 31 | */  | 
            ||
| 32 | private $internalRouteDirPattern = [  | 
            ||
| 33 | "#block/#",  | 
            ||
| 34 | ];  | 
            ||
| 35 | |||
| 36 | private $internalRouteFilePattern = [  | 
            ||
| 37 |         "#^block[_-]{1}#", | 
            ||
| 38 | "#^_#",  | 
            ||
| 39 | ];  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * Routes that should be used in toc.  | 
            ||
| 43 | */  | 
            ||
| 44 | private $allowedInTocPattern = "([\d]+_(\w)+)";  | 
            ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Create a breadcrumb, append slash / to all dirs.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @param string $route current route.  | 
            ||
| 52 | *  | 
            ||
| 53 | * @return array with values for the breadcrumb.  | 
            ||
| 54 | */  | 
            ||
| 55 | public function createBreadcrumb($route)  | 
            ||
| 56 |     { | 
            ||
| 57 | $breadcrumbs = [];  | 
            ||
| 58 | |||
| 59 |         while ($route !== "./" && $route !== "/") { | 
            ||
| 60 | $routeIndex = $this->mapRoute2IndexKey($route);  | 
            ||
| 61 | $item["url"] = $route;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 62 | $item["text"] = $this->getBreadcrumbTitle($this->index[$routeIndex]["file"]);  | 
            ||
| 63 | $breadcrumbs[] = $item;  | 
            ||
| 64 | $route = dirname($route) . "/";  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | krsort($breadcrumbs);  | 
            ||
| 68 | return $breadcrumbs;  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * Get time when the content was last updated.  | 
            ||
| 75 | *  | 
            ||
| 76 | * @return string with the time.  | 
            ||
| 77 | */  | 
            ||
| 78 | /*public function PublishTime() { | 
            ||
| 79 |   if(!empty($this['published'])) { | 
            ||
| 80 | return $this['published'];  | 
            ||
| 81 |   } else if(isset($this['updated'])) { | 
            ||
| 82 | return $this['updated'];  | 
            ||
| 83 |   } else { | 
            ||
| 84 | return $this['created'];  | 
            ||
| 85 | }  | 
            ||
| 86 | }  | 
            ||
| 87 | */  | 
            ||
| 88 | /**  | 
            ||
| 89 | * Get the action for latest updated of the content.  | 
            ||
| 90 | *  | 
            ||
| 91 | * @return string with the time.  | 
            ||
| 92 | */  | 
            ||
| 93 | /*public function PublishAction() { | 
            ||
| 94 |   if(!empty($this['published'])) { | 
            ||
| 95 |     //return t('Published'); | 
            ||
| 96 |     return t('Last updated'); | 
            ||
| 97 |   } else if(isset($this['updated'])) { | 
            ||
| 98 |     return t('Updated'); | 
            ||
| 99 |   } else { | 
            ||
| 100 |     return t('Created'); | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | */  | 
            ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * Set default values from configuration.  | 
            ||
| 109 | *  | 
            ||
| 110 | * @return this.  | 
            ||
| 111 | */  | 
            ||
| 112 | public function setDefaultsFromConfiguration()  | 
            ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * Should the cache be used or ignored.  | 
            ||
| 125 | *  | 
            ||
| 126 | * @param boolean $use true to use the cache or false to ignore the cache  | 
            ||
| 127 | *  | 
            ||
| 128 | * @return this.  | 
            ||
| 129 | */  | 
            ||
| 130 | public function useCache($use)  | 
            ||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * Get the index as an array.  | 
            ||
| 141 | *  | 
            ||
| 142 | * @return array as index.  | 
            ||
| 143 | */  | 
            ||
| 144 | public function getIndex()  | 
            ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * Create the index of all content into an array.  | 
            ||
| 153 | *  | 
            ||
| 154 | * @return array as index.  | 
            ||
| 155 | */  | 
            ||
| 156 | View Code Duplication | private function loadIndex()  | 
            |
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Check if a filename is to be marked as an internal route..  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param string $filepath as the basepath (routepart) to the file.  | 
            ||
| 179 | *  | 
            ||
| 180 | * @return boolean true if the route content is internal, else false  | 
            ||
| 181 | */  | 
            ||
| 182 | private function isInternalRoute($filepath)  | 
            ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * Check if filepath should be used as part of toc.  | 
            ||
| 204 | *  | 
            ||
| 205 | * @param string $filepath as the basepath (routepart) to the file.  | 
            ||
| 206 | *  | 
            ||
| 207 | * @return boolean true if the route content shoul dbe in toc, else false  | 
            ||
| 208 | */  | 
            ||
| 209 | private function allowInToc($filepath)  | 
            ||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * Generate an index from the directory structure.  | 
            ||
| 218 | *  | 
            ||
| 219 | * @return array as index for all content files.  | 
            ||
| 220 | */  | 
            ||
| 221 | private function createIndex()  | 
            ||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | /**  | 
            ||
| 264 | * Create the index of all meta content into an array.  | 
            ||
| 265 | *  | 
            ||
| 266 | * @return array as index.  | 
            ||
| 267 | */  | 
            ||
| 268 | View Code Duplication | private function loadMetaIndex()  | 
            |
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Generate an index for meta files.  | 
            ||
| 289 | *  | 
            ||
| 290 | * @return array as table of content.  | 
            ||
| 291 | */  | 
            ||
| 292 | private function createMetaIndex()  | 
            ||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /**  | 
            ||
| 319 | * Get a reference to meta data for specific route.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @param string $route current route used to access page.  | 
            ||
| 322 | *  | 
            ||
| 323 | * @return array as table of content.  | 
            ||
| 324 | */  | 
            ||
| 325 | private function getMetaForRoute($route)  | 
            ||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /**  | 
            ||
| 336 | * Get the frontmatter of a document.  | 
            ||
| 337 | *  | 
            ||
| 338 | * @param string $file to get frontmatter from.  | 
            ||
| 339 | *  | 
            ||
| 340 | * @return array as frontmatter.  | 
            ||
| 341 | */  | 
            ||
| 342 | private function getFrontmatter($file)  | 
            ||
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | /**  | 
            ||
| 356 | * Get the title of a document.  | 
            ||
| 357 | *  | 
            ||
| 358 | * @deprecated in favor of getFrontmatter  | 
            ||
| 359 | *  | 
            ||
| 360 | * @param string $file to get title from.  | 
            ||
| 361 | *  | 
            ||
| 362 | * @return string as the title.  | 
            ||
| 363 | */  | 
            ||
| 364 | private function getTitle($file)  | 
            ||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | /**  | 
            ||
| 373 | * Get the title of a document to use for breadcrumb.  | 
            ||
| 374 | *  | 
            ||
| 375 | * @param string $file to get title from.  | 
            ||
| 376 | *  | 
            ||
| 377 | * @return string as the breadcrumb title.  | 
            ||
| 378 | */  | 
            ||
| 379 | private function getBreadcrumbTitle($file)  | 
            ||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | /**  | 
            ||
| 394 | * Create a table of content for routes at particular level.  | 
            ||
| 395 | *  | 
            ||
| 396 | * @param string $route base route to use.  | 
            ||
| 397 | *  | 
            ||
| 398 | * @return array as the toc.  | 
            ||
| 399 | */  | 
            ||
| 400 | private function createBaseRouteToc($route)  | 
            ||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | /**  | 
            ||
| 425 | * Map the route to the correct key in the index.  | 
            ||
| 426 | *  | 
            ||
| 427 | * @param string $route current route used to access page.  | 
            ||
| 428 | *  | 
            ||
| 429 | * @return string as key or false if no match.  | 
            ||
| 430 | */  | 
            ||
| 431 | private function mapRoute2IndexKey($route)  | 
            ||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * Map the route to the correct entry in the index.  | 
            ||
| 450 | *  | 
            ||
| 451 | * @param string $route current route used to access page.  | 
            ||
| 452 | *  | 
            ||
| 453 | * @return array as the matched route.  | 
            ||
| 454 | */  | 
            ||
| 455 | private function mapRoute2Index($route)  | 
            ||
| 465 | |||
| 466 | |||
| 467 | |||
| 468 | /**  | 
            ||
| 469 | * Get view by mergin information from meta and frontmatter.  | 
            ||
| 470 | *  | 
            ||
| 471 | * @param string $route current route used to access page.  | 
            ||
| 472 | * @param array $frontmatter for the content.  | 
            ||
| 473 | * @param string $key for the view to retrive.  | 
            ||
| 474 | * @param string $distinct how to merge the array.  | 
            ||
| 475 | *  | 
            ||
| 476 | * @return array with data to add as view.  | 
            ||
| 477 | */  | 
            ||
| 478 | private function getView($route, $frontmatter, $key, $distinct = true)  | 
            ||
| 499 | |||
| 500 | |||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * Get details on extra views.  | 
            ||
| 504 | *  | 
            ||
| 505 | * @param string $route current route used to access page.  | 
            ||
| 506 | * @param array $frontmatter for the content.  | 
            ||
| 507 | *  | 
            ||
| 508 | * @return array with page data to send to view.  | 
            ||
| 509 | */  | 
            ||
| 510 | private function getViews($route, $frontmatter)  | 
            ||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | /**  | 
            ||
| 537 | * Load extra info into views based of meta information provided in each  | 
            ||
| 538 | * view.  | 
            ||
| 539 | *  | 
            ||
| 540 | * @param string $view with current settings.  | 
            ||
| 541 | * @param string $route to load view from.  | 
            ||
| 542 | * @param string $baseurl to prepend relative urls.  | 
            ||
| 543 | *  | 
            ||
| 544 | * @return array with view details.  | 
            ||
| 545 | */  | 
            ||
| 546 | private function getAdditionalViewDataForRoute($view, $route, $baseurl)  | 
            ||
| 560 | |||
| 561 | |||
| 562 | |||
| 563 | /**  | 
            ||
| 564 | * Order and limit toc items.  | 
            ||
| 565 | *  | 
            ||
| 566 | * @param string &$toc array with current toc.  | 
            ||
| 567 | * @param string &$meta on how to order and limit toc.  | 
            ||
| 568 | *  | 
            ||
| 569 | * @return void.  | 
            ||
| 570 | */  | 
            ||
| 571 | private function orderAndlimitToc(&$toc, &$meta)  | 
            ||
| 600 | |||
| 601 | |||
| 602 | |||
| 603 | /**  | 
            ||
| 604 | * Find next and previous links of current content.  | 
            ||
| 605 | *  | 
            ||
| 606 | * @param string $routeIndex target route to find next and previous for.  | 
            ||
| 607 | *  | 
            ||
| 608 | * @return array with next and previous if found.  | 
            ||
| 609 | */  | 
            ||
| 610 | private function findNextAndPrevious($routeIndex)  | 
            ||
| 654 | |||
| 655 | |||
| 656 | |||
| 657 | /**  | 
            ||
| 658 | * Load extra info into views based of meta information provided in each  | 
            ||
| 659 | * view.  | 
            ||
| 660 | *  | 
            ||
| 661 | * @param array &$views with all views.  | 
            ||
| 662 | * @param string $route current route  | 
            ||
| 663 | * @param string $routeIndex route with appended /index  | 
            ||
| 664 | *  | 
            ||
| 665 | * @throws NotFoundException when mapping can not be done.  | 
            ||
| 666 | *  | 
            ||
| 667 | * @return void.  | 
            ||
| 668 | */  | 
            ||
| 669 | private function loadAdditionalContent(&$views, $route, $routeIndex)  | 
            ||
| 714 | |||
| 715 | |||
| 716 | |||
| 717 | /**  | 
            ||
| 718 | * Get basurl from view, if it is defined.  | 
            ||
| 719 | *  | 
            ||
| 720 | * @param array $views data for all views.  | 
            ||
| 721 | * @param string $current for current view if any.  | 
            ||
| 722 | *  | 
            ||
| 723 | * @return string | null as baseurl.  | 
            ||
| 724 | */  | 
            ||
| 725 | private function getBaseurl($views, $current = null)  | 
            ||
| 739 | |||
| 740 | |||
| 741 | |||
| 742 | /**  | 
            ||
| 743 | * Parse text, find and update all a href to use baseurl.  | 
            ||
| 744 | *  | 
            ||
| 745 | * @param object &$filtered with text and excerpt to process.  | 
            ||
| 746 | * @param string $baseurl add as baseurl for all relative urls.  | 
            ||
| 747 | *  | 
            ||
| 748 | * @return void.  | 
            ||
| 749 | */  | 
            ||
| 750 | private function addBaseurl2AnchorUrls(&$filtered, $baseurl)  | 
            ||
| 763 | |||
| 764 | |||
| 765 | |||
| 766 | /**  | 
            ||
| 767 | * Load extra info intro views based of meta information provided in each  | 
            ||
| 768 | * view.  | 
            ||
| 769 | *  | 
            ||
| 770 | * @param string $key array with all views.  | 
            ||
| 771 | * @param string $content array with all views.  | 
            ||
| 772 | *  | 
            ||
| 773 | * @throws NotFoundException when mapping can not be done.  | 
            ||
| 774 | *  | 
            ||
| 775 | * @return void.  | 
            ||
| 776 | */  | 
            ||
| 777 | private function loadFileContent($key, $content)  | 
            ||
| 799 | |||
| 800 | |||
| 801 | |||
| 802 | /**  | 
            ||
| 803 | * Look up the route in the index and use that to retrieve the filtered  | 
            ||
| 804 | * content.  | 
            ||
| 805 | *  | 
            ||
| 806 | * @param string $route to look up.  | 
            ||
| 807 | *  | 
            ||
| 808 | * @return array with content and filtered version.  | 
            ||
| 809 | */  | 
            ||
| 810 | public function mapRoute2Content($route)  | 
            ||
| 818 | |||
| 819 | |||
| 820 | |||
| 821 | /**  | 
            ||
| 822 | * Map url to content if such mapping can be done, exclude internal routes.  | 
            ||
| 823 | *  | 
            ||
| 824 | * @param string $route optional route to look up.  | 
            ||
| 825 | *  | 
            ||
| 826 | * @return object with content and filtered version.  | 
            ||
| 827 | */  | 
            ||
| 828 | public function contentForRoute($route = null)  | 
            ||
| 838 | |||
| 839 | |||
| 840 | |||
| 841 | /**  | 
            ||
| 842 | * Map url to content, even internal content, if such mapping can be done.  | 
            ||
| 843 | *  | 
            ||
| 844 | * @param string $route optional route to look up.  | 
            ||
| 845 | *  | 
            ||
| 846 | * @return object with content and filtered version.  | 
            ||
| 847 | */  | 
            ||
| 848 | public function contentForInternalRoute($route = null)  | 
            ||
| 891 | }  | 
            ||
| 892 | 
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.