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 |
||
22 | class ViewController |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var Config |
||
27 | */ |
||
28 | private $config; |
||
29 | |||
30 | /** |
||
31 | * Holds the view variables |
||
32 | * @var array |
||
33 | */ |
||
34 | private $variable = []; |
||
35 | |||
36 | /** |
||
37 | * Holds the view template |
||
38 | * @var string |
||
39 | */ |
||
40 | private $template = ''; |
||
41 | |||
42 | /**The template path without filename |
||
43 | * @var string |
||
44 | */ |
||
45 | private $templatePath = ''; |
||
46 | |||
47 | /** |
||
48 | * Holds the registered view helpers |
||
49 | * @var array |
||
50 | */ |
||
51 | //private $viewHelpers = []; |
||
52 | |||
53 | /** |
||
54 | * Holds the parent template |
||
55 | * @var ViewController |
||
56 | */ |
||
57 | private $parentTemplate = null; |
||
58 | |||
59 | /** |
||
60 | * ViewController constructor. |
||
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 | * @throws Exception |
||
255 | * @throws TemplateException |
||
256 | */ |
||
257 | public function render() |
||
287 | |||
288 | /** |
||
289 | * Magic method for providing a view helpers |
||
290 | * |
||
291 | * @param string $name The class name |
||
292 | * @param array $arguments Arguments if given |
||
293 | * |
||
294 | * @return AbstractViewHelper |
||
295 | * |
||
296 | * @throws ViewHelperException |
||
297 | */ |
||
298 | public function __call($name, $arguments) |
||
333 | |||
334 | /** |
||
335 | * Abstraction of call_user_func_array |
||
336 | * |
||
337 | * @param $class |
||
338 | * @param $arguments |
||
339 | * |
||
340 | * @return mixed |
||
341 | */ |
||
342 | private function _callUserFuncArray($class, $arguments) |
||
346 | |||
347 | /** |
||
348 | * Destructor |
||
349 | */ |
||
350 | public function __destruct() |
||
355 | |||
356 | } |
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..