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:
Complex classes like DataCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DataCollection |
||
12 | { |
||
13 | /** |
||
14 | * @var \Browscap\Data\Platform[] |
||
15 | */ |
||
16 | private $platforms = []; |
||
17 | |||
18 | /** |
||
19 | * @var \Browscap\Data\Engine[] |
||
20 | */ |
||
21 | private $engines = []; |
||
22 | |||
23 | /** |
||
24 | * @var \Browscap\Data\Device[] |
||
25 | */ |
||
26 | private $devices = []; |
||
27 | |||
28 | /** |
||
29 | * @var \Browscap\Data\Division[] |
||
30 | */ |
||
31 | private $divisions = []; |
||
32 | |||
33 | /** |
||
34 | * @var \Browscap\Data\Division |
||
35 | */ |
||
36 | private $defaultProperties; |
||
37 | |||
38 | /** |
||
39 | * @var \Browscap\Data\Division |
||
40 | */ |
||
41 | private $defaultBrowser; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $divisionsHaveBeenSorted = false; |
||
47 | |||
48 | /** |
||
49 | * @var \DateTimeImmutable |
||
50 | */ |
||
51 | private $generationDate; |
||
52 | |||
53 | /** |
||
54 | * @var \Psr\Log\LoggerInterface |
||
55 | */ |
||
56 | private $logger; |
||
57 | |||
58 | /** |
||
59 | * @var string[] |
||
60 | */ |
||
61 | private $allDivisions = []; |
||
62 | |||
63 | /** |
||
64 | * @var DivisionFactory |
||
65 | */ |
||
66 | private $divisionFactory; |
||
67 | |||
68 | /** |
||
69 | * Create a new data collection for the specified version |
||
70 | * |
||
71 | * @param string $version |
||
|
|||
72 | * @param \Psr\Log\LoggerInterface $logger |
||
73 | */ |
||
74 | public function __construct(LoggerInterface $logger) |
||
80 | |||
81 | /** |
||
82 | * Load a platforms.json file and parse it into the platforms data array |
||
83 | * |
||
84 | * @param string $filename Name of the file |
||
85 | 70 | * |
|
86 | * @throws \RuntimeException if the file does not exist or has invalid JSON |
||
87 | 70 | * @throws \UnexpectedValueException |
|
88 | 70 | * |
|
89 | 70 | * @return void |
|
90 | */ |
||
91 | public function addPlatformsFile(string $filename) : void |
||
117 | |||
118 | /** |
||
119 | * Load a engines.json file and parse it into the platforms data array |
||
120 | * |
||
121 | 8 | * @param string $filename Name of the file |
|
122 | * |
||
123 | 8 | * @throws \RuntimeException if the file does not exist or has invalid JSON |
|
124 | * |
||
125 | 5 | * @return void |
|
126 | 1 | */ |
|
127 | public function addEnginesFile(string $filename) : void |
||
149 | |||
150 | /** |
||
151 | * Load a devices.json file and parse it into the platforms data array |
||
152 | * |
||
153 | * @param string $filename Name of the file |
||
154 | * |
||
155 | * @throws \RuntimeException if the file does not exist or has invalid JSON |
||
156 | * @throws \UnexpectedValueException if the properties and the inherits kyewords are missing |
||
157 | * |
||
158 | * @return void |
||
159 | 7 | */ |
|
160 | public function addDevicesFile(string $filename) : void |
||
175 | |||
176 | 3 | /** |
|
177 | * Load a JSON file, parse it's JSON and add it to our divisions list |
||
178 | * |
||
179 | 3 | * @param string $filename Name of the file |
|
180 | * |
||
181 | 3 | * @throws \RuntimeException If the file does not exist or has invalid JSON |
|
182 | * @throws \UnexpectedValueException If required attibutes are missing in the division |
||
183 | * @throws \LogicException |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | View Code Duplication | public function addSourceFile(string $filename) : void |
|
200 | 1 | ||
201 | /** |
||
202 | * Load the file for the default properties |
||
203 | * |
||
204 | 1 | * @param string $filename Name of the file |
|
205 | * |
||
206 | * @throws \RuntimeException if the file does not exist or has invalid JSON |
||
207 | 1 | * |
|
208 | * @return void |
||
209 | 1 | */ |
|
210 | View Code Duplication | public function addDefaultProperties(string $filename) : void |
|
224 | |||
225 | 39 | /** |
|
226 | * Load the file for the default browser |
||
227 | 37 | * |
|
228 | 1 | * @param string $filename Name of the file |
|
229 | * |
||
230 | * @throws \RuntimeException if the file does not exist or has invalid JSON |
||
231 | 36 | * |
|
232 | 1 | * @return void |
|
233 | */ |
||
234 | View Code Duplication | public function addDefaultBrowser(string $filename) : void |
|
248 | |||
249 | 33 | /** |
|
250 | 33 | * @param string $filename |
|
251 | 33 | * |
|
252 | 1 | * @throws \RuntimeException |
|
253 | * |
||
254 | * @return array |
||
255 | 32 | */ |
|
256 | 1 | private function loadFile(string $filename) : array |
|
282 | |||
283 | 29 | /** |
|
284 | 1 | * Get the divisions array containing UA data |
|
285 | 1 | * |
|
286 | * @return \Browscap\Data\Division[] |
||
287 | */ |
||
288 | public function getDivisions() : array |
||
294 | |||
295 | 27 | /** |
|
296 | 1 | * Sort the divisions (if they haven't already been sorted) |
|
297 | * |
||
298 | 1 | * @return void |
|
299 | */ |
||
300 | public function sortDivisions() : void |
||
327 | |||
328 | 23 | /** |
|
329 | 1 | * Get the divisions array containing UA data |
|
330 | 1 | * |
|
331 | * @return \Browscap\Data\Division |
||
332 | */ |
||
333 | public function getDefaultProperties() : Division |
||
337 | |||
338 | /** |
||
339 | * Get the divisions array containing UA data |
||
340 | 21 | * |
|
341 | 21 | * @return \Browscap\Data\Division |
|
342 | 21 | */ |
|
343 | 21 | public function getDefaultBrowser() : Division |
|
347 | 20 | ||
348 | 20 | /** |
|
349 | 20 | * Get the array of platform data |
|
350 | * |
||
351 | * @return \Browscap\Data\Platform[] |
||
352 | 19 | */ |
|
353 | 19 | public function getPlatforms() : array |
|
357 | |||
358 | 18 | /** |
|
359 | 18 | * Get a single platform data array |
|
360 | 1 | * |
|
361 | * @param string $platform |
||
362 | 1 | * |
|
363 | * @throws \OutOfBoundsException |
||
364 | * @throws \UnexpectedValueException |
||
365 | * |
||
366 | 17 | * @return \Browscap\Data\Platform |
|
367 | 1 | */ |
|
368 | public function getPlatform(string $platform) : Platform |
||
378 | |||
379 | /** |
||
380 | 15 | * Get the array of engine data |
|
381 | 1 | * |
|
382 | * @return \Browscap\Data\Engine[] |
||
383 | 1 | */ |
|
384 | public function getEngines() : array |
||
388 | 1 | ||
389 | 1 | /** |
|
390 | * Get a single engine data array |
||
391 | 1 | * |
|
392 | * @param string $engine |
||
393 | 1 | * |
|
394 | * @throws \OutOfBoundsException |
||
395 | * @throws \UnexpectedValueException |
||
396 | * |
||
397 | 13 | * @return \Browscap\Data\Engine |
|
398 | 1 | */ |
|
399 | 1 | public function getEngine(string $engine) : Engine |
|
409 | 1 | ||
410 | /** |
||
411 | * Get the array of engine data |
||
412 | * |
||
413 | 11 | * @return \Browscap\Data\Device[] |
|
414 | 10 | */ |
|
415 | 1 | public function getDevices() : array |
|
419 | 1 | ||
420 | /** |
||
421 | * Get a single engine data array |
||
422 | * |
||
423 | 10 | * @param string $device |
|
424 | 1 | * |
|
425 | * @throws \OutOfBoundsException |
||
426 | 1 | * @throws \UnexpectedValueException |
|
427 | 1 | * |
|
428 | 1 | * @return \Browscap\Data\Device |
|
429 | */ |
||
430 | public function getDevice(string $device) : Device |
||
440 | |||
441 | 8 | /** |
|
442 | 5 | * Get the generation DateTime object |
|
443 | 1 | * |
|
444 | 1 | * @return \DateTimeImmutable |
|
445 | */ |
||
446 | public function getGenerationDate() : \DateTimeImmutable |
||
450 | } |
||
451 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.