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 Properties 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 Properties, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Properties |
||
28 | { |
||
29 | /** constants */ |
||
30 | const PROPERTY_TYPE_BOOLEAN = 'b'; |
||
31 | const PROPERTY_TYPE_INTEGER = 'i'; |
||
32 | const PROPERTY_TYPE_FLOAT = 'f'; |
||
33 | const PROPERTY_TYPE_DATE = 'd'; |
||
34 | const PROPERTY_TYPE_STRING = 's'; |
||
35 | const PROPERTY_TYPE_UNKNOWN = 'u'; |
||
36 | |||
37 | /** |
||
38 | * Creator. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $creator = 'Unknown Creator'; |
||
43 | |||
44 | /** |
||
45 | * LastModifiedBy. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $lastModifiedBy; |
||
50 | |||
51 | /** |
||
52 | * Created. |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | private $created; |
||
57 | |||
58 | /** |
||
59 | * Modified. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | private $modified; |
||
64 | |||
65 | /** |
||
66 | * Title. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $title = 'Untitled Spreadsheet'; |
||
71 | |||
72 | /** |
||
73 | * Description. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | private $description = ''; |
||
78 | |||
79 | /** |
||
80 | * Subject. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $subject = ''; |
||
85 | |||
86 | /** |
||
87 | * Keywords. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $keywords = ''; |
||
92 | |||
93 | /** |
||
94 | * Category. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | private $category = ''; |
||
99 | |||
100 | /** |
||
101 | * Manager. |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | private $manager = ''; |
||
106 | |||
107 | /** |
||
108 | * Company. |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | private $company = 'Microsoft Corporation'; |
||
113 | |||
114 | /** |
||
115 | * Custom Properties. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | private $customProperties = []; |
||
120 | |||
121 | /** |
||
122 | * Create a new Document Properties instance. |
||
123 | */ |
||
124 | 73 | public function __construct() |
|
131 | |||
132 | /** |
||
133 | * Get Creator. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 58 | public function getCreator() |
|
141 | |||
142 | /** |
||
143 | * Set Creator. |
||
144 | * |
||
145 | * @param string $creator |
||
146 | * |
||
147 | * @return Properties |
||
148 | */ |
||
149 | 48 | public function setCreator($creator) |
|
155 | |||
156 | /** |
||
157 | * Get Last Modified By. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 54 | public function getLastModifiedBy() |
|
165 | |||
166 | /** |
||
167 | * Set Last Modified By. |
||
168 | * |
||
169 | * @param string $pValue |
||
170 | * |
||
171 | * @return Properties |
||
172 | */ |
||
173 | 43 | public function setLastModifiedBy($pValue) |
|
179 | |||
180 | /** |
||
181 | * Get Created. |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | 54 | public function getCreated() |
|
189 | |||
190 | /** |
||
191 | * Set Created. |
||
192 | * |
||
193 | * @param datetime $pValue |
||
194 | * |
||
195 | * @return Properties |
||
196 | */ |
||
197 | 19 | View Code Duplication | public function setCreated($pValue) |
213 | |||
214 | /** |
||
215 | * Get Modified. |
||
216 | * |
||
217 | * @return int |
||
218 | */ |
||
219 | 54 | public function getModified() |
|
223 | |||
224 | /** |
||
225 | * Set Modified. |
||
226 | * |
||
227 | * @param datetime $pValue |
||
228 | * |
||
229 | * @return Properties |
||
230 | */ |
||
231 | 19 | View Code Duplication | public function setModified($pValue) |
247 | |||
248 | /** |
||
249 | * Get Title. |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 58 | public function getTitle() |
|
257 | |||
258 | /** |
||
259 | * Set Title. |
||
260 | * |
||
261 | * @param string $title |
||
262 | * |
||
263 | * @return Properties |
||
264 | */ |
||
265 | 47 | public function setTitle($title) |
|
271 | |||
272 | /** |
||
273 | * Get Description. |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | 58 | public function getDescription() |
|
281 | |||
282 | /** |
||
283 | * Set Description. |
||
284 | * |
||
285 | * @param string $description |
||
286 | * |
||
287 | * @return Properties |
||
288 | */ |
||
289 | 47 | public function setDescription($description) |
|
295 | |||
296 | /** |
||
297 | * Get Subject. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | 58 | public function getSubject() |
|
305 | |||
306 | /** |
||
307 | * Set Subject. |
||
308 | * |
||
309 | * @param string $subject |
||
310 | * |
||
311 | * @return Properties |
||
312 | */ |
||
313 | 47 | public function setSubject($subject) |
|
319 | |||
320 | /** |
||
321 | * Get Keywords. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 58 | public function getKeywords() |
|
329 | |||
330 | /** |
||
331 | * Set Keywords. |
||
332 | * |
||
333 | * @param string $keywords |
||
334 | * |
||
335 | * @return Properties |
||
336 | */ |
||
337 | 47 | public function setKeywords($keywords) |
|
343 | |||
344 | /** |
||
345 | * Get Category. |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | 58 | public function getCategory() |
|
353 | |||
354 | /** |
||
355 | * Set Category. |
||
356 | * |
||
357 | * @param string $category |
||
358 | * |
||
359 | * @return Properties |
||
360 | */ |
||
361 | 42 | public function setCategory($category) |
|
367 | |||
368 | /** |
||
369 | * Get Company. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 57 | public function getCompany() |
|
377 | |||
378 | /** |
||
379 | * Set Company. |
||
380 | * |
||
381 | * @param string $company |
||
382 | * |
||
383 | * @return Properties |
||
384 | */ |
||
385 | 10 | public function setCompany($company) |
|
391 | |||
392 | /** |
||
393 | * Get Manager. |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | 57 | public function getManager() |
|
401 | |||
402 | /** |
||
403 | * Set Manager. |
||
404 | * |
||
405 | * @param string $manager |
||
406 | * |
||
407 | * @return Properties |
||
408 | */ |
||
409 | 6 | public function setManager($manager) |
|
415 | |||
416 | /** |
||
417 | * Get a List of Custom Property Names. |
||
418 | * |
||
419 | * @return array of string |
||
420 | */ |
||
421 | 53 | public function getCustomProperties() |
|
425 | |||
426 | /** |
||
427 | * Check if a Custom Property is defined. |
||
428 | * |
||
429 | * @param string $propertyName |
||
430 | * |
||
431 | * @return bool |
||
432 | */ |
||
433 | public function isCustomPropertySet($propertyName) |
||
437 | |||
438 | /** |
||
439 | * Get a Custom Property Value. |
||
440 | * |
||
441 | * @param string $propertyName |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | 1 | public function getCustomPropertyValue($propertyName) |
|
451 | |||
452 | /** |
||
453 | * Get a Custom Property Type. |
||
454 | * |
||
455 | * @param string $propertyName |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | 1 | public function getCustomPropertyType($propertyName) |
|
465 | |||
466 | /** |
||
467 | * Set a Custom Property. |
||
468 | * |
||
469 | * @param string $propertyName |
||
470 | * @param mixed $propertyValue |
||
471 | * @param string $propertyType |
||
472 | * 'i' : Integer |
||
473 | * 'f' : Floating Point |
||
474 | * 's' : String |
||
475 | * 'd' : Date/Time |
||
476 | * 'b' : Boolean |
||
477 | * |
||
478 | * @return Properties |
||
479 | */ |
||
480 | 5 | public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) |
|
507 | |||
508 | /** |
||
509 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
510 | */ |
||
511 | View Code Duplication | public function __clone() |
|
522 | |||
523 | public static function convertProperty($propertyValue, $propertyType) |
||
582 | |||
583 | public static function convertPropertyType($propertyType) |
||
636 | } |
||
637 |
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.