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 ChargeSort |
||
23 | { |
||
24 | public static function anyCharges(): SortChain |
||
32 | |||
33 | View Code Duplication | private static function byHardwareType(): \Closure |
|
48 | |||
49 | private static function byType(): \Closure |
||
50 | { |
||
51 | $order = [ |
||
52 | 'rack', |
||
53 | 'rack_unit', |
||
54 | 'ip_num', |
||
55 | 'support_time', |
||
56 | 'backup_du', |
||
57 | 'server_traf_max', |
||
58 | 'server_traf95_max', |
||
59 | 'server_du', |
||
60 | 'server_ssd', |
||
61 | 'server_sata', |
||
62 | 'win_license', |
||
63 | ]; |
||
64 | |||
65 | return function (Charge $charge) use ($order) { |
||
66 | if (($key = array_search($charge->type, $order, true)) !== false) { |
||
67 | return $key; |
||
68 | } |
||
69 | |||
70 | return INF; |
||
71 | }; |
||
72 | } |
||
73 | |||
74 | private static function keepDiscountsWithParents(): \Closure |
||
84 | |||
85 | private static function byName(): \Closure |
||
91 | } |
||
92 |
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.