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 | * @var array |
||
34 | */ |
||
35 | private $viewArray = []; |
||
36 | |||
37 | /** |
||
38 | * @var Request |
||
39 | */ |
||
40 | protected $request; |
||
41 | |||
42 | /** |
||
43 | * AbstractController constructor. |
||
44 | * @param Request $request |
||
45 | */ |
||
46 | public function __construct(Request $request) |
||
50 | |||
51 | /** |
||
52 | * Returns the service locator |
||
53 | * |
||
54 | * @return ServiceLocator |
||
55 | */ |
||
56 | public function getServiceLocator() :ServiceLocator |
||
60 | |||
61 | /** |
||
62 | * Returns the session manager |
||
63 | * |
||
64 | * @return SessionManagerService|ServiceInterface |
||
65 | */ |
||
66 | public function getSessionManager() :SessionManagerService |
||
70 | |||
71 | /** |
||
72 | * Returns the view controller |
||
73 | * |
||
74 | * @return ViewController |
||
75 | */ |
||
76 | public function getView() :ViewController |
||
89 | |||
90 | /** |
||
91 | * Returns the orm/entity manager |
||
92 | * |
||
93 | * @return DbService|ServiceInterface |
||
94 | */ |
||
95 | public function getDb() :DbService |
||
99 | |||
100 | /** |
||
101 | * Render view with given template |
||
102 | * |
||
103 | * @param string $template |
||
104 | * @param array $variables |
||
105 | * @return Response |
||
106 | */ |
||
107 | public function render(string $template = '', $variables = []) :Response |
||
111 | |||
112 | /** |
||
113 | * @param array $roles |
||
114 | * |
||
115 | * @return bool|null |
||
116 | */ |
||
117 | public function isPermitted($roles = []) |
||
123 | |||
124 | /** |
||
125 | * @param string $uri |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function redirect(string $uri) :bool |
||
134 | |||
135 | /** |
||
136 | * @param string $key |
||
137 | * @param string $message |
||
138 | */ |
||
139 | public function setFlashMessage(string $key, string $message) |
||
144 | |||
145 | /** |
||
146 | * @param $key |
||
147 | * @return string|null |
||
148 | */ |
||
149 | public function getFlashMessage(string $key) |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @param array $parameters |
||
158 | * @param bool $absolute |
||
159 | * |
||
160 | * @return string |
||
161 | * @throws RouteInvalidException |
||
162 | */ |
||
163 | public function route(string $name, array $parameters = [], $absolute = false) |
||
192 | |||
193 | /** |
||
194 | * @return Request |
||
195 | */ |
||
196 | public function getRequest() :Request |
||
200 | |||
201 | /** |
||
202 | * Magic method for providing a view helper |
||
203 | * |
||
204 | * @param string $name The class name |
||
205 | * @param array $arguments Arguments if given |
||
206 | * @return AbstractPlugin |
||
207 | * @throws PluginException |
||
208 | * @codeCoverageIgnore Not implemented yet |
||
209 | */ |
||
210 | View Code Duplication | public function __call($name, $arguments) |
|
243 | |||
244 | } |
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.