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 |
||
25 | class Dispatcher |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * The current request object |
||
30 | * |
||
31 | * @var Request |
||
32 | */ |
||
33 | protected $request; |
||
34 | |||
35 | /** |
||
36 | * The configuration object |
||
37 | * |
||
38 | * @var Config |
||
39 | */ |
||
40 | protected $config; |
||
41 | |||
42 | /** |
||
43 | * user, api |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $requestType = 'default'; |
||
48 | |||
49 | /** |
||
50 | * Dispatcher constructor. |
||
51 | * |
||
52 | * @param Request $request |
||
53 | * @param Config $config |
||
54 | */ |
||
55 | public function __construct(Request $request, Config $config) |
||
60 | |||
61 | /** |
||
62 | * Bootstrap for every route call |
||
63 | * |
||
64 | * @return Response|JsonResponse|mixed |
||
65 | * @throws MethodNotFoundException |
||
66 | * @throws ClassNotFoundException |
||
67 | * @throws DispatchFailureException |
||
68 | * @throws IncompatibleResponseException |
||
69 | */ |
||
70 | public function dispatch() |
||
110 | |||
111 | /** |
||
112 | * @return bool|string |
||
113 | */ |
||
114 | private function resolveAssetsPath() |
||
138 | |||
139 | /** |
||
140 | * @param $file |
||
141 | * @return string |
||
142 | * @codeCoverageIgnore |
||
143 | */ |
||
144 | public function sendCssFileHeader($file) |
||
150 | |||
151 | /** |
||
152 | * @param $file |
||
153 | * @return string |
||
154 | * @codeCoverageIgnore |
||
155 | */ |
||
156 | public function sendJsFileHeader($file) |
||
162 | |||
163 | /** |
||
164 | * Get data for specific route path |
||
165 | * |
||
166 | * @param string $path |
||
167 | * |
||
168 | * @return array |
||
169 | * @throws MethodNotFoundException |
||
170 | */ |
||
171 | private function getRoute($path) |
||
191 | |||
192 | /** |
||
193 | * Determines if we have a direct/static route match |
||
194 | * |
||
195 | * @param string $uri The request uri |
||
196 | * @param array $data The result from ClassParser |
||
197 | * |
||
198 | * @return array |
||
199 | * @throws MethodNotFoundException |
||
200 | */ |
||
201 | private function getDirectMatch($uri, array $data) :array |
||
227 | |||
228 | /** |
||
229 | * Determines if we have a variable route match |
||
230 | * |
||
231 | * @param string $uri |
||
232 | * @param array $data |
||
233 | * |
||
234 | * @return array |
||
235 | * @throws MethodNotFoundException |
||
236 | */ |
||
237 | private function getVariableMatch($uri, array $data) :array |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | private function getRestfulAction() |
||
302 | |||
303 | } |
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.