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:
| 1 | <?php |
||
| 7 | final class Controller |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Controller constructor. |
||
| 11 | * |
||
| 12 | * Set context and fetch post id |
||
| 13 | */ |
||
| 14 | function __construct() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * make singleton |
||
| 21 | * |
||
| 22 | * @return null|Controller |
||
| 23 | */ |
||
| 24 | public static function Instance() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set all pages from within the functions.php->setControllers() |
||
| 36 | * |
||
| 37 | * @param $pages |
||
| 38 | */ |
||
| 39 | public function setPages($pages) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set context for the page |
||
| 46 | */ |
||
| 47 | private function setContext() |
||
| 53 | |||
| 54 | private function returnId($key) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Find the correct controller for the single post object |
||
| 65 | * |
||
| 66 | * If none controller is found, fallback on teh default post |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function single() |
|
| 85 | |||
| 86 | public function fourOFour() |
||
| 90 | |||
| 91 | View Code Duplication | public function archive() |
|
| 108 | |||
| 109 | public function page() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Fallback when no controller found or controller file isn't present |
||
| 142 | */ |
||
| 143 | private function returnDefault() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return name of the body class |
||
| 152 | * |
||
| 153 | * @return bool|string |
||
| 154 | */ |
||
| 155 | public function getClass() |
||
| 163 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.