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 |
||
5 | class DeviceDetector implements DetectorInterface |
||
6 | { |
||
7 | /** |
||
8 | * Determine the user's device. |
||
9 | * |
||
10 | * @param Device $device |
||
11 | * @param UserAgent $userAgent |
||
12 | * @return bool |
||
13 | */ |
||
14 | 2 | public static function detect(Device $device, UserAgent $userAgent) |
|
24 | |||
25 | /** |
||
26 | * Determine if the device is iPad. |
||
27 | * |
||
28 | * @param Device $device |
||
29 | * @param UserAgent $userAgent |
||
30 | * @return bool |
||
31 | */ |
||
32 | 2 | View Code Duplication | private static function checkIpad(Device $device, UserAgent $userAgent) |
41 | |||
42 | /** |
||
43 | * Determine if the device is iPhone. |
||
44 | * |
||
45 | * @param Device $device |
||
46 | * @param UserAgent $userAgent |
||
47 | * @return bool |
||
48 | */ |
||
49 | 2 | View Code Duplication | private static function checkIphone(Device $device, UserAgent $userAgent) |
58 | |||
59 | /** |
||
60 | * Determine if the device is Windows Phone. |
||
61 | * |
||
62 | * @param Device $device |
||
63 | * @param UserAgent $userAgent |
||
64 | * @return bool |
||
65 | */ |
||
66 | 2 | View Code Duplication | private static function checkWindowsPhone(Device $device, UserAgent $userAgent) |
79 | } |
||
80 |
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.