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 |
||
| 10 | class View |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Active layout for view |
||
| 14 | * |
||
| 15 | * @var string|bool |
||
| 16 | */ |
||
| 17 | protected $layout; |
||
| 18 | |||
| 19 | private $pathToView; |
||
| 20 | |||
| 21 | private $pathToLayout; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * Rendered view content |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $view = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Data object for view |
||
| 33 | * |
||
| 34 | * @var object |
||
| 35 | */ |
||
| 36 | protected $properties; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * \Zewa\Config reference |
||
| 40 | * |
||
| 41 | * @var Config |
||
| 42 | */ |
||
| 43 | protected $configuration; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * \Zewa\Router reference |
||
| 47 | * |
||
| 48 | * @var Router |
||
| 49 | */ |
||
| 50 | protected $router; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * \Zewa\Router reference |
||
| 54 | * |
||
| 55 | * @var Router |
||
| 56 | */ |
||
| 57 | protected $request; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $queuedJS = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $queuedCSS = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Load up some basic configuration settings. |
||
| 71 | */ |
||
| 72 | public function __construct(Config $config, Router $router, Request $request) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns base URL for app |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | private function baseURL($path = '') |
||
| 90 | |||
| 91 | /* |
||
| 92 | * @todo create method for returning |
||
| 93 | * a valid json string with header.. |
||
| 94 | * view shouldn't set header logic, |
||
| 95 | * and the framework doesn't care what returns the string |
||
| 96 | * ..but view should handle the json_encode... |
||
| 97 | * seems overkill to call header() with returning a $view->json; |
||
| 98 | * thoughts?*/ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Loads a view |
||
| 102 | * |
||
| 103 | * @access public |
||
| 104 | * @param string|bool $view view to load |
||
| 105 | * @param string|bool $layout |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function render($view = false, $layout = false) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * formats and prepares view for inclusion |
||
| 139 | * @param $viewName |
||
| 140 | * @return string |
||
| 141 | * @throws Exception\LookupException |
||
| 142 | */ |
||
| 143 | private function prepareView($viewName) |
||
| 153 | |||
| 154 | public function setView($viewName, $layout = false) |
||
| 161 | |||
| 162 | public function setProperty($property, $value = false) |
||
| 171 | |||
| 172 | public function setLayout($layout) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Processes view/layouts and exposes variables to the view/layout |
||
| 191 | * |
||
| 192 | * @access private |
||
| 193 | * @param string $file file being rendered |
||
| 194 | * @return string processed content |
||
| 195 | */ |
||
| 196 | //@TODO: come back and clean up this and the way the view receives stuff |
||
| 197 | private function process($file) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Helper method for grabbing aggregated css files |
||
| 217 | * |
||
| 218 | * @access protected |
||
| 219 | * @return string css includes |
||
| 220 | */ |
||
| 221 | protected function fetchCSS() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Helper method for grabbing aggregated JS files |
||
| 238 | * |
||
| 239 | * @access protected |
||
| 240 | * @return string JS includes |
||
| 241 | */ |
||
| 242 | protected function fetchJS() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Helper method for adding css files for aggregation/render |
||
| 259 | * |
||
| 260 | * @access public |
||
| 261 | * @param $files array |
||
| 262 | * @param $place string |
||
| 263 | * @return string css includes |
||
| 264 | * @throws Exception\LookupException |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function addCSS($files = [], $place = 'append') |
|
| 274 | |||
| 275 | View Code Duplication | public function addJS($files = [], $place = 'append') |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Set 404 header, and return 404 view contents |
||
| 286 | * |
||
| 287 | * @access public |
||
| 288 | * @param $data array |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function render404($data = []) |
||
| 298 | } |
||
| 299 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: