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:
Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Controller |
||
15 | { |
||
16 | /** Response */ |
||
17 | protected $_response; |
||
18 | /** Webroot */ |
||
19 | protected $_webroot; |
||
20 | /** Theme name */ |
||
21 | protected $_themeName = null; |
||
22 | /** Request URI */ |
||
23 | protected $_pathInfo = null; |
||
24 | /** Title (body title) */ |
||
25 | protected $_title = null; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | */ |
||
30 | public function __construct() |
||
39 | |||
40 | /** |
||
41 | * Before action hook |
||
42 | * Anything here gets called immediately BEFORE the Action method runs. |
||
43 | */ |
||
44 | public function _before() |
||
49 | |||
50 | /** |
||
51 | * After action hook |
||
52 | * Anything here gets called immediately AFTER the Action method runs. |
||
53 | */ |
||
54 | public function _after() |
||
58 | |||
59 | /** |
||
60 | * Get action |
||
61 | * |
||
62 | * @param mixed $var |
||
63 | * @return mixed |
||
64 | */ |
||
65 | public function get($var = null) |
||
75 | |||
76 | /** |
||
77 | * Put action |
||
78 | * |
||
79 | * @param mixed $var |
||
80 | * @return mixed |
||
81 | */ |
||
82 | public function put($var = null) |
||
92 | |||
93 | /** |
||
94 | * Post action |
||
95 | * |
||
96 | * @param mixed $var |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function post($var = null) |
||
109 | |||
110 | /** |
||
111 | * Delete action |
||
112 | * |
||
113 | * @param mixed $var |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function delete($var = null) |
||
126 | |||
127 | /** |
||
128 | * Set Path Info (Requested URI) |
||
129 | */ |
||
130 | public function setPathInfo($pathInfo) |
||
134 | |||
135 | /** |
||
136 | * Prepare theme |
||
137 | */ |
||
138 | public function prepareTheme() |
||
142 | |||
143 | /** |
||
144 | * Set the theme name in the response object |
||
145 | * |
||
146 | * @param string $name, the name/id of the theme |
||
147 | */ |
||
148 | public function setThemeName($name) |
||
152 | |||
153 | /** |
||
154 | * Set the theme name in both the response and the theme objects |
||
155 | * |
||
156 | * @param string $name, the name/id of the theme |
||
157 | */ |
||
158 | public function setTheme($name) |
||
163 | |||
164 | /** |
||
165 | * Get the theme name |
||
166 | * |
||
167 | * @return string $name |
||
168 | */ |
||
169 | public function getThemeName() |
||
173 | |||
174 | /** |
||
175 | * Set the theme template used to render the page |
||
176 | * @param string $template |
||
177 | */ |
||
178 | public function setThemeTemplate($template) |
||
182 | |||
183 | /** |
||
184 | * Get Response object |
||
185 | * |
||
186 | * @return ResponseObject |
||
187 | */ |
||
188 | public function getResponse() |
||
192 | |||
193 | /** |
||
194 | * Send |
||
195 | */ |
||
196 | final public function send() |
||
200 | |||
201 | /** |
||
202 | * Set Response key/value data |
||
203 | * formerly called, setResponseDataValue() |
||
204 | * |
||
205 | * @param string $key |
||
206 | * @param mixed $value |
||
207 | */ |
||
208 | public function setResponseKeyValue($key, $value) |
||
212 | |||
213 | /** |
||
214 | * Get Response data value |
||
215 | * |
||
216 | * @param string $key |
||
217 | */ |
||
218 | public function getResponseKeyValue($key) |
||
222 | |||
223 | /** |
||
224 | * Add page title text to current page |
||
225 | * |
||
226 | * @param string $title |
||
227 | */ |
||
228 | public function setPageTitle($title) |
||
232 | |||
233 | /** |
||
234 | * Set page content title to be themed in the view |
||
235 | * |
||
236 | * @param string $title |
||
237 | */ |
||
238 | public function setBodyTitle($title) |
||
243 | |||
244 | /** |
||
245 | * Get page content title to be themed in a layout or view |
||
246 | * |
||
247 | * @param string $title |
||
248 | */ |
||
249 | public function getBodyTitle() |
||
253 | |||
254 | /** |
||
255 | * Set both the title (header) and page title (body) at the same time |
||
256 | * @param string $title |
||
257 | */ |
||
258 | public function setTitle($title) |
||
263 | |||
264 | /** |
||
265 | * Set both the title (header) and page title (body) at the same time |
||
266 | * @param string $title |
||
267 | */ |
||
268 | public function getTitle() |
||
272 | |||
273 | /** |
||
274 | * Set the response content |
||
275 | * |
||
276 | * @param string $content |
||
277 | */ |
||
278 | public function setContent($content) |
||
282 | |||
283 | /** |
||
284 | * Add/append html text to the response content |
||
285 | * |
||
286 | * @param string @content |
||
287 | */ |
||
288 | public function appendContent($content) |
||
292 | |||
293 | /** |
||
294 | * Autoaction |
||
295 | * |
||
296 | * @param string $var |
||
297 | * @param string $httpMethod |
||
298 | */ |
||
299 | protected function _autoaction($var, $httpMethod = 'get') |
||
310 | |||
311 | /** |
||
312 | * Call back for preg_replace in urlToActionName |
||
313 | */ |
||
314 | private function _replaceActionName($parts) |
||
318 | |||
319 | /** |
||
320 | * Modify the action name coming from the URL into proper action name |
||
321 | * @param string $name: The raw controller action name |
||
322 | * @return string |
||
323 | */ |
||
324 | public function urlToActionName($name, $httpMethod) |
||
331 | |||
332 | /** |
||
333 | * Load a view with the given data |
||
334 | * |
||
335 | * @param string $viewName |
||
336 | * @param array $data |
||
337 | * @return string $html, view contents |
||
338 | */ |
||
339 | View Code Duplication | public function getView($viewName, $data = null, $templateRootFolder = null) |
|
349 | |||
350 | /** |
||
351 | * Add a view from the current theme with the given data |
||
352 | * |
||
353 | * @param mixed $view, can be a string (view name) or object (view object) |
||
354 | * @param array $data |
||
355 | * @return string $html, view contents |
||
356 | */ |
||
357 | public function addView($view, $data = null) |
||
366 | |||
367 | /** |
||
368 | * Load a layout with the given data |
||
369 | * |
||
370 | * @param string $layoutName |
||
371 | * @param array $data |
||
372 | * @return string $html, layout contents |
||
373 | */ |
||
374 | public function getLayout($layoutName, $data = null, $templateRootFolder = null) |
||
386 | |||
387 | /** |
||
388 | * Redirect to another url |
||
389 | * @param string $url |
||
390 | */ |
||
391 | public function redirect($url) |
||
396 | |||
397 | /** |
||
398 | * Add Meta tags to the page |
||
399 | * |
||
400 | * @param string $name, html meta name (e.g. 'description' or 'keywords') |
||
401 | * @param string $content |
||
402 | */ |
||
403 | public function addMeta($name, $content) |
||
407 | |||
408 | /** |
||
409 | * Set multiple meta fields at once |
||
410 | * @param array $meta |
||
411 | */ |
||
412 | public function setMeta($meta) |
||
417 | |||
418 | /** |
||
419 | * Add Css includes to the page |
||
420 | * |
||
421 | * @param string $name |
||
422 | * @param string $file |
||
423 | * @param int $order |
||
424 | * @param int $active |
||
425 | */ |
||
426 | public function addCss($name, $file, $order = 10, $active = 1) |
||
432 | |||
433 | /** |
||
434 | * Add Css includes to the page |
||
435 | * |
||
436 | * @param string $name |
||
437 | * @param string $file |
||
438 | * @param int $order |
||
439 | * @param int $active |
||
440 | */ |
||
441 | public function addJs($name, $file, $order = 10, $active = 1) |
||
447 | |||
448 | /** |
||
449 | * Magic method experiments |
||
450 | */ |
||
451 | |||
452 | /** |
||
453 | * Get a value |
||
454 | */ |
||
455 | public function __get($key) |
||
459 | |||
460 | /** |
||
461 | * Set a value |
||
462 | */ |
||
463 | public function __set($key, $value) |
||
467 | |||
468 | |||
469 | /** |
||
470 | * |
||
471 | * |
||
472 | * Code below is deprecated, do not use |
||
473 | * @todo Should be deleted, refactored or moved! |
||
474 | * |
||
475 | * |
||
476 | */ |
||
477 | |||
478 | |||
479 | /** |
||
480 | * Add phpToJs variable to be set on the current page |
||
481 | * |
||
482 | * @param mixed $key |
||
483 | * @param mixed $value |
||
484 | */ |
||
485 | public function addPhpToJs($key, $value) |
||
501 | } |
||
502 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.