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 |
||
15 | class Collector |
||
16 | { |
||
17 | /** |
||
18 | * @var mixed |
||
19 | */ |
||
20 | private $data; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $collectedKeys; |
||
26 | |||
27 | public function __construct() |
||
32 | |||
33 | /** |
||
34 | * @param string $name |
||
35 | * @param mixed $data |
||
36 | * @param string $destination |
||
37 | * @param string $type |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | View Code Duplication | public function collect($name, $data, $destination = DataCollection::DESTINATION_PROFILER, $type = null) |
|
50 | |||
51 | /** |
||
52 | * @param string $name |
||
53 | * @param mixed $data |
||
54 | * @param string $destination |
||
55 | * @param string $type |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | View Code Duplication | public function collectOnce($name, $data, $destination = DataCollection::DESTINATION_PROFILER, $type = null) |
|
69 | |||
70 | /** |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function getData() |
||
77 | |||
78 | private function handleDataKey($name) |
||
84 | } |
||
85 |
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.