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 |
||
9 | class View |
||
10 | { |
||
11 | /** |
||
12 | * System configuration |
||
13 | * |
||
14 | * @var object |
||
15 | */ |
||
16 | protected $configuration; |
||
17 | |||
18 | /** |
||
19 | * Instantiated request class pointer |
||
20 | * |
||
21 | * @var object |
||
22 | */ |
||
23 | protected $request; |
||
24 | |||
25 | /** |
||
26 | * Active layout for view |
||
27 | * |
||
28 | * @var string|bool |
||
29 | */ |
||
30 | protected $layout = false; |
||
31 | |||
32 | /** |
||
33 | * Active module for view |
||
34 | * |
||
35 | * @var string|bool |
||
36 | */ |
||
37 | protected $module = false; |
||
38 | |||
39 | /** |
||
40 | * Rendered view content |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $view = false; |
||
45 | |||
46 | /** |
||
47 | * Data object for view |
||
48 | * |
||
49 | * @var object |
||
50 | */ |
||
51 | protected $properties; |
||
52 | |||
53 | /** |
||
54 | * Router object for view injection |
||
55 | * |
||
56 | * @var object |
||
57 | */ |
||
58 | protected $router; |
||
59 | |||
60 | /** |
||
61 | * Load up some basic configuration settings. |
||
62 | */ |
||
63 | View Code Duplication | public function __construct() |
|
71 | |||
72 | private function baseURL($path = '') |
||
76 | |||
77 | private function currentURL($params = false) |
||
81 | |||
82 | |||
83 | private function currentURI() |
||
87 | /** |
||
88 | * Loads a view |
||
89 | * |
||
90 | * @access public |
||
91 | * |
||
92 | * @param string $requestedView relative path for the view |
||
93 | * @param string $renderName array of data to expose to view |
||
94 | * |
||
95 | * @throws \Exception when a view can not be found |
||
96 | */ |
||
97 | public function render() |
||
112 | |||
113 | public function setView($requestedView) |
||
136 | |||
137 | public function setProperty($property, $value = false) |
||
146 | |||
147 | public function setLayout($layout) |
||
164 | |||
165 | public function setModule($module = false) |
||
173 | /** |
||
174 | * Processes view/layouts and exposes variables to the view/layout |
||
175 | * |
||
176 | * @access private |
||
177 | * |
||
178 | * @param string $file file being rendered |
||
179 | * |
||
180 | * @return string processed content |
||
181 | */ |
||
182 | //@TODO: come back and clean up this and the way the view receives stuff |
||
183 | private function process($file) |
||
200 | |||
201 | // private function verifyResource($resource) { |
||
202 | // |
||
203 | // $path = PUBLIC_PATH . DIRECTORY_SEPARATOR . $resource; |
||
204 | // |
||
205 | // if (!file_exists($path)) { |
||
206 | // return false; |
||
207 | // } |
||
208 | // |
||
209 | // return true; |
||
210 | // } |
||
211 | |||
212 | View Code Duplication | protected function fetchCSS() |
|
229 | |||
230 | View Code Duplication | protected function fetchJS() |
|
247 | |||
248 | View Code Duplication | public function addCSS($sheets = [], $place = 'append') |
|
281 | |||
282 | View Code Duplication | public function addJS($scripts = [], $place = 'append') |
|
315 | |||
316 | |||
317 | /** |
||
318 | * Set 401 header, and return noaccess view contents |
||
319 | * |
||
320 | * @access public |
||
321 | * @return string |
||
322 | */ |
||
323 | public function renderNoAccess($data) |
||
330 | |||
331 | /** |
||
332 | * Set 404 header, and return 404 view contents |
||
333 | * |
||
334 | * @access public |
||
335 | * @param $module string |
||
336 | * @param $data array |
||
337 | * @return string |
||
338 | */ |
||
339 | public function render404($data = []) |
||
346 | } |
||
347 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.