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 Column 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 Column, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Column |
||
38 | { |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $name; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $type; |
||
48 | |||
49 | /** |
||
50 | * @var integer |
||
51 | */ |
||
52 | protected $limit = null; |
||
53 | |||
54 | /** |
||
55 | * @var boolean |
||
56 | */ |
||
57 | protected $null = false; |
||
58 | |||
59 | /** |
||
60 | * @var mixed |
||
61 | */ |
||
62 | protected $default = null; |
||
63 | |||
64 | /** |
||
65 | * @var boolean |
||
66 | */ |
||
67 | protected $identity = false; |
||
68 | |||
69 | /** |
||
70 | * @var integer |
||
71 | */ |
||
72 | protected $precision; |
||
73 | |||
74 | /** |
||
75 | * @var integer |
||
76 | */ |
||
77 | protected $scale; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $after; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $update; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $comment; |
||
93 | |||
94 | /** |
||
95 | * @var boolean |
||
96 | */ |
||
97 | protected $signed = true; |
||
98 | |||
99 | /** |
||
100 | * @var boolean |
||
101 | */ |
||
102 | protected $timezone = false; |
||
103 | |||
104 | /** |
||
105 | * @var array |
||
106 | */ |
||
107 | protected $properties = []; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $collation; |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $encoding; |
||
118 | |||
119 | /** |
||
120 | * @var array |
||
121 | */ |
||
122 | protected $values; |
||
123 | |||
124 | /** |
||
125 | * Sets the column name. |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @return Column |
||
129 | */ |
||
130 | 215 | public function setName($name) |
|
135 | |||
136 | /** |
||
137 | * Gets the column name. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 212 | public function getName() |
|
145 | |||
146 | /** |
||
147 | * Sets the column type. |
||
148 | * |
||
149 | * @param string $type |
||
150 | * @return Column |
||
151 | */ |
||
152 | 214 | public function setType($type) |
|
157 | |||
158 | /** |
||
159 | * Gets the column type. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 212 | public function getType() |
|
167 | |||
168 | /** |
||
169 | * Sets the column limit. |
||
170 | * |
||
171 | * @param integer $limit |
||
172 | * @return Column |
||
173 | */ |
||
174 | 192 | public function setLimit($limit) |
|
179 | |||
180 | /** |
||
181 | * Gets the column limit. |
||
182 | * |
||
183 | * @return integer |
||
184 | */ |
||
185 | 192 | public function getLimit() |
|
189 | |||
190 | /** |
||
191 | * Sets whether the column allows nulls. |
||
192 | * |
||
193 | * @param boolean $null |
||
194 | * @return Column |
||
195 | */ |
||
196 | 208 | public function setNull($null) |
|
201 | |||
202 | /** |
||
203 | * Gets whether the column allows nulls. |
||
204 | * |
||
205 | * @return boolean |
||
206 | */ |
||
207 | 215 | public function getNull() |
|
211 | |||
212 | /** |
||
213 | * Does the column allow nulls? |
||
214 | * |
||
215 | * @return boolean |
||
216 | */ |
||
217 | 214 | public function isNull() |
|
221 | |||
222 | /** |
||
223 | * Sets the default column value. |
||
224 | * |
||
225 | * @param mixed $default |
||
226 | * @return Column |
||
227 | */ |
||
228 | 207 | public function setDefault($default) |
|
233 | |||
234 | /** |
||
235 | * Gets the default column value. |
||
236 | * |
||
237 | * @return mixed |
||
238 | */ |
||
239 | 215 | public function getDefault() |
|
243 | |||
244 | /** |
||
245 | * Sets whether or not the column is an identity column. |
||
246 | * |
||
247 | * @param boolean $identity |
||
248 | * @return Column |
||
249 | */ |
||
250 | 158 | public function setIdentity($identity) |
|
255 | |||
256 | /** |
||
257 | * Gets whether or not the column is an identity column. |
||
258 | * |
||
259 | * @return boolean |
||
260 | */ |
||
261 | 199 | public function getIdentity() |
|
265 | |||
266 | /** |
||
267 | * Is the column an identity column? |
||
268 | * |
||
269 | * @return boolean |
||
270 | */ |
||
271 | 198 | public function isIdentity() |
|
275 | |||
276 | /** |
||
277 | * Sets the name of the column to add this column after. |
||
278 | * |
||
279 | * @param string $after After |
||
280 | * @return Column |
||
281 | */ |
||
282 | 1 | public function setAfter($after) |
|
287 | |||
288 | /** |
||
289 | * Returns the name of the column to add this column after. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | 20 | public function getAfter() |
|
297 | |||
298 | /** |
||
299 | * Sets the 'ON UPDATE' mysql column function. |
||
300 | * |
||
301 | * @param string $update On Update function |
||
302 | * @return Column |
||
303 | */ |
||
304 | 15 | public function setUpdate($update) |
|
309 | |||
310 | /** |
||
311 | * Returns the value of the ON UPDATE column function. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 145 | public function getUpdate() |
|
319 | |||
320 | /** |
||
321 | * Sets the column precision for decimal. |
||
322 | * |
||
323 | * @param integer $precision |
||
324 | * @return Column |
||
325 | */ |
||
326 | 9 | public function setPrecision($precision) |
|
331 | |||
332 | /** |
||
333 | * Gets the column precision for decimal. |
||
334 | * |
||
335 | * @return integer |
||
336 | */ |
||
337 | 131 | public function getPrecision() |
|
341 | |||
342 | /** |
||
343 | * Sets the column scale for decimal. |
||
344 | * |
||
345 | * @param integer $scale |
||
346 | * @return Column |
||
347 | */ |
||
348 | 9 | public function setScale($scale) |
|
353 | |||
354 | /** |
||
355 | * Gets the column scale for decimal. |
||
356 | * |
||
357 | * @return integer |
||
358 | */ |
||
359 | 2 | public function getScale() |
|
363 | |||
364 | /** |
||
365 | * Sets the column comment. |
||
366 | * |
||
367 | * @param string $comment |
||
368 | * @return Column |
||
369 | */ |
||
370 | 9 | public function setComment($comment) |
|
375 | |||
376 | /** |
||
377 | * Gets the column comment. |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | 198 | public function getComment() |
|
385 | |||
386 | /** |
||
387 | * Sets whether field should be signed. |
||
388 | * |
||
389 | * @param bool $signed |
||
390 | * @return Column |
||
391 | */ |
||
392 | 68 | public function setSigned($signed) |
|
397 | |||
398 | /** |
||
399 | * Gets whether field should be signed. |
||
400 | * |
||
401 | * @return boolean |
||
402 | */ |
||
403 | 89 | public function getSigned() |
|
407 | |||
408 | /** |
||
409 | * Should the column be signed? |
||
410 | * |
||
411 | * @return boolean |
||
412 | */ |
||
413 | 89 | public function isSigned() |
|
417 | |||
418 | /** |
||
419 | * Sets whether the field should have a timezone identifier. |
||
420 | * Used for date/time columns only! |
||
421 | * |
||
422 | * @param bool $timezone |
||
423 | * @return Column |
||
424 | */ |
||
425 | 1 | public function setTimezone($timezone) |
|
430 | |||
431 | /** |
||
432 | * Gets whether field has a timezone identifier. |
||
433 | * |
||
434 | * @return boolean |
||
435 | */ |
||
436 | 68 | public function getTimezone() |
|
440 | |||
441 | /** |
||
442 | * Should the column have a timezone? |
||
443 | * |
||
444 | * @return boolean |
||
445 | */ |
||
446 | 68 | public function isTimezone() |
|
450 | |||
451 | /** |
||
452 | * Sets field properties. |
||
453 | * |
||
454 | * @param array $properties |
||
455 | * |
||
456 | * @return Column |
||
457 | */ |
||
458 | public function setProperties($properties) |
||
463 | |||
464 | /** |
||
465 | * Gets field properties |
||
466 | * |
||
467 | * @return array |
||
468 | */ |
||
469 | public function getProperties() |
||
473 | |||
474 | /** |
||
475 | * Sets field values. |
||
476 | * |
||
477 | * @param mixed (array|string) $values |
||
478 | * |
||
479 | * @return Column |
||
480 | */ |
||
481 | 9 | public function setValues($values) |
|
489 | |||
490 | /** |
||
491 | * Gets field values |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | 131 | public function getValues() |
|
499 | |||
500 | /** |
||
501 | * Sets the column collation. |
||
502 | * |
||
503 | * @param string $collation |
||
504 | * |
||
505 | * @throws \UnexpectedValueException If collation not allowed for type |
||
506 | * @return $this |
||
507 | */ |
||
508 | 1 | View Code Duplication | public function setCollation($collation) |
|
|||
509 | { |
||
510 | $allowedTypes = [ |
||
511 | 1 | AdapterInterface::PHINX_TYPE_CHAR, |
|
512 | 1 | AdapterInterface::PHINX_TYPE_STRING, |
|
513 | 1 | AdapterInterface::PHINX_TYPE_TEXT, |
|
514 | 1 | ]; |
|
515 | 1 | if (!in_array($this->getType(), $allowedTypes)) { |
|
516 | throw new \UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes)); |
||
517 | } |
||
518 | |||
519 | 1 | $this->collation = $collation; |
|
520 | |||
521 | 1 | return $this; |
|
522 | } |
||
523 | |||
524 | /** |
||
525 | * Gets the column collation. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 89 | public function getCollation() |
|
530 | { |
||
531 | 89 | return $this->collation; |
|
532 | } |
||
533 | |||
534 | /** |
||
535 | * Sets the column character set. |
||
536 | * |
||
537 | * @param string $encoding |
||
538 | * |
||
539 | * @throws \UnexpectedValueException If character set not allowed for type |
||
540 | * @return $this |
||
541 | */ |
||
542 | View Code Duplication | public function setEncoding($encoding) |
|
543 | { |
||
544 | $allowedTypes = [ |
||
545 | AdapterInterface::PHINX_TYPE_CHAR, |
||
546 | AdapterInterface::PHINX_TYPE_STRING, |
||
547 | AdapterInterface::PHINX_TYPE_TEXT, |
||
548 | ]; |
||
549 | if (!in_array($this->getType(), $allowedTypes)) { |
||
550 | throw new \UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes)); |
||
551 | } |
||
552 | |||
553 | $this->encoding = $encoding; |
||
554 | |||
555 | return $this; |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Gets the column character set. |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | 89 | public function getEncoding() |
|
564 | { |
||
565 | 89 | return $this->encoding; |
|
566 | } |
||
567 | |||
568 | /** |
||
569 | * Gets all allowed options. Each option must have a corresponding `setFoo` method. |
||
570 | * |
||
571 | * @return array |
||
572 | */ |
||
573 | 209 | protected function getValidOptions() |
|
593 | |||
594 | /** |
||
595 | * Gets all aliased options. Each alias must reference a valid option. |
||
596 | * |
||
597 | * @return array |
||
598 | */ |
||
599 | 209 | protected function getAliasedOptions() |
|
605 | |||
606 | /** |
||
607 | * Utility method that maps an array of column options to this objects methods. |
||
608 | * |
||
609 | * @param array $options Options |
||
610 | * @return Column |
||
611 | */ |
||
612 | 209 | public function setOptions($options) |
|
632 | } |
||
633 |
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.