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 |
||
23 | class PriceSort |
||
24 | { |
||
25 | public static function anyPrices(): SortChain |
||
31 | |||
32 | /** |
||
33 | * @return SortChain |
||
34 | */ |
||
35 | public static function serverPrices(): SortChain |
||
45 | |||
46 | public static function zonePrices(): SortChain |
||
50 | |||
51 | private static function byServerPriceType() |
||
61 | |||
62 | private static function byServerPriceGroups(): Closure |
||
76 | |||
77 | private static function byServerMainPrices(): Closure |
||
78 | { |
||
79 | $order = [ |
||
80 | 'rack', |
||
81 | 'rack_unit', |
||
82 | 'ip_num', |
||
83 | 'support_time', |
||
84 | 'backup_du', |
||
85 | 'server_traf_max', |
||
86 | 'cdn_traf_max', |
||
87 | 'server_traf95_max', |
||
88 | 'cdn_traf95_max', |
||
89 | 'server_du', |
||
90 | 'storage_du', |
||
91 | 'cdn_cache', |
||
92 | 'server_ssd', |
||
93 | 'server_sata', |
||
94 | 'win_license', |
||
95 | ]; |
||
96 | |||
97 | View Code Duplication | return static function (Price $price) use ($order) { |
|
|
|||
98 | if (($key = array_search($price->getSubtype(), $order, true)) !== false) { |
||
99 | return $key; |
||
100 | } |
||
101 | |||
102 | return INF; |
||
103 | }; |
||
104 | } |
||
105 | |||
106 | View Code Duplication | private static function byHardwareType(): Closure |
|
119 | |||
120 | private static function byTargetObjectName(): Closure |
||
126 | |||
127 | private static function byObjectType(): Closure |
||
128 | { |
||
129 | $order = ['dedicated', 'net', 'model_group', 'part']; |
||
130 | |||
131 | View Code Duplication | return function (Price $price) use ($order) { |
|
132 | if (($key = array_search($price->object->type, $order, true)) !== false) { |
||
133 | return $key; |
||
134 | } |
||
135 | |||
136 | return INF; |
||
137 | }; |
||
138 | } |
||
139 | |||
140 | private static function byObjectNo(): Closure |
||
146 | } |
||
147 |
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.