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 | abstract class InformationUnit |
||
24 | { |
||
25 | /** |
||
26 | * InformationUnit constructor. |
||
27 | * |
||
28 | * @param float $amount |
||
29 | * |
||
30 | * @throws \OutOfBoundsException |
||
31 | */ |
||
32 | public function __construct(float $amount) |
||
39 | |||
40 | /** |
||
41 | * @return float |
||
42 | */ |
||
43 | public function getAmount() : float |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getDimension() : string |
||
55 | |||
56 | /** |
||
57 | * @param InformationUnit $unit |
||
58 | * |
||
59 | * @return InformationUnit |
||
60 | */ |
||
61 | public function add(InformationUnit $unit) : self |
||
68 | |||
69 | /** |
||
70 | * @param InformationUnit $unit |
||
71 | * |
||
72 | * @return InformationUnit |
||
73 | * @throws \OutOfBoundsException |
||
74 | */ |
||
75 | public function subtract(InformationUnit $unit) : self |
||
88 | |||
89 | /** |
||
90 | * @param float $number |
||
91 | * |
||
92 | * @return InformationUnit |
||
93 | */ |
||
94 | public function multiply(float $number) : self |
||
100 | |||
101 | /** |
||
102 | * @param float $number |
||
103 | * |
||
104 | * @return InformationUnit |
||
105 | * @throws \OutOfBoundsException |
||
106 | */ |
||
107 | public function divide(float $number) : self |
||
117 | |||
118 | /** |
||
119 | * @return Bytes |
||
120 | * @throws \OutOfBoundsException |
||
121 | */ |
||
122 | public function toB() : Bytes |
||
126 | |||
127 | /** |
||
128 | * @return KiloBytes |
||
129 | * @throws \OutOfBoundsException |
||
130 | */ |
||
131 | public function toKB() : KiloBytes |
||
135 | |||
136 | /** |
||
137 | * @return MegaBytes |
||
138 | * @throws \OutOfBoundsException |
||
139 | */ |
||
140 | public function toMB() : MegaBytes |
||
144 | |||
145 | /** |
||
146 | * @return GigaBytes |
||
147 | * @throws \OutOfBoundsException |
||
148 | */ |
||
149 | public function toGB() : GigaBytes |
||
153 | |||
154 | /** |
||
155 | * @return TeraBytes |
||
156 | * @throws \OutOfBoundsException |
||
157 | */ |
||
158 | public function toTB() : TeraBytes |
||
162 | |||
163 | /** |
||
164 | * @return PetaBytes |
||
165 | * @throws \OutOfBoundsException |
||
166 | */ |
||
167 | public function toPB() : PetaBytes |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function __toString() : string |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | public function asHumanReadableString() : string |
||
187 | |||
188 | /** |
||
189 | * @param float $value |
||
190 | * @param int $fromDimension |
||
191 | * @param int $toDimension |
||
192 | * |
||
193 | * @return float |
||
194 | * |
||
195 | */ |
||
196 | protected static function convert(float $value, int $fromDimension, int $toDimension) : float |
||
214 | |||
215 | /** |
||
216 | * @return int |
||
217 | */ |
||
218 | protected function getDimensionCode() : int |
||
222 | |||
223 | /** |
||
224 | * @var int |
||
225 | */ |
||
226 | protected static $multiplier = 1024; |
||
227 | |||
228 | /** |
||
229 | * Dimension codes |
||
230 | */ |
||
231 | protected static $byte = 1; |
||
232 | protected static $kiloByte = 2; |
||
233 | protected static $megaByte = 3; |
||
234 | protected static $gigaByte = 4; |
||
235 | protected static $teraByte = 5; |
||
236 | protected static $petaByte = 6; |
||
237 | |||
238 | /** |
||
239 | * @var array |
||
240 | */ |
||
241 | protected static $dimensionName = [ |
||
242 | 1 => 'B', |
||
243 | 2 => 'kB', |
||
244 | 3 => 'MB', |
||
245 | 4 => 'GB', |
||
246 | 5 => 'TB', |
||
247 | 6 => 'PB' |
||
248 | ]; |
||
249 | |||
250 | /** |
||
251 | * @var int |
||
252 | */ |
||
253 | protected $dimension; |
||
254 | |||
255 | /** |
||
256 | * @var float |
||
257 | */ |
||
258 | private $amount; |
||
259 | } |
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.