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 |
||
27 | class Filesize implements JsonSerializable |
||
28 | { |
||
29 | /** |
||
30 | * The filesize in bytes |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | private $bytes; |
||
35 | |||
36 | /** |
||
37 | * @param int $bytes |
||
38 | * |
||
39 | * @throws InvalidArgumentException If given bytes are not an integer |
||
40 | */ |
||
41 | public function __construct($bytes) |
||
49 | |||
50 | /** |
||
51 | * Sets the bytes |
||
52 | * |
||
53 | * @param int $bytes |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | private function setBytes($bytes) |
||
61 | |||
62 | /** |
||
63 | * Getter for bytes |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | public function getBytes() |
||
71 | |||
72 | /** |
||
73 | * Returns the filesize in KiloBytes |
||
74 | * |
||
75 | * @return float |
||
76 | */ |
||
77 | public function getKiloBytes() |
||
81 | |||
82 | /** |
||
83 | * Returns the filesize in MegaBytes |
||
84 | * |
||
85 | * @return float |
||
86 | */ |
||
87 | public function getMegaBytes() |
||
91 | |||
92 | /** |
||
93 | * Returns the filesize in GigaBytes |
||
94 | * |
||
95 | * @return float |
||
96 | */ |
||
97 | public function getGigaBytes() |
||
101 | |||
102 | /** |
||
103 | * Creates instance from a megabyte string notation |
||
104 | * Examples: |
||
105 | * |
||
106 | * - '5MB' |
||
107 | * - '5 MB' |
||
108 | * - '5.4MB' |
||
109 | * - '5.4 MB' |
||
110 | * |
||
111 | * @param string $filesize |
||
112 | * |
||
113 | * @return Filesize |
||
114 | */ |
||
115 | View Code Duplication | public static function fromMegaBytesString($filesize) |
|
135 | |||
136 | /** |
||
137 | * Creates new instance from megabyte amount |
||
138 | * Examples: |
||
139 | * |
||
140 | * - 5 |
||
141 | * - 5.4 |
||
142 | * |
||
143 | * @param int|float $megabytes |
||
144 | * |
||
145 | * @return Filesize |
||
146 | */ |
||
147 | View Code Duplication | public static function fromMegaBytes($megabytes) |
|
157 | |||
158 | /** |
||
159 | * Creates instance from a kilobyte string notation |
||
160 | * Examples: |
||
161 | * |
||
162 | * - '5KB' |
||
163 | * - '5 KB' |
||
164 | * - '5.4KB' |
||
165 | * - '5.4 KB' |
||
166 | * |
||
167 | * @param string $filesize |
||
168 | * |
||
169 | * @return Filesize |
||
170 | */ |
||
171 | View Code Duplication | public static function fromKiloBytesString($filesize) |
|
191 | |||
192 | /** |
||
193 | * Creates new instance from kilobyte amount |
||
194 | * Examples: |
||
195 | * |
||
196 | * - 5 |
||
197 | * - 5.4 |
||
198 | * |
||
199 | * @param int|float $kilobytes |
||
200 | * |
||
201 | * @return Filesize |
||
202 | */ |
||
203 | View Code Duplication | public static function fromKiloBytes($kilobytes) |
|
213 | |||
214 | /** |
||
215 | * Creates a new instance from given Filesize object |
||
216 | * |
||
217 | * @param Filesize $filesize |
||
218 | * |
||
219 | * @return Filesize |
||
220 | */ |
||
221 | public static function fromFilesize(Filesize $filesize) |
||
227 | |||
228 | /** |
||
229 | * @inheritDoc |
||
230 | * |
||
231 | * @return int |
||
232 | */ |
||
233 | public function jsonSerialize() |
||
237 | |||
238 | /** |
||
239 | * Returns string representation |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function __toString() |
||
247 | } |
||
248 |
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.