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 |
||
9 | class Map |
||
10 | { |
||
11 | /** |
||
12 | * @var EntityGubbins[] |
||
13 | */ |
||
14 | private $entities; |
||
15 | |||
16 | /** |
||
17 | * @var Association[] |
||
18 | */ |
||
19 | private $associations; |
||
20 | |||
21 | /** |
||
22 | * @param EntityGubbins $entity |
||
23 | */ |
||
24 | public function addEntity(EntityGubbins $entity) |
||
31 | |||
32 | /** |
||
33 | * @return EntityGubbins[] |
||
34 | */ |
||
35 | public function getEntities() |
||
39 | |||
40 | public function resolveEntity($entityClassName) |
||
44 | |||
45 | /** |
||
46 | * @param EntityGubbins[] $entities |
||
47 | */ |
||
48 | public function setEntities(array $entities) |
||
60 | |||
61 | /** |
||
62 | * @param Association[] $associations |
||
63 | */ |
||
64 | public function setAssociations(array $associations) |
||
70 | |||
71 | /** |
||
72 | * @param Association $association |
||
73 | */ |
||
74 | public function addAssociation(Association $association) |
||
88 | |||
89 | /** |
||
90 | * @return Association[] |
||
91 | */ |
||
92 | public function getAssociations() |
||
96 | |||
97 | /** |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function isOK() |
||
109 | |||
110 | /** |
||
111 | * @param AssociationMonomorphic $association |
||
112 | */ |
||
113 | View Code Duplication | private function addAssociationMonomorphic(AssociationMonomorphic $association) |
|
120 | |||
121 | /** |
||
122 | * @param AssociationPolymorphic $association |
||
123 | */ |
||
124 | View Code Duplication | private function addAssociationPolymorphic(AssociationPolymorphic $association) |
|
133 | } |
||
134 |
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.