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:
Complex classes like EntityContainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntityContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EntityContainer extends IsOK |
||
17 | { |
||
18 | use IsOKToolboxTrait, TSimpleIdentifierTrait, AccessTypeTraits; |
||
19 | /** |
||
20 | * @property string $name |
||
21 | */ |
||
22 | private $name = null; |
||
23 | |||
24 | /** |
||
25 | * @property string $extends |
||
26 | */ |
||
27 | private $extends = null; |
||
28 | |||
29 | /** |
||
30 | * @property string $typeAccess |
||
31 | */ |
||
32 | private $typeAccess = null; |
||
33 | |||
34 | /** |
||
35 | * @property boolean $lazyLoadingEnabled |
||
36 | */ |
||
37 | private $lazyLoadingEnabled = null; |
||
38 | |||
39 | /** |
||
40 | * @property boolean $isDefaultEntityContainer |
||
41 | */ |
||
42 | private $isDefaultEntityContainer = false; |
||
43 | |||
44 | /** |
||
45 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType $documentation |
||
46 | */ |
||
47 | private $documentation = null; |
||
48 | |||
49 | /** |
||
50 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType[] |
||
51 | * $functionImport |
||
52 | */ |
||
53 | private $functionImport = []; |
||
54 | |||
55 | /** |
||
56 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType[] $entitySet |
||
57 | */ |
||
58 | private $entitySet = []; |
||
59 | |||
60 | /** |
||
61 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType[] |
||
62 | * $associationSet |
||
63 | */ |
||
64 | private $associationSet = []; |
||
65 | |||
66 | public function __construct($name = "DefaultContainer") |
||
70 | |||
71 | /** |
||
72 | * Gets as name |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getName() |
||
80 | |||
81 | /** |
||
82 | * Sets a new name |
||
83 | * |
||
84 | * @param string $name |
||
85 | * @return self |
||
86 | */ |
||
87 | public function setName($name) |
||
96 | |||
97 | /** |
||
98 | * Gets as extends |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getExtends() |
||
106 | |||
107 | /** |
||
108 | * Sets a new extends |
||
109 | * |
||
110 | * @param string $extends |
||
111 | * @return self |
||
112 | */ |
||
113 | public function setExtends($extends) |
||
122 | |||
123 | /** |
||
124 | * Gets as typeAccess |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getTypeAccess() |
||
132 | |||
133 | /** |
||
134 | * Sets a new typeAccess |
||
135 | * |
||
136 | * @param string $typeAccess |
||
137 | * @return self |
||
138 | */ |
||
139 | View Code Duplication | public function setTypeAccess($typeAccess) |
|
148 | |||
149 | /** |
||
150 | * Gets as lazyLoadingEnabled |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | public function getLazyLoadingEnabled() |
||
158 | |||
159 | /** |
||
160 | * Sets a new lazyLoadingEnabled |
||
161 | * |
||
162 | * @param boolean $lazyLoadingEnabled |
||
163 | * @return self |
||
164 | */ |
||
165 | public function setLazyLoadingEnabled($lazyLoadingEnabled) |
||
170 | |||
171 | /** |
||
172 | * Gets as documentation |
||
173 | * |
||
174 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType |
||
175 | */ |
||
176 | public function getDocumentation() |
||
180 | |||
181 | /** |
||
182 | * Gets as lazyLoadingEnabled |
||
183 | * |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function getIsDefaultEntityContainer() |
||
190 | |||
191 | /** |
||
192 | * Sets a new isDefaultEntityContainer |
||
193 | * |
||
194 | * @param boolean $isDefaultEntityContainer |
||
195 | * @return self |
||
196 | */ |
||
197 | public function setIsDefaultEntityContainer($isDefaultEntityContainer) |
||
202 | |||
203 | /** |
||
204 | * Sets a new documentation |
||
205 | * |
||
206 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType $documentation |
||
207 | * @return self |
||
208 | */ |
||
209 | View Code Duplication | public function setDocumentation(TDocumentationType $documentation) |
|
218 | |||
219 | /** |
||
220 | * Adds as functionImport |
||
221 | * |
||
222 | * @return self |
||
223 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType |
||
224 | * $functionImport |
||
225 | */ |
||
226 | public function addToFunctionImport(FunctionImportAnonymousType $functionImport) |
||
235 | |||
236 | /** |
||
237 | * isset functionImport |
||
238 | * |
||
239 | * @param scalar $index |
||
240 | * @return boolean |
||
241 | */ |
||
242 | public function issetFunctionImport($index) |
||
246 | |||
247 | /** |
||
248 | * unset functionImport |
||
249 | * |
||
250 | * @param scalar $index |
||
251 | * @return void |
||
252 | */ |
||
253 | public function unsetFunctionImport($index) |
||
257 | |||
258 | /** |
||
259 | * Gets as functionImport |
||
260 | * |
||
261 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType[] |
||
262 | */ |
||
263 | public function getFunctionImport() |
||
267 | |||
268 | /** |
||
269 | * Sets a new functionImport |
||
270 | * |
||
271 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType[] |
||
272 | * $functionImport |
||
273 | * @return self |
||
274 | */ |
||
275 | public function setFunctionImport(array $functionImport) |
||
287 | |||
288 | /** |
||
289 | * Adds as entitySet |
||
290 | * |
||
291 | * @return self |
||
292 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType $entitySet |
||
293 | */ |
||
294 | View Code Duplication | public function addToEntitySet(EntitySetAnonymousType $entitySet) |
|
303 | |||
304 | /** |
||
305 | * isset entitySet |
||
306 | * |
||
307 | * @param scalar $index |
||
308 | * @return boolean |
||
309 | */ |
||
310 | public function issetEntitySet($index) |
||
314 | |||
315 | /** |
||
316 | * unset entitySet |
||
317 | * |
||
318 | * @param scalar $index |
||
319 | * @return void |
||
320 | */ |
||
321 | public function unsetEntitySet($index) |
||
325 | |||
326 | /** |
||
327 | * Gets as entitySet |
||
328 | * |
||
329 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType[] |
||
330 | */ |
||
331 | public function getEntitySet() |
||
335 | |||
336 | /** |
||
337 | * Sets a new entitySet |
||
338 | * |
||
339 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType[] $entitySet |
||
340 | * @return self |
||
341 | */ |
||
342 | View Code Duplication | public function setEntitySet(array $entitySet) |
|
354 | |||
355 | /** |
||
356 | * Adds as associationSet |
||
357 | * |
||
358 | * @return self |
||
359 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType |
||
360 | * $associationSet |
||
361 | */ |
||
362 | public function addToAssociationSet(AssociationSetAnonymousType $associationSet) |
||
371 | |||
372 | /** |
||
373 | * isset associationSet |
||
374 | * |
||
375 | * @param scalar $index |
||
376 | * @return boolean |
||
377 | */ |
||
378 | public function issetAssociationSet($index) |
||
382 | |||
383 | /** |
||
384 | * unset associationSet |
||
385 | * |
||
386 | * @param scalar $index |
||
387 | * @return void |
||
388 | */ |
||
389 | public function unsetAssociationSet($index) |
||
393 | |||
394 | /** |
||
395 | * Gets as associationSet |
||
396 | * |
||
397 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType[] |
||
398 | */ |
||
399 | public function getAssociationSet() |
||
403 | |||
404 | /** |
||
405 | * Sets a new associationSet |
||
406 | * |
||
407 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType[] |
||
408 | * $associationSet |
||
409 | * @return self |
||
410 | */ |
||
411 | View Code Duplication | public function setAssociationSet(array $associationSet) |
|
423 | |||
424 | public function isOK(&$msg = null) |
||
471 | |||
472 | public function isStructureOK(&$msg = null) |
||
486 | } |
||
487 |
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.