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 |
||
32 | abstract class AbstractRelation extends Base |
||
33 | { |
||
34 | /** |
||
35 | * @readwrite |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $propertyName; |
||
39 | |||
40 | /** |
||
41 | * @readwrite |
||
42 | * @var EntityDescriptorInterface |
||
43 | */ |
||
44 | protected $entityDescriptor; |
||
45 | |||
46 | /** |
||
47 | * Parent or related entity class name |
||
48 | * @readwrite |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $parentEntity; |
||
52 | |||
53 | /** |
||
54 | * @readwrite |
||
55 | * @var EntityDescriptorInterface |
||
56 | */ |
||
57 | protected $parentEntityDescriptor; |
||
58 | |||
59 | /** |
||
60 | * @readwrite |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $foreignKey; |
||
64 | |||
65 | /** |
||
66 | * @readwrite |
||
67 | * @var AdapterInterface |
||
68 | */ |
||
69 | protected $adapter; |
||
70 | |||
71 | /** |
||
72 | * @readwrite |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $lazyLoaded = true; |
||
76 | |||
77 | /** |
||
78 | * Returns the property holding the relation |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 6 | public function getPropertyName() |
|
86 | |||
87 | |||
88 | /** |
||
89 | * Gets the entity descriptor |
||
90 | * |
||
91 | * @return EntityDescriptorInterface |
||
92 | */ |
||
93 | 14 | public function getEntityDescriptor() |
|
97 | |||
98 | /** |
||
99 | * Sets entity descriptor |
||
100 | * |
||
101 | * @param EntityDescriptorInterface $descriptor |
||
102 | * @return $this |
||
103 | */ |
||
104 | 48 | public function setEntityDescriptor(EntityDescriptorInterface $descriptor) |
|
109 | |||
110 | /** |
||
111 | * Gets parent entity class name |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 10 | public function getParentEntity() |
|
119 | |||
120 | /** |
||
121 | * Gets the parent or related entity descriptor |
||
122 | * |
||
123 | * @return EntityDescriptorInterface |
||
124 | */ |
||
125 | 18 | public function getParentEntityDescriptor() |
|
135 | |||
136 | /** |
||
137 | * Sets parent entity descriptor |
||
138 | * |
||
139 | * @param EntityDescriptorInterface $parentEntityDescriptor |
||
140 | * @return BelongsTo |
||
141 | */ |
||
142 | 18 | public function setParentEntityDescriptor( |
|
148 | |||
149 | /** |
||
150 | * Gets the foreign key field name |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 8 | View Code Duplication | public function getForeignKey() |
163 | |||
164 | /** |
||
165 | * Register the retrieved entities in the repository identity map |
||
166 | * |
||
167 | * @param EntityInterface|EntityCollection $entity |
||
168 | * |
||
169 | * @return EntityInterface|EntityCollection |
||
170 | */ |
||
171 | 6 | protected function registerEntity($entity) |
|
179 | |||
180 | /** |
||
181 | * Set the database adapter for this relation |
||
182 | * |
||
183 | * @param AdapterInterface $adapter |
||
184 | * @return $this|self|AbstractRelation |
||
185 | */ |
||
186 | 8 | public function setAdapter(AdapterInterface $adapter) |
|
191 | |||
192 | /** |
||
193 | * Gets relation adapter |
||
194 | * |
||
195 | * @return AdapterInterface |
||
196 | */ |
||
197 | 8 | public function getAdapter() |
|
206 | } |
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.