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 |
||
7 | class Collection extends ArrayObject |
||
8 | { |
||
9 | /** |
||
10 | * @param string $entityMethod |
||
11 | * @return static |
||
12 | */ |
||
13 | View Code Duplication | public function rebuildByEntityMethod($entityMethod) |
|
23 | |||
24 | /** |
||
25 | * @param string $entityMethod |
||
26 | * @return array |
||
27 | */ |
||
28 | public function getValuesByEntityMethod($entityMethod) |
||
38 | |||
39 | /** |
||
40 | * @param string $entityMethod |
||
41 | * @param mixed $value |
||
42 | * @return static |
||
43 | */ |
||
44 | View Code Duplication | public function filter($entityMethod, $value) |
|
56 | |||
57 | /** |
||
58 | * @return Collection |
||
59 | */ |
||
60 | public function rebuildById() |
||
64 | |||
65 | /** |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getIds() |
||
78 | |||
79 | /** |
||
80 | * @param $attribute |
||
81 | * @param bool $unique |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getValueByAttribute($attribute, $unique = true) |
||
104 | |||
105 | /** |
||
106 | * @return Entity |
||
107 | */ |
||
108 | public function getFirst() |
||
114 | } |
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.