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 | abstract class AbstractController |
||
23 | { |
||
24 | /** |
||
25 | * @var BaseApp |
||
26 | */ |
||
27 | protected $app; |
||
28 | |||
29 | /** |
||
30 | * @var Request |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * @var View |
||
36 | */ |
||
37 | protected $view; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $autoRenderView = true; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $autoRenderViewScript; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param BaseApp $app |
||
53 | * @param Request $request |
||
54 | */ |
||
55 | 19 | final public function __construct(BaseApp $app, Request $request) |
|
65 | |||
66 | /** |
||
67 | * Initialization method meant to be overridden in descendant classes (optional). |
||
68 | */ |
||
69 | 19 | protected function init() |
|
72 | |||
73 | /** |
||
74 | * Pre dispatch method meant to be overridden in descendant classes (optional). |
||
75 | * |
||
76 | * This method is called right before the actual action method is called/dispatched. |
||
77 | * Override this instead of init() if access to dispatch properties is required (like |
||
78 | * action name) or you need to return a response. |
||
79 | * |
||
80 | * @param string $action |
||
81 | * @return Response|null |
||
82 | */ |
||
83 | 8 | protected function preDispatch($action) |
|
87 | |||
88 | /** |
||
89 | * @param bool $autoRenderView |
||
90 | */ |
||
91 | 2 | public function setAutoRenderView($autoRenderView) |
|
95 | |||
96 | /** |
||
97 | * @param string $autoRenderViewScript |
||
98 | */ |
||
99 | 1 | public function setAutoRenderViewScript($autoRenderViewScript) |
|
103 | |||
104 | /** |
||
105 | * Dispatch the requested action |
||
106 | * |
||
107 | * @param string|null $action action id/name (lowercase, - word separation) |
||
108 | * @param array $actionArgs |
||
109 | * @return Response |
||
110 | * @throws Routing\Exception\ResourceNotFoundException |
||
111 | */ |
||
112 | 11 | public function dispatch($action = null, array $actionArgs = []) |
|
136 | |||
137 | /** |
||
138 | * @param string $actionMethod |
||
139 | * @param array $actionArgs |
||
140 | * @return array |
||
141 | * @throws Routing\Exception\ResourceNotFoundException |
||
142 | */ |
||
143 | 11 | protected function getCollectedDispatchArgs($actionMethod, array $actionArgs = []) |
|
179 | |||
180 | /** |
||
181 | * @param string $action |
||
182 | * @param mixed $actionResponse |
||
183 | * @return Response |
||
184 | */ |
||
185 | 7 | protected function getDispatchResponse($action, $actionResponse) |
|
206 | |||
207 | /** |
||
208 | * @param string $action |
||
209 | * @param string $controller |
||
210 | * @param string|null $module |
||
211 | * @return string |
||
212 | */ |
||
213 | 1 | public function getAutoRenderViewScriptName($action, $controller, $module = null) |
|
219 | |||
220 | /** |
||
221 | * Post dispatch method meant to be overridden in descendant classes (optional). |
||
222 | * This method is called right after an action method has returned it's response, |
||
223 | * but before the dispatch method returns the response. |
||
224 | */ |
||
225 | 7 | protected function postDispatch() |
|
228 | |||
229 | /** |
||
230 | * Forwards request to another action and/or controller |
||
231 | * |
||
232 | * @param string $action Action name as lowercase separated string |
||
233 | * @param string|null $controller Controller name as lowercase separated string |
||
234 | * @param string|null $module Module name as lowercase separated string |
||
235 | * @param array $actionArgs |
||
236 | * @return Response |
||
237 | */ |
||
238 | 3 | protected function forward($action, $controller = null, $module = null, array $actionArgs = []) |
|
258 | |||
259 | /** |
||
260 | * Get current or a new url merged with provided parameters. |
||
261 | * |
||
262 | * @param string $relativeUrl |
||
263 | * @param array $parameters |
||
264 | * @return string |
||
265 | */ |
||
266 | 2 | protected function getUrl($relativeUrl = null, array $parameters = []) |
|
282 | |||
283 | /** |
||
284 | * Shortcut method to access GET/query parameters. |
||
285 | * |
||
286 | * @param string $key |
||
287 | * @param mixed $default |
||
288 | * @return string|array |
||
289 | */ |
||
290 | 2 | View Code Duplication | protected function get($key = null, $default = null) |
298 | |||
299 | /** |
||
300 | * Shortcut method to access POST/request parameters. |
||
301 | * |
||
302 | * @param string $key |
||
303 | * @param mixed $default |
||
304 | * @return string|array |
||
305 | */ |
||
306 | 1 | View Code Duplication | protected function post($key = null, $default = null) |
314 | } |
||
315 |