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 |
||
| 17 | class Column |
||
| 18 | { |
||
| 19 | public const BIGINTEGER = AdapterInterface::PHINX_TYPE_BIG_INTEGER; |
||
| 20 | public const SMALLINTEGER = AdapterInterface::PHINX_TYPE_SMALL_INTEGER; |
||
| 21 | public const TINYINTEGER = AdapterInterface::PHINX_TYPE_TINY_INTEGER; |
||
| 22 | public const BINARY = AdapterInterface::PHINX_TYPE_BINARY; |
||
| 23 | public const BOOLEAN = AdapterInterface::PHINX_TYPE_BOOLEAN; |
||
| 24 | public const CHAR = AdapterInterface::PHINX_TYPE_CHAR; |
||
| 25 | public const DATE = AdapterInterface::PHINX_TYPE_DATE; |
||
| 26 | public const DATETIME = AdapterInterface::PHINX_TYPE_DATETIME; |
||
| 27 | public const DECIMAL = AdapterInterface::PHINX_TYPE_DECIMAL; |
||
| 28 | public const FLOAT = AdapterInterface::PHINX_TYPE_FLOAT; |
||
| 29 | public const INTEGER = AdapterInterface::PHINX_TYPE_INTEGER; |
||
| 30 | public const STRING = AdapterInterface::PHINX_TYPE_STRING; |
||
| 31 | public const TEXT = AdapterInterface::PHINX_TYPE_TEXT; |
||
| 32 | public const TIME = AdapterInterface::PHINX_TYPE_TIME; |
||
| 33 | public const TIMESTAMP = AdapterInterface::PHINX_TYPE_TIMESTAMP; |
||
| 34 | public const UUID = AdapterInterface::PHINX_TYPE_UUID; |
||
| 35 | public const BINARYUUID = AdapterInterface::PHINX_TYPE_BINARYUUID; |
||
| 36 | /** MySQL-only column type */ |
||
| 37 | public const ENUM = AdapterInterface::PHINX_TYPE_ENUM; |
||
| 38 | /** MySQL-only column type */ |
||
| 39 | public const SET = AdapterInterface::PHINX_TYPE_STRING; |
||
| 40 | /** MySQL-only column type */ |
||
| 41 | public const BLOB = AdapterInterface::PHINX_TYPE_BLOB; |
||
| 42 | /** MySQL-only column type */ |
||
| 43 | public const YEAR = AdapterInterface::PHINX_TYPE_YEAR; |
||
| 44 | /** MySQL/Postgres-only column type */ |
||
| 45 | public const JSON = AdapterInterface::PHINX_TYPE_JSON; |
||
| 46 | /** Postgres-only column type */ |
||
| 47 | public const JSONB = AdapterInterface::PHINX_TYPE_JSONB; |
||
| 48 | /** Postgres-only column type */ |
||
| 49 | public const CIDR = AdapterInterface::PHINX_TYPE_CIDR; |
||
| 50 | /** Postgres-only column type */ |
||
| 51 | public const INET = AdapterInterface::PHINX_TYPE_INET; |
||
| 52 | /** Postgres-only column type */ |
||
| 53 | public const MACADDR = AdapterInterface::PHINX_TYPE_MACADDR; |
||
| 54 | /** Postgres-only column type */ |
||
| 55 | public const INTERVAL = AdapterInterface::PHINX_TYPE_INTERVAL; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $name; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string|\Phinx\Util\Literal |
||
| 64 | */ |
||
| 65 | protected $type; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $limit; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $null = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var mixed|null |
||
| 79 | */ |
||
| 80 | protected $default; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $identity = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | protected $seed; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $increment; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | protected $scale; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $after; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $update; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $comment; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $signed = true; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | protected $timezone = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | 215 | protected $properties = []; |
|
| 131 | |||
| 132 | 215 | /** |
|
| 133 | 215 | * @var string |
|
| 134 | */ |
||
| 135 | protected $collation; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $encoding; |
||
| 141 | 212 | ||
| 142 | /** |
||
| 143 | 212 | * @var int|null |
|
| 144 | */ |
||
| 145 | protected $srid; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var array |
||
| 149 | */ |
||
| 150 | protected $values; |
||
| 151 | |||
| 152 | 214 | /** |
|
| 153 | * Sets the column name. |
||
| 154 | 214 | * |
|
| 155 | 214 | * @param string $name Name |
|
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function setName($name) |
||
| 165 | 212 | ||
| 166 | /** |
||
| 167 | * Gets the column name. |
||
| 168 | * |
||
| 169 | * @return string|null |
||
| 170 | */ |
||
| 171 | public function getName() |
||
| 175 | |||
| 176 | 192 | /** |
|
| 177 | 192 | * Sets the column type. |
|
| 178 | * |
||
| 179 | * @param string|\Phinx\Util\Literal $type Column type |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function setType($type) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the column type. |
||
| 192 | * |
||
| 193 | * @return string|\Phinx\Util\Literal |
||
| 194 | */ |
||
| 195 | public function getType() |
||
| 199 | 208 | ||
| 200 | /** |
||
| 201 | * Sets the column limit. |
||
| 202 | * |
||
| 203 | * @param int $limit Limit |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | 215 | public function setLimit($limit) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the column limit. |
||
| 216 | * |
||
| 217 | 214 | * @return int |
|
| 218 | */ |
||
| 219 | 214 | public function getLimit() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Sets whether the column allows nulls. |
||
| 226 | * |
||
| 227 | * @param bool $null Null |
||
| 228 | 207 | * |
|
| 229 | * @return $this |
||
| 230 | 207 | */ |
|
| 231 | 207 | public function setNull($null) |
|
| 237 | |||
| 238 | /** |
||
| 239 | 215 | * Gets whether the column allows nulls. |
|
| 240 | * |
||
| 241 | 215 | * @return bool |
|
| 242 | */ |
||
| 243 | public function getNull() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Does the column allow nulls? |
||
| 250 | 158 | * |
|
| 251 | * @return bool |
||
| 252 | 158 | */ |
|
| 253 | 158 | public function isNull() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Sets the default column value. |
||
| 260 | * |
||
| 261 | 199 | * @param mixed $default Default |
|
| 262 | * |
||
| 263 | 199 | * @return $this |
|
| 264 | */ |
||
| 265 | public function setDefault($default) |
||
| 271 | 198 | ||
| 272 | /** |
||
| 273 | 198 | * Gets the default column value. |
|
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function getDefault() |
||
| 281 | |||
| 282 | 1 | /** |
|
| 283 | * Sets whether or not the column is an identity column. |
||
| 284 | 1 | * |
|
| 285 | 1 | * @param bool $identity Identity |
|
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setIdentity($identity) |
||
| 295 | 20 | ||
| 296 | /** |
||
| 297 | * Gets whether or not the column is an identity column. |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function getIdentity() |
||
| 305 | |||
| 306 | 15 | /** |
|
| 307 | 15 | * Is the column an identity column? |
|
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function isIdentity() |
||
| 315 | 145 | ||
| 316 | /** |
||
| 317 | 145 | * Sets the name of the column to add this column after. |
|
| 318 | * |
||
| 319 | * @param string $after After |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function setAfter($after) |
||
| 329 | 9 | ||
| 330 | /** |
||
| 331 | * Returns the name of the column to add this column after. |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getAfter() |
||
| 339 | 131 | ||
| 340 | /** |
||
| 341 | * Sets the 'ON UPDATE' mysql column function. |
||
| 342 | * |
||
| 343 | * @param string $update On Update function |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function setUpdate($update) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the value of the ON UPDATE column function. |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 2 | public function getUpdate() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Sets the number precision for decimal or float column. |
||
| 366 | * |
||
| 367 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, |
||
| 368 | * and the column could store value from -999.99 to 999.99. |
||
| 369 | * |
||
| 370 | 9 | * @param int $precision Number precision |
|
| 371 | * |
||
| 372 | 9 | * @return $this |
|
| 373 | 9 | */ |
|
| 374 | public function setPrecision($precision) |
||
| 380 | |||
| 381 | 198 | /** |
|
| 382 | * Gets the number precision for decimal or float column. |
||
| 383 | 198 | * |
|
| 384 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, |
||
| 385 | * and the column could store value from -999.99 to 999.99. |
||
| 386 | * |
||
| 387 | * @return int |
||
| 388 | */ |
||
| 389 | public function getPrecision() |
||
| 393 | |||
| 394 | 68 | /** |
|
| 395 | 68 | * Gets the column identity seed. |
|
| 396 | * |
||
| 397 | * @return int |
||
| 398 | */ |
||
| 399 | public function getSeed() |
||
| 403 | 89 | ||
| 404 | /** |
||
| 405 | 89 | * Gets the column identity increment. |
|
| 406 | * |
||
| 407 | * @return int |
||
| 408 | */ |
||
| 409 | public function getIncrement() |
||
| 413 | 89 | ||
| 414 | /** |
||
| 415 | 89 | * Sets the column identity seed. |
|
| 416 | * |
||
| 417 | * @param int $seed Number seed |
||
| 418 | * |
||
| 419 | * @return $this |
||
| 420 | */ |
||
| 421 | public function setSeed($seed) |
||
| 427 | 1 | ||
| 428 | 1 | /** |
|
| 429 | * Sets the column identity increment. |
||
| 430 | * |
||
| 431 | * @param int $increment Number increment |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function setIncrement($increment) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Sets the number scale for decimal or float column. |
||
| 444 | * |
||
| 445 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, |
||
| 446 | 68 | * and the column could store value from -999.99 to 999.99. |
|
| 447 | * |
||
| 448 | 68 | * @param int $scale Number scale |
|
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function setScale($scale) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Gets the number scale for decimal or float column. |
||
| 461 | * |
||
| 462 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, |
||
| 463 | * and the column could store value from -999.99 to 999.99. |
||
| 464 | * |
||
| 465 | * @return int |
||
| 466 | */ |
||
| 467 | public function getScale() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Sets the number precision and scale for decimal or float column. |
||
| 474 | * |
||
| 475 | * For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale, |
||
| 476 | * and the column could store value from -999.99 to 999.99. |
||
| 477 | * |
||
| 478 | * @param int $precision Number precision |
||
| 479 | * @param int $scale Number scale |
||
| 480 | * |
||
| 481 | 9 | * @return $this |
|
| 482 | */ |
||
| 483 | 9 | public function setPrecisionAndScale($precision, $scale) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Sets the column comment. |
||
| 493 | * |
||
| 494 | * @param string $comment Comment |
||
| 495 | 131 | * |
|
| 496 | * @return $this |
||
| 497 | 131 | */ |
|
| 498 | public function setComment($comment) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Gets the column comment. |
||
| 507 | * |
||
| 508 | 1 | * @return string |
|
| 509 | */ |
||
| 510 | public function getComment() |
||
| 514 | 1 | ||
| 515 | 1 | /** |
|
| 516 | * Sets whether field should be signed. |
||
| 517 | * |
||
| 518 | * @param bool $signed Signed |
||
| 519 | 1 | * |
|
| 520 | * @return $this |
||
| 521 | 1 | */ |
|
| 522 | public function setSigned($signed) |
||
| 528 | |||
| 529 | 89 | /** |
|
| 530 | * Gets whether field should be signed. |
||
| 531 | 89 | * |
|
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function getSigned() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Should the column be signed? |
||
| 541 | * |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | public function isSigned() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Sets whether the field should have a timezone identifier. |
||
| 551 | * Used for date/time columns only! |
||
| 552 | * |
||
| 553 | * @param bool $timezone Timezone |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function setTimezone($timezone) |
||
| 563 | 89 | ||
| 564 | /** |
||
| 565 | 89 | * Gets whether field has a timezone identifier. |
|
| 566 | * |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function getTimezone() |
||
| 573 | 209 | ||
| 574 | /** |
||
| 575 | * Should the column have a timezone? |
||
| 576 | 209 | * |
|
| 577 | 209 | * @return bool |
|
| 578 | 209 | */ |
|
| 579 | 209 | public function isTimezone() |
|
| 583 | 209 | ||
| 584 | 209 | /** |
|
| 585 | 209 | * Sets field properties. |
|
| 586 | 209 | * |
|
| 587 | 209 | * @param array $properties Properties |
|
| 588 | 209 | * |
|
| 589 | 209 | * @return $this |
|
| 590 | 209 | */ |
|
| 591 | 209 | public function setProperties($properties) |
|
| 597 | |||
| 598 | /** |
||
| 599 | 209 | * Gets field properties |
|
| 600 | * |
||
| 601 | * @return array |
||
| 602 | 209 | */ |
|
| 603 | 209 | public function getProperties() |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Sets field values. |
||
| 610 | * |
||
| 611 | * @param string[]|string $values Value(s) |
||
| 612 | 209 | * |
|
| 613 | * @return $this |
||
| 614 | 209 | */ |
|
| 615 | 209 | public function setValues($values) |
|
| 624 | 1 | ||
| 625 | /** |
||
| 626 | * Gets field values |
||
| 627 | 207 | * |
|
| 628 | 207 | * @return array |
|
| 629 | 208 | */ |
|
| 630 | 208 | public function getValues() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Sets the column collation. |
||
| 637 | * |
||
| 638 | * @param string $collation Collation |
||
| 639 | * |
||
| 640 | * @throws \UnexpectedValueException If collation not allowed for type |
||
| 641 | * |
||
| 642 | * @return $this |
||
| 643 | */ |
||
| 644 | public function setCollation($collation) |
||
| 645 | { |
||
| 646 | $allowedTypes = [ |
||
| 647 | AdapterInterface::PHINX_TYPE_CHAR, |
||
| 648 | AdapterInterface::PHINX_TYPE_STRING, |
||
| 649 | AdapterInterface::PHINX_TYPE_TEXT, |
||
| 650 | ]; |
||
| 651 | if (!in_array($this->getType(), $allowedTypes, true)) { |
||
| 652 | throw new UnexpectedValueException('Collation may be set only for types: ' . implode(', ', $allowedTypes)); |
||
| 653 | } |
||
| 654 | |||
| 655 | $this->collation = $collation; |
||
| 656 | |||
| 657 | return $this; |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Gets the column collation. |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public function getCollation() |
||
| 666 | { |
||
| 667 | return $this->collation; |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Sets the column character set. |
||
| 672 | * |
||
| 673 | * @param string $encoding Encoding |
||
| 674 | * |
||
| 675 | * @throws \UnexpectedValueException If character set not allowed for type |
||
| 676 | * |
||
| 677 | * @return $this |
||
| 678 | */ |
||
| 679 | public function setEncoding($encoding) |
||
| 680 | { |
||
| 681 | $allowedTypes = [ |
||
| 682 | AdapterInterface::PHINX_TYPE_CHAR, |
||
| 683 | AdapterInterface::PHINX_TYPE_STRING, |
||
| 684 | AdapterInterface::PHINX_TYPE_TEXT, |
||
| 685 | ]; |
||
| 686 | if (!in_array($this->getType(), $allowedTypes, true)) { |
||
| 687 | throw new UnexpectedValueException('Character set may be set only for types: ' . implode(', ', $allowedTypes)); |
||
| 688 | } |
||
| 689 | |||
| 690 | $this->encoding = $encoding; |
||
| 691 | |||
| 692 | return $this; |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Gets the column character set. |
||
| 697 | * |
||
| 698 | * @return string |
||
| 699 | */ |
||
| 700 | public function getEncoding() |
||
| 701 | { |
||
| 702 | return $this->encoding; |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Sets the column SRID. |
||
| 707 | * |
||
| 708 | * @param int $srid SRID |
||
| 709 | * @return \Phinx\Db\Table\Column |
||
| 710 | */ |
||
| 711 | public function setSrid($srid) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Gets the column SRID. |
||
| 720 | * |
||
| 721 | * @return int|null |
||
| 722 | */ |
||
| 723 | public function getSrid() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Gets all allowed options. Each option must have a corresponding `setFoo` method. |
||
| 730 | * |
||
| 731 | * @return array |
||
| 732 | */ |
||
| 733 | protected function getValidOptions() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Gets all aliased options. Each alias must reference a valid option. |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | */ |
||
| 761 | protected function getAliasedOptions() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Utility method that maps an array of column options to this objects methods. |
||
| 771 | * |
||
| 772 | * @param array $options Options |
||
| 773 | * |
||
| 774 | * @throws \RuntimeException |
||
| 775 | * |
||
| 776 | * @return $this |
||
| 777 | */ |
||
| 778 | public function setOptions($options) |
||
| 799 | } |
||
| 800 |