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 |
||
20 | class ViewController |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var Config |
||
25 | */ |
||
26 | private $config; |
||
27 | |||
28 | /** |
||
29 | * Holds the view variables |
||
30 | * @var array |
||
31 | */ |
||
32 | private $variable = []; |
||
33 | |||
34 | /** |
||
35 | * Holds the view template |
||
36 | * @var string |
||
37 | */ |
||
38 | private $template = ''; |
||
39 | |||
40 | /**The template path without filename |
||
41 | * @var string |
||
42 | */ |
||
43 | private $templatePath = ''; |
||
44 | |||
45 | /** |
||
46 | * Holds the registered view helpers |
||
47 | * @var array |
||
48 | */ |
||
49 | //private $viewHelpers = []; |
||
|
|||
50 | |||
51 | /** |
||
52 | * Holds the parent template |
||
53 | * @var ViewController |
||
54 | */ |
||
55 | private $parentTemplate = null; |
||
56 | |||
57 | /** |
||
58 | * ViewController constructor. |
||
59 | * |
||
60 | * @throws ServiceNotFoundException |
||
61 | */ |
||
62 | public function __construct() |
||
66 | |||
67 | /** |
||
68 | * Set template for this view |
||
69 | * |
||
70 | * @param string $template |
||
71 | * @return self |
||
72 | * |
||
73 | * @throws FileNotFoundException |
||
74 | */ |
||
75 | public function setTemplate(string $template = '') |
||
91 | |||
92 | /** |
||
93 | * Set the template path |
||
94 | * |
||
95 | * @param string $path |
||
96 | * @return self |
||
97 | */ |
||
98 | public function setTemplatePath(string $path = '') |
||
103 | |||
104 | /** |
||
105 | * Get the template path |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getTemplatePath() |
||
113 | |||
114 | /** |
||
115 | * Add javascript from outside |
||
116 | * |
||
117 | * @param string $file |
||
118 | * @return self |
||
119 | */ |
||
120 | public function addScript(string $file) |
||
125 | |||
126 | /** |
||
127 | * Add stylesheet from outside |
||
128 | * |
||
129 | * @param string $file |
||
130 | * @return self |
||
131 | */ |
||
132 | public function addStylesheet(string $file) |
||
137 | |||
138 | /** |
||
139 | * Return current template |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getTemplate() :string |
||
147 | |||
148 | /** |
||
149 | * Set a single variable |
||
150 | * |
||
151 | * @param string $key |
||
152 | * @param string|array $value |
||
153 | */ |
||
154 | public function setVariable(string $key = '', $value = '') |
||
158 | |||
159 | /** |
||
160 | * Get a single variable |
||
161 | * |
||
162 | * @param string $key |
||
163 | * @return string|array |
||
164 | */ |
||
165 | public function getVariable(string $key) |
||
173 | |||
174 | /** |
||
175 | * Check if variable exists |
||
176 | * |
||
177 | * @param string $key |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function hasVariable(string $key) :bool |
||
188 | |||
189 | /** |
||
190 | * Set many variables at once |
||
191 | * |
||
192 | * @param array $variables |
||
193 | * @return self |
||
194 | */ |
||
195 | public function setVariables(array $variables = []) |
||
203 | |||
204 | /** |
||
205 | * Get all variables |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function getVariables() :array |
||
213 | |||
214 | /** |
||
215 | * Define parent template |
||
216 | * |
||
217 | * @param ViewController $view |
||
218 | */ |
||
219 | public function setParentTemplate(ViewController $view) |
||
223 | |||
224 | /** |
||
225 | * Get parent template |
||
226 | * |
||
227 | * @return ViewController |
||
228 | */ |
||
229 | public function getParentTemplate() |
||
233 | |||
234 | /** |
||
235 | * Strip spaces and tabs from output |
||
236 | * |
||
237 | * @param $output |
||
238 | * @return string |
||
239 | */ |
||
240 | private function _cleanOutput($output) :string |
||
248 | |||
249 | /** |
||
250 | * Render the current view |
||
251 | * |
||
252 | * @return string |
||
253 | * @throws ServiceNotFoundException |
||
254 | */ |
||
255 | public function render() |
||
279 | |||
280 | /** |
||
281 | * Magic method for providing a view helpers |
||
282 | * |
||
283 | * @param string $name The class name |
||
284 | * @param array $arguments Arguments if given |
||
285 | * |
||
286 | * @return AbstractViewHelper |
||
287 | * |
||
288 | * @throws ViewHelperException |
||
289 | * @throws ServiceNotFoundException |
||
290 | */ |
||
291 | public function __call($name, $arguments) |
||
338 | |||
339 | /** |
||
340 | * Abstraction of call_user_func_array |
||
341 | * |
||
342 | * @param $class |
||
343 | * @param $arguments |
||
344 | * |
||
345 | * @return mixed |
||
346 | */ |
||
347 | private function _callUserFuncArray($class, $arguments) |
||
351 | |||
352 | /** |
||
353 | * Destructor |
||
354 | */ |
||
355 | public function __destruct() |
||
360 | |||
361 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.