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() |
||
102 | |||
103 | /** |
||
104 | * @return bool|string |
||
105 | */ |
||
106 | private function resolveAssetsPath() |
||
130 | |||
131 | /** |
||
132 | * @param $file |
||
133 | * @return string |
||
134 | * @codeCoverageIgnore |
||
135 | */ |
||
136 | public function sendCssFileHeader($file) |
||
142 | |||
143 | /** |
||
144 | * @param $file |
||
145 | * @return string |
||
146 | * @codeCoverageIgnore |
||
147 | */ |
||
148 | public function sendJsFileHeader($file) |
||
154 | |||
155 | /** |
||
156 | * Get data for specific route path |
||
157 | * |
||
158 | * @param string $path |
||
159 | * |
||
160 | * @return array |
||
161 | * @throws MethodNotFoundException |
||
162 | */ |
||
163 | private function getRoute($path) |
||
183 | |||
184 | /** |
||
185 | * Determines if we have a direct/static route match |
||
186 | * |
||
187 | * @param string $uri The request uri |
||
188 | * @param array $data The result from ClassParser |
||
189 | * |
||
190 | * @return array |
||
191 | * @throws MethodNotFoundException |
||
192 | */ |
||
193 | private function getDirectMatch($uri, array $data) :array |
||
219 | |||
220 | /** |
||
221 | * Determines if we have a variable route match |
||
222 | * |
||
223 | * @param string $uri |
||
224 | * @param array $data |
||
225 | * |
||
226 | * @return array |
||
227 | * @throws MethodNotFoundException |
||
228 | */ |
||
229 | private function getVariableMatch($uri, array $data) :array |
||
264 | |||
265 | /** |
||
266 | * @return string |
||
267 | */ |
||
268 | private function getRestfulAction() |
||
291 | |||
292 | } |
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.