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 | class Dispatcher |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * The current request object |
||
27 | * |
||
28 | * @var Request |
||
29 | */ |
||
30 | protected $request; |
||
31 | |||
32 | /** |
||
33 | * The configuration object |
||
34 | * |
||
35 | * @var Config |
||
36 | */ |
||
37 | protected $config; |
||
38 | |||
39 | /** |
||
40 | * user, api |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $requestType = 'default'; |
||
45 | |||
46 | /** |
||
47 | * Dispatcher constructor. |
||
48 | * |
||
49 | * @param Request $request |
||
50 | * @param Config $config |
||
51 | */ |
||
52 | public function __construct(Request $request, Config $config) |
||
57 | |||
58 | /** |
||
59 | * Bootstrap for every route call |
||
60 | * |
||
61 | * @return Response|JsonResponse|mixed |
||
62 | * @throws MethodNotFoundException |
||
63 | * @throws ClassNotFoundException |
||
64 | * @throws DispatchFailureException |
||
65 | * @throws IncompatibleResponseException |
||
66 | */ |
||
67 | public function dispatch() |
||
106 | |||
107 | /** |
||
108 | * @return bool|string |
||
109 | */ |
||
110 | private function resolveAssetsPath() |
||
134 | |||
135 | /** |
||
136 | * @param $file |
||
137 | * @return string |
||
138 | * @codeCoverageIgnore |
||
139 | */ |
||
140 | public function sendCssFileHeader($file) |
||
146 | |||
147 | /** |
||
148 | * @param $file |
||
149 | * @return string |
||
150 | * @codeCoverageIgnore |
||
151 | */ |
||
152 | public function sendJsFileHeader($file) |
||
158 | |||
159 | /** |
||
160 | * Get data for specific route path |
||
161 | * |
||
162 | * @param string $path |
||
163 | * |
||
164 | * @return array |
||
165 | * @throws MethodNotFoundException |
||
166 | */ |
||
167 | private function getRoute($path) |
||
187 | |||
188 | /** |
||
189 | * Determines if we have a direct/static route match |
||
190 | * |
||
191 | * @param string $uri The request uri |
||
192 | * @param array $data The result from ClassParser |
||
193 | * |
||
194 | * @return array |
||
195 | * @throws MethodNotFoundException |
||
196 | */ |
||
197 | private function getDirectMatch($uri, array $data) :array |
||
223 | |||
224 | /** |
||
225 | * Determines if we have a variable route match |
||
226 | * |
||
227 | * @param string $uri |
||
228 | * @param array $data |
||
229 | * |
||
230 | * @return array |
||
231 | * @throws MethodNotFoundException |
||
232 | */ |
||
233 | private function getVariableMatch($uri, array $data) :array |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | private function getRestfulAction() |
||
298 | |||
299 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.