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|\Phinx\Util\Literal | ||
| 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 $scale; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * @var string | ||
| 76 | */ | ||
| 77 | protected $after; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * @var string | ||
| 81 | */ | ||
| 82 | protected $update; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * @var string | ||
| 86 | */ | ||
| 87 | protected $comment; | ||
| 88 | |||
| 89 | /** | ||
| 90 | * @var boolean | ||
| 91 | */ | ||
| 92 | protected $signed = true; | ||
| 93 | |||
| 94 | /** | ||
| 95 | * @var boolean | ||
| 96 | */ | ||
| 97 | protected $timezone = false; | ||
| 98 | |||
| 99 | /** | ||
| 100 | * @var array | ||
| 101 | */ | ||
| 102 | protected $properties = []; | ||
| 103 | |||
| 104 | /** | ||
| 105 | * @var string | ||
| 106 | */ | ||
| 107 | protected $collation; | ||
| 108 | |||
| 109 | /** | ||
| 110 | * @var string | ||
| 111 | */ | ||
| 112 | protected $encoding; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @var array | ||
| 116 | */ | ||
| 117 | protected $values; | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Sets the column name. | ||
| 121 | * | ||
| 122 | * @param string $name | ||
| 123 | * @return \Phinx\Db\Table\Column | ||
| 124 | */ | ||
| 125 | public function setName($name) | ||
| 131 | |||
| 132 | 215 | /** | |
| 133 | 215 | * Gets the column name. | |
| 134 | * | ||
| 135 | * @return string|null | ||
| 136 | */ | ||
| 137 | public function getName() | ||
| 141 | 212 | ||
| 142 | /** | ||
| 143 | 212 | * Sets the column type. | |
| 144 | * | ||
| 145 | * @param string|\Phinx\Util\Literal $type Column type | ||
| 146 | * @return \Phinx\Db\Table\Column | ||
| 147 | */ | ||
| 148 | public function setType($type) | ||
| 154 | 214 | ||
| 155 | 214 | /** | |
| 156 | * Gets the column type. | ||
| 157 | * | ||
| 158 | * @return string|\Phinx\Util\Literal | ||
| 159 | */ | ||
| 160 | public function getType() | ||
| 164 | |||
| 165 | 212 | /** | |
| 166 | * Sets the column limit. | ||
| 167 | * | ||
| 168 | * @param int $limit | ||
| 169 | * @return \Phinx\Db\Table\Column | ||
| 170 | */ | ||
| 171 | public function setLimit($limit) | ||
| 177 | 192 | ||
| 178 | /** | ||
| 179 | * Gets the column limit. | ||
| 180 | * | ||
| 181 | * @return int | ||
| 182 | */ | ||
| 183 | public function getLimit() | ||
| 187 | 192 | ||
| 188 | /** | ||
| 189 | * Sets whether the column allows nulls. | ||
| 190 | * | ||
| 191 | * @param bool $null | ||
| 192 | * @return \Phinx\Db\Table\Column | ||
| 193 | */ | ||
| 194 | public function setNull($null) | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Gets whether the column allows nulls. | ||
| 203 | * | ||
| 204 | * @return bool | ||
| 205 | */ | ||
| 206 | public function getNull() | ||
| 210 | |||
| 211 | /** | ||
| 212 | * Does the column allow nulls? | ||
| 213 | * | ||
| 214 | * @return bool | ||
| 215 | */ | ||
| 216 | public function isNull() | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Sets the default column value. | ||
| 223 | * | ||
| 224 | * @param mixed $default | ||
| 225 | * @return \Phinx\Db\Table\Column | ||
| 226 | */ | ||
| 227 | 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 bool $identity | ||
| 248 | * @return \Phinx\Db\Table\Column | ||
| 249 | */ | ||
| 250 | 158 | public function setIdentity($identity) | |
| 256 | |||
| 257 | /** | ||
| 258 | * Gets whether or not the column is an identity column. | ||
| 259 | * | ||
| 260 | * @return bool | ||
| 261 | 199 | */ | |
| 262 | public function getIdentity() | ||
| 266 | |||
| 267 | /** | ||
| 268 | * Is the column an identity column? | ||
| 269 | * | ||
| 270 | * @return bool | ||
| 271 | 198 | */ | |
| 272 | public function isIdentity() | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Sets the name of the column to add this column after. | ||
| 279 | * | ||
| 280 | * @param string $after After | ||
| 281 | * @return \Phinx\Db\Table\Column | ||
| 282 | 1 | */ | |
| 283 | public function setAfter($after) | ||
| 289 | |||
| 290 | /** | ||
| 291 | * Returns the name of the column to add this column after. | ||
| 292 | * | ||
| 293 | 20 | * @return string | |
| 294 | */ | ||
| 295 | 20 | public function getAfter() | |
| 299 | |||
| 300 | /** | ||
| 301 | * Sets the 'ON UPDATE' mysql column function. | ||
| 302 | * | ||
| 303 | * @param string $update On Update function | ||
| 304 | 15 | * @return \Phinx\Db\Table\Column | |
| 305 | */ | ||
| 306 | 15 | public function setUpdate($update) | |
| 312 | |||
| 313 | /** | ||
| 314 | * Returns the value of the ON UPDATE column function. | ||
| 315 | 145 | * | |
| 316 | * @return string | ||
| 317 | 145 | */ | |
| 318 | public function getUpdate() | ||
| 322 | |||
| 323 | /** | ||
| 324 | * Sets the number precision for decimal or float column. | ||
| 325 | * | ||
| 326 | 9 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, | |
| 327 | * and the column could store value from -999.99 to 999.99. | ||
| 328 | 9 | * | |
| 329 | 9 | * @param int $precision Number precision | |
| 330 | * @return \Phinx\Db\Table\Column | ||
| 331 | */ | ||
| 332 | public function setPrecision($precision) | ||
| 338 | |||
| 339 | 131 | /** | |
| 340 | * Gets the number precision for decimal or float column. | ||
| 341 | * | ||
| 342 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, | ||
| 343 | * and the column could store value from -999.99 to 999.99. | ||
| 344 | * | ||
| 345 | * @return int | ||
| 346 | */ | ||
| 347 | public function getPrecision() | ||
| 351 | 9 | ||
| 352 | /** | ||
| 353 | * Gets the column identity seed. | ||
| 354 | * | ||
| 355 | * @return int | ||
| 356 | */ | ||
| 357 | public function getSeed() | ||
| 361 | 2 | ||
| 362 | /** | ||
| 363 | * Gets the column identity increment. | ||
| 364 | * | ||
| 365 | * @return int | ||
| 366 | */ | ||
| 367 | public function getIncrement() | ||
| 371 | |||
| 372 | 9 | /** | |
| 373 | 9 | * Sets the column identity seed. | |
| 374 | * | ||
| 375 | * @param int $seed | ||
| 376 | * @return \Phinx\Db\Table\Column | ||
| 377 | */ | ||
| 378 | public function setSeed($seed) | ||
| 384 | |||
| 385 | /** | ||
| 386 | * Sets the column identity increment. | ||
| 387 | * | ||
| 388 | * @param int $increment | ||
| 389 | * @return \Phinx\Db\Table\Column | ||
| 390 | */ | ||
| 391 | public function setIncrement($increment) | ||
| 397 | |||
| 398 | /** | ||
| 399 | * Sets the number scale for decimal or float column. | ||
| 400 | * | ||
| 401 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, | ||
| 402 | * and the column could store value from -999.99 to 999.99. | ||
| 403 | 89 | * | |
| 404 | * @param int $scale Number scale | ||
| 405 | 89 | * @return \Phinx\Db\Table\Column | |
| 406 | */ | ||
| 407 | public function setScale($scale) | ||
| 413 | 89 | ||
| 414 | /** | ||
| 415 | 89 | * Gets the number scale for decimal or float column. | |
| 416 | * | ||
| 417 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, | ||
| 418 | * and the column could store value from -999.99 to 999.99. | ||
| 419 | * | ||
| 420 | * @return int | ||
| 421 | */ | ||
| 422 | public function getScale() | ||
| 426 | |||
| 427 | 1 | /** | |
| 428 | 1 | * Sets the number precision and scale for decimal or float column. | |
| 429 | * | ||
| 430 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, | ||
| 431 | * and the column could store value from -999.99 to 999.99. | ||
| 432 | * | ||
| 433 | * @param int $precision Number precision | ||
| 434 | * @param int $scale Number scale | ||
| 435 | * @return \Phinx\Db\Table\Column | ||
| 436 | 68 | */ | |
| 437 | public function setPrecisionAndScale($precision, $scale) | ||
| 444 | |||
| 445 | /** | ||
| 446 | 68 | * Sets the column comment. | |
| 447 | * | ||
| 448 | 68 | * @param string $comment | |
| 449 | * @return \Phinx\Db\Table\Column | ||
| 450 | */ | ||
| 451 | public function setComment($comment) | ||
| 457 | |||
| 458 | /** | ||
| 459 | * Gets the column comment. | ||
| 460 | * | ||
| 461 | * @return string | ||
| 462 | */ | ||
| 463 | public function getComment() | ||
| 467 | |||
| 468 | /** | ||
| 469 | * Sets whether field should be signed. | ||
| 470 | * | ||
| 471 | * @param bool $signed | ||
| 472 | * @return \Phinx\Db\Table\Column | ||
| 473 | */ | ||
| 474 | public function setSigned($signed) | ||
| 480 | |||
| 481 | 9 | /** | |
| 482 | * Gets whether field should be signed. | ||
| 483 | 9 | * | |
| 484 | 2 | * @return bool | |
| 485 | 2 | */ | |
| 486 | 9 | public function getSigned() | |
| 490 | |||
| 491 | /** | ||
| 492 | * Should the column be signed? | ||
| 493 | * | ||
| 494 | * @return bool | ||
| 495 | 131 | */ | |
| 496 | public function isSigned() | ||
| 500 | |||
| 501 | /** | ||
| 502 | * Sets whether the field should have a timezone identifier. | ||
| 503 | * Used for date/time columns only! | ||
| 504 | * | ||
| 505 | * @param bool $timezone | ||
| 506 | * @return \Phinx\Db\Table\Column | ||
| 507 | */ | ||
| 508 | 1 | public function setTimezone($timezone) | |
| 514 | 1 | ||
| 515 | 1 | /** | |
| 516 | * Gets whether field has a timezone identifier. | ||
| 517 | * | ||
| 518 | * @return bool | ||
| 519 | 1 | */ | |
| 520 | public function getTimezone() | ||
| 524 | |||
| 525 | /** | ||
| 526 | * Should the column have a timezone? | ||
| 527 | * | ||
| 528 | * @return bool | ||
| 529 | 89 | */ | |
| 530 | public function isTimezone() | ||
| 534 | |||
| 535 | /** | ||
| 536 | * Sets field properties. | ||
| 537 | * | ||
| 538 | * @param array $properties | ||
| 539 | * | ||
| 540 | * @return \Phinx\Db\Table\Column | ||
| 541 | */ | ||
| 542 | public function setProperties($properties) | ||
| 548 | |||
| 549 | /** | ||
| 550 | * Gets field properties | ||
| 551 | * | ||
| 552 | * @return array | ||
| 553 | */ | ||
| 554 | public function getProperties() | ||
| 558 | |||
| 559 | /** | ||
| 560 | * Sets field values. | ||
| 561 | * | ||
| 562 | * @param array|string $values | ||
| 563 | 89 | * | |
| 564 | * @return \Phinx\Db\Table\Column | ||
| 565 | 89 | */ | |
| 566 | public function setValues($values) | ||
| 575 | |||
| 576 | 209 | /** | |
| 577 | 209 | * Gets field values | |
| 578 | 209 | * | |
| 579 | 209 | * @return array | |
| 580 | 209 | */ | |
| 581 | 209 | public function getValues() | |
| 585 | 209 | ||
| 586 | 209 | /** | |
| 587 | 209 | * Sets the column collation. | |
| 588 | 209 | * | |
| 589 | 209 | * @param string $collation | |
| 590 | 209 | * | |
| 591 | 209 | * @throws \UnexpectedValueException If collation not allowed for type | |
| 592 | * @return $this | ||
| 593 | */ | ||
| 594 | View Code Duplication | public function setCollation($collation) | |
| 595 |     { | ||
| 596 | $allowedTypes = [ | ||
| 597 | AdapterInterface::PHINX_TYPE_CHAR, | ||
| 598 | AdapterInterface::PHINX_TYPE_STRING, | ||
| 599 | 209 | AdapterInterface::PHINX_TYPE_TEXT, | |
| 600 | ]; | ||
| 601 |         if (!in_array($this->getType(), $allowedTypes)) { | ||
| 602 | 209 |             throw new \UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes)); | |
| 603 | 209 | } | |
| 604 | |||
| 605 | $this->collation = $collation; | ||
| 606 | |||
| 607 | return $this; | ||
| 608 | } | ||
| 609 | |||
| 610 | /** | ||
| 611 | * Gets the column collation. | ||
| 612 | 209 | * | |
| 613 | * @return string | ||
| 614 | 209 | */ | |
| 615 | 209 | public function getCollation() | |
| 616 |     { | ||
| 617 | 209 | return $this->collation; | |
| 618 | 208 | } | |
| 619 | |||
| 620 | /** | ||
| 621 | * Sets the column character set. | ||
| 622 | * | ||
| 623 | 208 | * @param string $encoding | |
| 624 | 1 | * | |
| 625 | * @throws \UnexpectedValueException If character set not allowed for type | ||
| 626 | * @return $this | ||
| 627 | 207 | */ | |
| 628 | 207 | View Code Duplication | public function setEncoding($encoding) | 
| 629 | 208 |     { | |
| 630 | 208 | $allowedTypes = [ | |
| 631 | AdapterInterface::PHINX_TYPE_CHAR, | ||
| 632 | AdapterInterface::PHINX_TYPE_STRING, | ||
| 633 | AdapterInterface::PHINX_TYPE_TEXT, | ||
| 634 | ]; | ||
| 635 |         if (!in_array($this->getType(), $allowedTypes)) { | ||
| 636 |             throw new \UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes)); | ||
| 637 | } | ||
| 638 | |||
| 639 | $this->encoding = $encoding; | ||
| 640 | |||
| 641 | return $this; | ||
| 642 | } | ||
| 643 | |||
| 644 | /** | ||
| 645 | * Gets the column character set. | ||
| 646 | * | ||
| 647 | * @return string | ||
| 648 | */ | ||
| 649 | public function getEncoding() | ||
| 650 |     { | ||
| 651 | return $this->encoding; | ||
| 652 | } | ||
| 653 | |||
| 654 | /** | ||
| 655 | * Gets all allowed options. Each option must have a corresponding `setFoo` method. | ||
| 656 | * | ||
| 657 | * @return array | ||
| 658 | */ | ||
| 659 | protected function getValidOptions() | ||
| 681 | |||
| 682 | /** | ||
| 683 | * Gets all aliased options. Each alias must reference a valid option. | ||
| 684 | * | ||
| 685 | * @return array | ||
| 686 | */ | ||
| 687 | protected function getAliasedOptions() | ||
| 694 | |||
| 695 | /** | ||
| 696 | * Utility method that maps an array of column options to this objects methods. | ||
| 697 | * | ||
| 698 | * @param array $options Options | ||
| 699 | * @return \Phinx\Db\Table\Column | ||
| 700 | */ | ||
| 701 | public function setOptions($options) | ||
| 722 | } | ||
| 723 | 
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: