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 |
||
28 | abstract class AbstractController |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Holds the views per controller request |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $_viewArray = []; |
||
37 | |||
38 | /** |
||
39 | * Holds the request |
||
40 | * |
||
41 | * @var Request |
||
42 | */ |
||
43 | protected $request; |
||
44 | |||
45 | /** |
||
46 | * AbstractController constructor. |
||
47 | * |
||
48 | * @param Request $request The request object |
||
49 | */ |
||
50 | public function __construct(Request $request) |
||
54 | |||
55 | /** |
||
56 | * Returns the service locator |
||
57 | * |
||
58 | * @return ServiceLocator |
||
59 | */ |
||
60 | public function getServiceLocator() :ServiceLocator |
||
64 | |||
65 | /** |
||
66 | * Returns the session manager |
||
67 | * |
||
68 | * @return SessionManagerService|ServiceInterface |
||
69 | */ |
||
70 | public function getSessionManager() :SessionManagerService |
||
74 | |||
75 | /** |
||
76 | * Returns the view controller |
||
77 | * |
||
78 | * @return ViewController |
||
79 | */ |
||
80 | public function getView() :ViewController |
||
95 | |||
96 | /** |
||
97 | * Returns the orm/entity manager |
||
98 | * |
||
99 | * @return DbService|ServiceInterface |
||
100 | */ |
||
101 | public function getDb() :DbService |
||
105 | |||
106 | /** |
||
107 | * Render view with given template |
||
108 | * |
||
109 | * @param string $template The template to be rendered |
||
110 | * @param array $variables The variables for the template |
||
111 | * |
||
112 | * @return Response |
||
113 | */ |
||
114 | public function render(string $template = '', $variables = []) :Response |
||
123 | |||
124 | /** |
||
125 | * Check if user is permitted based on his role(s) |
||
126 | * |
||
127 | * @param array $roles The corresponding user roles |
||
128 | * |
||
129 | * @return bool|null |
||
130 | */ |
||
131 | public function isPermitted($roles = []) |
||
138 | |||
139 | /** |
||
140 | * Redirect to specific uri |
||
141 | * |
||
142 | * @param string $uri The target uri |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function redirect(string $uri) :bool |
||
152 | |||
153 | /** |
||
154 | * Set a universal text token which is valid for exactly one request/call |
||
155 | * |
||
156 | * @param string $key Key for the flash message |
||
157 | * @param string $message Content for the flash message |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | public function setFlashMessage(string $key, string $message) |
||
166 | |||
167 | /** |
||
168 | * Retrieve a flash message |
||
169 | * |
||
170 | * @param string $key The flash message key |
||
171 | * |
||
172 | * @return string|null |
||
173 | */ |
||
174 | public function getFlashMessage(string $key) |
||
179 | |||
180 | /** |
||
181 | * Get the url for a specific route name |
||
182 | * |
||
183 | * @param string $name Name of the route |
||
184 | * @param array $parameters Apply parameters where necessary |
||
185 | * @param bool $absolute Return an absolute url with host as prefix |
||
186 | * |
||
187 | * @return string |
||
188 | * @throws RouteInvalidException |
||
189 | */ |
||
190 | public function route(string $name, array $parameters = [], $absolute = false) |
||
225 | |||
226 | /** |
||
227 | * Return the current request object |
||
228 | * |
||
229 | * @return Request |
||
230 | */ |
||
231 | public function getRequest() :Request |
||
235 | |||
236 | /** |
||
237 | * Magic method for providing a view helper |
||
238 | * |
||
239 | * @param string $name The class name |
||
240 | * @param array $arguments Arguments if given |
||
241 | * |
||
242 | * @return AbstractPlugin |
||
243 | * @throws PluginException |
||
244 | * |
||
245 | * @codeCoverageIgnore Not implemented yet |
||
246 | */ |
||
247 | View Code Duplication | public function __call($name, $arguments) |
|
280 | |||
281 | } |
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.