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 |
||
| 11 | class View |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Active layout for view |
||
| 15 | * |
||
| 16 | * @var string|bool |
||
| 17 | */ |
||
| 18 | protected $layout; |
||
| 19 | |||
| 20 | private $pathToView; |
||
| 21 | |||
| 22 | private $pathToLayout; |
||
| 23 | |||
| 24 | /** @var array */ |
||
| 25 | protected $viewQueue; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Rendered view content |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $view = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Data object for view |
||
| 36 | * |
||
| 37 | * @var object |
||
| 38 | */ |
||
| 39 | protected $properties; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * \Zewa\Config reference |
||
| 43 | * |
||
| 44 | * @var Config |
||
| 45 | */ |
||
| 46 | protected $configuration; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * \Zewa\Router reference |
||
| 50 | * |
||
| 51 | * @var Router |
||
| 52 | */ |
||
| 53 | protected $router; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * \Zewa\Router reference |
||
| 57 | * |
||
| 58 | * @var Router |
||
| 59 | */ |
||
| 60 | protected $request; |
||
| 61 | |||
| 62 | /** @var Container */ |
||
| 63 | protected $container; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $queuedJS = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $queuedCSS = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Load up some basic configuration settings. |
||
| 77 | */ |
||
| 78 | 11 | public function __construct(Config $config, Router $router, Request $request, Container $container) |
|
| 88 | |||
| 89 | /* |
||
| 90 | * @todo create method for returning |
||
| 91 | * a valid json string with header.. |
||
| 92 | * view shouldn't set header logic, |
||
| 93 | * and the framework doesn't care what returns the string |
||
| 94 | * ..but view should handle the json_encode... |
||
| 95 | * seems overkill to call header() with returning a $view->json; |
||
| 96 | * thoughts?*/ |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Loads a view |
||
| 100 | * |
||
| 101 | * @access public |
||
| 102 | * @param string|bool $view view to load |
||
| 103 | * @param string|bool $layout |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 6 | public function render($view = false, $layout = false) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * formats and prepares view for inclusion |
||
| 121 | * @param $viewName |
||
| 122 | * @return string |
||
| 123 | * @throws Exception\LookupException |
||
| 124 | */ |
||
| 125 | 5 | public function setView($viewName) |
|
| 135 | |||
| 136 | 1 | public function getView($view = null) |
|
| 144 | |||
| 145 | 2 | public function setProperty(string $key, $value) |
|
| 151 | |||
| 152 | 1 | public function getProperty(string $key = null, $default = null) |
|
| 162 | |||
| 163 | 1 | public function unsetProperty(string $key) |
|
| 171 | |||
| 172 | 5 | public function setLayout($layout = null) |
|
| 189 | |||
| 190 | 1 | public function getLayout() |
|
| 194 | |||
| 195 | 4 | private function renderViews() : string |
|
| 206 | |||
| 207 | 4 | private function bufferResponse() : string |
|
| 217 | |||
| 218 | 4 | private function buffer(string $path) : string |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Helper method for grabbing aggregated css files |
||
| 238 | * |
||
| 239 | * @access protected |
||
| 240 | * @return string css includes |
||
| 241 | */ |
||
| 242 | 2 | public function fetchCSS() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Helper method for grabbing aggregated JS files |
||
| 259 | * |
||
| 260 | * @access protected |
||
| 261 | * @return string JS includes |
||
| 262 | */ |
||
| 263 | 2 | public function fetchJS() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Helper method for adding css files for aggregation/render |
||
| 280 | * |
||
| 281 | * @access public |
||
| 282 | * @param $files array |
||
| 283 | * @param $place string |
||
| 284 | */ |
||
| 285 | 2 | View Code Duplication | public function addCSS($files = [], $place = 'append') |
| 293 | |||
| 294 | 2 | View Code Duplication | public function addJS($files = [], $place = 'append') |
| 302 | |||
| 303 | /** |
||
| 304 | * Set 404 header, and return 404 view contents |
||
| 305 | * |
||
| 306 | * @access public |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 1 | public function render404() |
|
| 315 | } |
||
| 316 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..