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 |
||
14 | class Display |
||
15 | { |
||
16 | |||
17 | /** @var array */ |
||
18 | protected $defaults = array( |
||
19 | 'script_path' => 'asset/script.js', |
||
20 | 'style_path' => 'asset/style.css' |
||
21 | ); |
||
22 | |||
23 | /** @var array */ |
||
24 | protected $options; |
||
25 | |||
26 | /** @var double */ |
||
27 | protected $startTime; |
||
28 | |||
29 | /** @var Console */ |
||
30 | protected $console; |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $speedData; |
||
34 | |||
35 | /** @var array */ |
||
36 | protected $queryData; |
||
37 | |||
38 | /** @var array */ |
||
39 | protected $memoryData; |
||
40 | |||
41 | /** @var array */ |
||
42 | protected $fileData; |
||
43 | |||
44 | /** |
||
45 | * @param array $options |
||
46 | */ |
||
47 | public function __construct(array $options = array()) |
||
52 | |||
53 | /** |
||
54 | * @param double $startTime |
||
55 | */ |
||
56 | public function setStartTime($startTime) |
||
60 | |||
61 | /** |
||
62 | * @param Console $console |
||
63 | */ |
||
64 | public function setConsole(Console $console) |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | protected function formatConsoleData() |
||
128 | |||
129 | /** |
||
130 | * Sets speed data |
||
131 | * |
||
132 | * @param array $data |
||
133 | */ |
||
134 | public function setSpeedData(array $data) |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function getSpeedMeta() |
||
152 | |||
153 | /** |
||
154 | * Sets file data |
||
155 | * |
||
156 | * @param array $data |
||
157 | */ |
||
158 | public function setFileData(array $data) |
||
162 | |||
163 | /** |
||
164 | * @return array |
||
165 | */ |
||
166 | View Code Duplication | protected function formatFileData() |
|
194 | |||
195 | /** |
||
196 | * Sets memory data |
||
197 | * |
||
198 | * @param array $data |
||
199 | */ |
||
200 | public function setMemoryData(array $data) |
||
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | public function getMemoryMeta() |
||
218 | |||
219 | /** |
||
220 | * Sets query data |
||
221 | * |
||
222 | * @param array $data |
||
223 | */ |
||
224 | public function setQueryData(array $data) |
||
228 | |||
229 | /** |
||
230 | * @return array |
||
231 | */ |
||
232 | View Code Duplication | public function formatQueryData() |
|
260 | |||
261 | |||
262 | /** |
||
263 | * Formatter for human-readable time |
||
264 | * Only handles time up to 60 minutes gracefully |
||
265 | * |
||
266 | * @param double $time |
||
267 | * @param integer $percision |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function getReadableTime($time, $percision = 3) |
||
283 | |||
284 | /** |
||
285 | * Formatter for human-readable memory |
||
286 | * Only handles time up to a few gigs gracefully |
||
287 | * |
||
288 | * @param double $size |
||
289 | * @param integer $percision |
||
290 | */ |
||
291 | protected function getReadableMemory($size, $percision = 2) |
||
301 | |||
302 | /** |
||
303 | * @param array $messages |
||
304 | * @param string $type |
||
305 | * @return array |
||
306 | */ |
||
307 | protected function filterMessages($messages, $type) |
||
313 | |||
314 | public function __invoke() |
||
346 | } |
||
347 |
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.