Complex classes like CratePlatform 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 CratePlatform, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class CratePlatform extends AbstractPlatform |
||
| 39 | { |
||
| 40 | |||
| 41 | const TIMESTAMP_FORMAT = 'Y-m-d\TH:i:s'; |
||
| 42 | const TIMESTAMP_FORMAT_TZ = 'Y-m-d\TH:i:sO'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | */ |
||
| 47 | 103 | public function __construct() |
|
| 48 | { |
||
| 49 | 103 | parent::__construct(); |
|
| 50 | 103 | $this->initializeDoctrineTypeMappings(); |
|
| 51 | 103 | if (!Type::hasType(MapType::NAME)) { |
|
| 52 | Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType'); |
||
| 53 | } |
||
| 54 | 103 | if (!Type::hasType(TimestampType::NAME)) { |
|
| 55 | Type::addType(TimestampType::NAME, 'Crate\DBAL\Types\TimestampType'); |
||
| 56 | } |
||
| 57 | 103 | Type::overrideType('array', 'Crate\DBAL\Types\ArrayType'); |
|
| 58 | 103 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | 1 | public function getSubstringExpression($value, $from = 0, $length = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritDoc} |
||
| 74 | */ |
||
| 75 | 1 | public function getNowExpression() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritDoc} |
||
| 82 | */ |
||
| 83 | 1 | public function getRegexpExpression() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritDoc} |
||
| 90 | */ |
||
| 91 | 1 | public function getDateDiffExpression($date1, $date2) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | */ |
||
| 99 | 2 | public function supportsSequences() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * If we want to support Schemas, we need to implement |
||
| 106 | * getListNamespacesSQL and getCreateSchemaSQL methods |
||
| 107 | * |
||
| 108 | * {@inheritDoc} |
||
| 109 | */ |
||
| 110 | 2 | public function supportsSchemas() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritDoc} |
||
| 117 | */ |
||
| 118 | 1 | public function supportsIdentityColumns() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritDoc} |
||
| 125 | */ |
||
| 126 | 1 | public function supportsIndexes() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritDoc} |
||
| 133 | */ |
||
| 134 | 55 | public function supportsCommentOnStatement() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritDoc} |
||
| 141 | */ |
||
| 142 | 7 | public function supportsForeignKeyConstraints() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritDoc} |
||
| 149 | */ |
||
| 150 | 1 | public function supportsForeignKeyOnUpdate() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritDoc} |
||
| 157 | */ |
||
| 158 | 1 | public function supportsViews() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritDoc} |
||
| 165 | */ |
||
| 166 | 1 | public function prefersSequences() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritDoc} |
||
| 173 | */ |
||
| 174 | 1 | public function getListDatabasesSQL() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritDoc} |
||
| 181 | */ |
||
| 182 | 12 | public function getListTablesSQL() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritDoc} |
||
| 190 | */ |
||
| 191 | 3 | public function getListTableColumnsSQL($table, $database = null) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritDoc} |
||
| 204 | */ |
||
| 205 | 3 | public function getListTableConstraintsSQL($table, $database = null) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritDoc} |
||
| 218 | */ |
||
| 219 | 5 | public function getAlterTableSQL(TableDiff $diff) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritDoc} |
||
| 262 | */ |
||
| 263 | 58 | public function getColumnDeclarationSQL($name, array $field) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Generate table index column declaration |
||
| 277 | * @codeCoverageIgnore |
||
| 278 | */ |
||
| 279 | public function getIndexDeclarationSQL($name, Index $index) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritDoc} |
||
| 293 | * |
||
| 294 | * Crate wants boolean values converted to the strings 'true'/'false'. |
||
| 295 | */ |
||
| 296 | 2 | public function convertBooleans($item) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritDoc} |
||
| 319 | */ |
||
| 320 | 1 | public function getBooleanTypeDeclarationSQL(array $field) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritDoc} |
||
| 327 | */ |
||
| 328 | 53 | public function getIntegerTypeDeclarationSQL(array $field) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritDoc} |
||
| 335 | */ |
||
| 336 | 1 | public function getBigIntTypeDeclarationSQL(array $field) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritDoc} |
||
| 343 | */ |
||
| 344 | 1 | public function getSmallIntTypeDeclarationSQL(array $field) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritDoc} |
||
| 351 | */ |
||
| 352 | 34 | public function getFloatDeclarationSQL(array $field) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritDoc} |
||
| 359 | */ |
||
| 360 | public function getDecimalTypeDeclarationSQL(array $columnDef) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritDoc} |
||
| 367 | */ |
||
| 368 | 24 | public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritDoc} |
||
| 375 | */ |
||
| 376 | 1 | public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritDoc} |
||
| 383 | */ |
||
| 384 | 1 | public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritDoc} |
||
| 391 | */ |
||
| 392 | 1 | public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritDoc} |
||
| 399 | */ |
||
| 400 | protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritDoc} |
||
| 407 | */ |
||
| 408 | 52 | protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritDoc} |
||
| 415 | */ |
||
| 416 | 1 | public function getClobTypeDeclarationSQL(array $field) |
|
| 417 | { |
||
| 418 | 1 | return 'STRING'; |
|
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets the SQL snippet used to declare an OBJECT column type. |
||
| 423 | * |
||
| 424 | * @param array $field |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 36 | public function getMapTypeDeclarationSQL(array $field, array $options) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Gets the SQL snippet used to declare an ARRAY column type. |
||
| 445 | * |
||
| 446 | * @param array $field |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 4 | public function getArrayTypeDeclarationSQL(array $field, array $options) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * {@inheritDoc} |
||
| 458 | */ |
||
| 459 | 2 | public function getName() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * {@inheritDoc} |
||
| 466 | * |
||
| 467 | * PostgreSQL returns all column names in SQL result sets in lowercase. |
||
| 468 | */ |
||
| 469 | 1 | public function getSQLResultCasing($column) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritDoc} |
||
| 476 | */ |
||
| 477 | 2 | public function getDateTimeTzFormatString() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * {@inheritDoc} |
||
| 484 | */ |
||
| 485 | 5 | public function getDateTimeFormatString() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * {@inheritDoc} |
||
| 492 | */ |
||
| 493 | 1 | public function getDateFormatString() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * {@inheritDoc} |
||
| 500 | */ |
||
| 501 | 1 | public function getTimeFormatString() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * {@inheritDoc} |
||
| 508 | */ |
||
| 509 | 1 | public function getTruncateTableSQL($tableName, $cascade = false) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * {@inheritDoc} |
||
| 516 | */ |
||
| 517 | 1 | public function getReadLockSQL() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * {@inheritDoc} |
||
| 524 | */ |
||
| 525 | 103 | protected function initializeDoctrineTypeMappings() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * {@inheritDoc} |
||
| 550 | */ |
||
| 551 | 52 | public function getVarcharMaxLength() |
|
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritDoc} |
||
| 558 | */ |
||
| 559 | 23 | protected function getReservedKeywordsClass() |
|
| 563 | |||
| 564 | /** |
||
| 565 | * {@inheritDoc} |
||
| 566 | */ |
||
| 567 | 1 | public function getBlobTypeDeclarationSQL(array $field) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * {@inheritDoc} |
||
| 574 | * Gets the SQL statement(s) to create a table with the specified name, columns and constraints |
||
| 575 | * on this platform. |
||
| 576 | * |
||
| 577 | * @param Table $table The name of the table. |
||
| 578 | * @param integer $createFlags |
||
| 579 | * |
||
| 580 | * @return array The sequence of SQL statements. |
||
| 581 | */ |
||
| 582 | 55 | public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES) |
|
| 583 | { |
||
| 584 | 55 | if (!is_int($createFlags)) { |
|
| 585 | $msg = "Second argument of CratePlatform::getCreateTableSQL() has to be integer."; |
||
| 586 | throw new \InvalidArgumentException($msg); |
||
| 587 | } |
||
| 588 | |||
| 589 | 55 | if (count($table->getColumns()) === 0) { |
|
| 590 | 2 | throw DBALException::noColumnsSpecifiedForTable($table->getName()); |
|
| 591 | } |
||
| 592 | |||
| 593 | 53 | $tableName = $table->getQuotedName($this); |
|
| 594 | 53 | $options = $table->getOptions(); |
|
| 595 | 53 | $options['uniqueConstraints'] = array(); |
|
| 596 | 53 | $options['indexes'] = array(); |
|
| 597 | 53 | $options['primary'] = array(); |
|
| 598 | |||
| 599 | 53 | if (($createFlags&self::CREATE_INDEXES) > 0) { |
|
| 600 | 53 | foreach ($table->getIndexes() as $index) { |
|
| 601 | /* @var $index Index */ |
||
| 602 | 39 | if ($index->isPrimary()) { |
|
| 603 | 37 | $platform = $this; |
|
| 604 | 37 | $options['primary'] = array_map(function ($columnName) use ($table, $platform) { |
|
| 605 | 37 | return $table->getColumn($columnName)->getQuotedName($platform); |
|
| 606 | 37 | }, $index->getColumns()); |
|
| 607 | 37 | $options['primary_index'] = $index; |
|
| 608 | 37 | } else { |
|
| 609 | 2 | $options['indexes'][$index->getName()] = $index; |
|
| 610 | } |
||
| 611 | 53 | } |
|
| 612 | 53 | } |
|
| 613 | |||
| 614 | 53 | $columnSql = array(); |
|
| 615 | 53 | $columns = array(); |
|
| 616 | |||
| 617 | 53 | foreach ($table->getColumns() as $column) { |
|
| 618 | 53 | if (null !== $this->_eventManager && |
|
| 619 | 53 | $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) { |
|
| 620 | |||
| 621 | 1 | $eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this); |
|
| 622 | 1 | $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs); |
|
| 623 | |||
| 624 | 1 | $columnSql = array_merge($columnSql, $eventArgs->getSql()); |
|
| 625 | |||
| 626 | 1 | if ($eventArgs->isDefaultPrevented()) { |
|
| 627 | continue; |
||
| 628 | } |
||
| 629 | 1 | } |
|
| 630 | 53 | $columns[$column->getQuotedName($this)] = $this->prepareColumnData($column, $options['primary']); |
|
| 631 | 53 | } |
|
| 632 | |||
| 633 | 53 | if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) { |
|
| 634 | 1 | $eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this); |
|
| 635 | 1 | $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs); |
|
| 636 | |||
| 637 | 1 | if ($eventArgs->isDefaultPrevented()) { |
|
| 638 | return array_merge($eventArgs->getSql(), $columnSql); |
||
| 639 | } |
||
| 640 | 1 | } |
|
| 641 | |||
| 642 | 53 | $sql = $this->_getCreateTableSQL($tableName, $columns, $options); |
|
| 643 | 53 | if ($this->supportsCommentOnStatement()) { |
|
| 644 | foreach ($table->getColumns() as $column) { |
||
| 645 | if ($this->getColumnComment($column)) { |
||
| 646 | $sql[] = $this->getCommentOnColumnSQL( |
||
| 647 | $tableName, |
||
| 648 | $column->getName(), |
||
| 649 | $this->getColumnComment($column) |
||
| 650 | ); |
||
| 651 | } |
||
| 652 | } |
||
| 653 | } |
||
| 654 | |||
| 655 | 53 | return array_merge($sql, $columnSql); |
|
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * {@inheritDoc} |
||
| 660 | */ |
||
| 661 | 53 | protected function _getCreateTableSQL($tableName, array $columns, array $options = array()) |
|
| 662 | { |
||
| 663 | 53 | $columnListSql = $this->getColumnDeclarationListSQL($columns); |
|
| 664 | |||
| 665 | 53 | if (isset($options['primary']) && ! empty($options['primary'])) { |
|
| 666 | 37 | $keyColumns = array_unique(array_values($options['primary'])); |
|
| 667 | 37 | $columnListSql .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
|
| 668 | 37 | } |
|
| 669 | |||
| 670 | 53 | if (isset($options['indexes']) && ! empty($options['indexes'])) { |
|
| 671 | 2 | foreach ($options['indexes'] as $index => $definition) { |
|
| 672 | 2 | $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition); |
|
| 673 | 2 | } |
|
| 674 | 2 | } |
|
| 675 | |||
| 676 | 53 | if (isset($options['foreignKeys'])) { |
|
| 677 | throw DBALException::notSupported("Create Table: foreign keys"); |
||
| 678 | } |
||
| 679 | |||
| 680 | 53 | $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql . ')'; |
|
| 681 | 53 | return array($query); |
|
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param \Doctrine\DBAL\Schema\Column $column The name of the table. |
||
| 686 | * @param array $primaries |
||
| 687 | * |
||
| 688 | * @return array The column data as associative array. |
||
| 689 | */ |
||
| 690 | 55 | public function prepareColumnData($column, $primaries = array()) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * {@inheritDoc} |
||
| 724 | */ |
||
| 725 | 1 | public function getCreateDatabaseSQL($database) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * {@inheritDoc} |
||
| 732 | */ |
||
| 733 | 1 | public function getDropDatabaseSQL($database) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * {@inheritDoc} |
||
| 740 | */ |
||
| 741 | 1 | public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table) |
|
| 745 | |||
| 746 | /** |
||
| 747 | * {@inheritDoc} |
||
| 748 | */ |
||
| 749 | 1 | public function getGuidTypeDeclarationSQL(array $field) |
|
| 753 | } |
||
| 754 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.