Complex classes like SchemaTool 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 SchemaTool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class SchemaTool |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var \Doctrine\ORM\EntityManagerInterface |
||
| 50 | */ |
||
| 51 | private $em; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
| 55 | */ |
||
| 56 | private $platform; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The quote strategy. |
||
| 60 | * |
||
| 61 | * @var \Doctrine\ORM\Mapping\QuoteStrategy |
||
| 62 | */ |
||
| 63 | private $quoteStrategy; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Initializes a new SchemaTool instance that uses the connection of the |
||
| 67 | * provided EntityManager. |
||
| 68 | * |
||
| 69 | * @param \Doctrine\ORM\EntityManagerInterface $em |
||
| 70 | */ |
||
| 71 | 1215 | public function __construct(EntityManagerInterface $em) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Creates the database schema for the given array of ClassMetadata instances. |
||
| 80 | * |
||
| 81 | * @param array $classes |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | * |
||
| 85 | * @throws ToolsException |
||
| 86 | */ |
||
| 87 | 251 | public function createSchema(array $classes) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Gets the list of DDL statements that are required to create the database schema for |
||
| 103 | * the given list of ClassMetadata instances. |
||
| 104 | * |
||
| 105 | * @param array $classes |
||
| 106 | * |
||
| 107 | * @return array The SQL statements needed to create the schema for the classes. |
||
| 108 | */ |
||
| 109 | 251 | public function getCreateSchemaSql(array $classes) |
|
| 110 | { |
||
| 111 | 251 | $schema = $this->getSchemaFromMetadata($classes); |
|
| 112 | |||
| 113 | 251 | return $schema->toSql($this->platform); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Detects instances of ClassMetadata that don't need to be processed in the SchemaTool context. |
||
| 118 | * |
||
| 119 | * @param ClassMetadata $class |
||
| 120 | * @param array $processedClasses |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 259 | private function processingNotRequired($class, array $processedClasses) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Creates a Schema instance from a given set of metadata classes. |
||
| 136 | * |
||
| 137 | * @param array $classes |
||
| 138 | * |
||
| 139 | * @return Schema |
||
| 140 | * |
||
| 141 | * @throws \Doctrine\ORM\ORMException |
||
| 142 | */ |
||
| 143 | 259 | public function getSchemaFromMetadata(array $classes) |
|
| 144 | { |
||
| 145 | // Reminder for processed classes, used for hierarchies |
||
| 146 | 259 | $processedClasses = []; |
|
| 147 | 259 | $eventManager = $this->em->getEventManager(); |
|
| 148 | 259 | $schemaManager = $this->em->getConnection()->getSchemaManager(); |
|
| 149 | 259 | $metadataSchemaConfig = $schemaManager->createSchemaConfig(); |
|
| 150 | |||
| 151 | 259 | $metadataSchemaConfig->setExplicitForeignKeyIndexes(false); |
|
| 152 | 259 | $schema = new Schema([], [], $metadataSchemaConfig); |
|
| 153 | |||
| 154 | 259 | $addedFks = []; |
|
| 155 | 259 | $blacklistedFks = []; |
|
| 156 | |||
| 157 | 259 | foreach ($classes as $class) { |
|
| 158 | /** @var \Doctrine\ORM\Mapping\ClassMetadata $class */ |
||
| 159 | 259 | if ($this->processingNotRequired($class, $processedClasses)) { |
|
| 160 | 36 | continue; |
|
| 161 | } |
||
| 162 | |||
| 163 | 259 | $table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform)); |
|
| 164 | |||
| 165 | 259 | if ($class->isInheritanceTypeSingleTable()) { |
|
| 166 | 39 | $this->gatherColumns($class, $table); |
|
| 167 | 39 | $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks); |
|
| 168 | |||
| 169 | // Add the discriminator column |
||
| 170 | 39 | $this->addDiscriminatorColumnDefinition($class, $table); |
|
| 171 | |||
| 172 | // Aggregate all the information from all classes in the hierarchy |
||
| 173 | 39 | foreach ($class->parentClasses as $parentClassName) { |
|
| 174 | // Parent class information is already contained in this class |
||
| 175 | $processedClasses[$parentClassName] = true; |
||
| 176 | } |
||
| 177 | |||
| 178 | 39 | foreach ($class->subClasses as $subClassName) { |
|
| 179 | 38 | $subClass = $this->em->getClassMetadata($subClassName); |
|
| 180 | 38 | $this->gatherColumns($subClass, $table); |
|
|
|
|||
| 181 | 38 | $this->gatherRelationsSql($subClass, $table, $schema, $addedFks, $blacklistedFks); |
|
| 182 | 39 | $processedClasses[$subClassName] = true; |
|
| 183 | } |
||
| 184 | 256 | } elseif ($class->isInheritanceTypeJoined()) { |
|
| 185 | // Add all non-inherited fields as columns |
||
| 186 | 40 | $pkColumns = []; |
|
| 187 | 40 | foreach ($class->fieldMappings as $fieldName => $mapping) { |
|
| 188 | 40 | if ( ! isset($mapping['inherited'])) { |
|
| 189 | 40 | $columnName = $this->quoteStrategy->getColumnName( |
|
| 190 | 40 | $mapping['fieldName'], |
|
| 191 | $class, |
||
| 192 | 40 | $this->platform |
|
| 193 | ); |
||
| 194 | 40 | $this->gatherColumn($class, $mapping, $table); |
|
| 195 | |||
| 196 | 40 | if ($class->isIdentifier($fieldName)) { |
|
| 197 | 40 | $pkColumns[] = $columnName; |
|
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | 40 | $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks); |
|
| 203 | |||
| 204 | // Add the discriminator column only to the root table |
||
| 205 | 40 | if ($class->name == $class->rootEntityName) { |
|
| 206 | 40 | $this->addDiscriminatorColumnDefinition($class, $table); |
|
| 207 | } else { |
||
| 208 | // Add an ID FK column to child tables |
||
| 209 | 39 | $inheritedKeyColumns = []; |
|
| 210 | 39 | foreach ($class->identifier as $identifierField) { |
|
| 211 | 39 | $idMapping = $class->fieldMappings[$identifierField]; |
|
| 212 | 39 | if (isset($idMapping['inherited'])) { |
|
| 213 | 39 | $this->gatherColumn($class, $idMapping, $table); |
|
| 214 | 39 | $columnName = $this->quoteStrategy->getColumnName( |
|
| 215 | $identifierField, |
||
| 216 | $class, |
||
| 217 | 39 | $this->platform |
|
| 218 | ); |
||
| 219 | // TODO: This seems rather hackish, can we optimize it? |
||
| 220 | 39 | $table->getColumn($columnName)->setAutoincrement(false); |
|
| 221 | |||
| 222 | 39 | $pkColumns[] = $columnName; |
|
| 223 | 39 | $inheritedKeyColumns[] = $columnName; |
|
| 224 | } |
||
| 225 | } |
||
| 226 | 39 | if (!empty($inheritedKeyColumns)) { |
|
| 227 | // Add a FK constraint on the ID column |
||
| 228 | 39 | $table->addForeignKeyConstraint( |
|
| 229 | 39 | $this->quoteStrategy->getTableName( |
|
| 230 | 39 | $this->em->getClassMetadata($class->rootEntityName), |
|
| 231 | 39 | $this->platform |
|
| 232 | ), |
||
| 233 | $inheritedKeyColumns, |
||
| 234 | $inheritedKeyColumns, |
||
| 235 | 39 | ['onDelete' => 'CASCADE'] |
|
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | } |
||
| 240 | |||
| 241 | 40 | $table->setPrimaryKey($pkColumns); |
|
| 242 | |||
| 243 | 244 | } elseif ($class->isInheritanceTypeTablePerClass()) { |
|
| 244 | throw ORMException::notSupported(); |
||
| 245 | } else { |
||
| 246 | 244 | $this->gatherColumns($class, $table); |
|
| 247 | 244 | $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks); |
|
| 248 | } |
||
| 249 | |||
| 250 | 259 | $pkColumns = []; |
|
| 251 | |||
| 252 | 259 | foreach ($class->identifier as $identifierField) { |
|
| 253 | 259 | if (isset($class->fieldMappings[$identifierField])) { |
|
| 254 | 259 | $pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform); |
|
| 255 | 24 | } elseif (isset($class->associationMappings[$identifierField])) { |
|
| 256 | /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */ |
||
| 257 | 24 | $assoc = $class->associationMappings[$identifierField]; |
|
| 258 | |||
| 259 | 24 | foreach ($assoc['joinColumns'] as $joinColumn) { |
|
| 260 | 259 | $pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); |
|
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | 259 | if ( ! $table->hasIndex('primary')) { |
|
| 266 | 249 | $table->setPrimaryKey($pkColumns); |
|
| 267 | } |
||
| 268 | |||
| 269 | // there can be unique indexes automatically created for join column |
||
| 270 | // if join column is also primary key we should keep only primary key on this column |
||
| 271 | // so, remove indexes overruled by primary key |
||
| 272 | 259 | $primaryKey = $table->getIndex('primary'); |
|
| 273 | |||
| 274 | 259 | foreach ($table->getIndexes() as $idxKey => $existingIndex) { |
|
| 275 | 259 | if ($primaryKey->overrules($existingIndex)) { |
|
| 276 | 259 | $table->dropIndex($idxKey); |
|
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | 259 | if (isset($class->table['indexes'])) { |
|
| 281 | 1 | foreach ($class->table['indexes'] as $indexName => $indexData) { |
|
| 282 | 1 | if ( ! isset($indexData['flags'])) { |
|
| 283 | 1 | $indexData['flags'] = []; |
|
| 284 | } |
||
| 285 | |||
| 286 | 1 | $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array) $indexData['flags'], isset($indexData['options']) ? $indexData['options'] : []); |
|
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | 259 | if (isset($class->table['uniqueConstraints'])) { |
|
| 291 | 4 | foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) { |
|
| 292 | 4 | $uniqIndex = new Index($indexName, $indexData['columns'], true, false, [], isset($indexData['options']) ? $indexData['options'] : []); |
|
| 293 | |||
| 294 | 4 | foreach ($table->getIndexes() as $tableIndexName => $tableIndex) { |
|
| 295 | 4 | if ($tableIndex->isFullfilledBy($uniqIndex)) { |
|
| 296 | 3 | $table->dropIndex($tableIndexName); |
|
| 297 | 4 | break; |
|
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | 4 | $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : []); |
|
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | 259 | if (isset($class->table['options'])) { |
|
| 306 | 1 | foreach ($class->table['options'] as $key => $val) { |
|
| 307 | 1 | $table->addOption($key, $val); |
|
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | 259 | $processedClasses[$class->name] = true; |
|
| 312 | |||
| 313 | 259 | if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) { |
|
| 314 | $seqDef = $class->sequenceGeneratorDefinition; |
||
| 315 | $quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform); |
||
| 316 | if ( ! $schema->hasSequence($quotedName)) { |
||
| 317 | $schema->createSequence( |
||
| 318 | $quotedName, |
||
| 319 | $seqDef['allocationSize'], |
||
| 320 | $seqDef['initialValue'] |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | 259 | if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) { |
|
| 326 | 1 | $eventManager->dispatchEvent( |
|
| 327 | 1 | ToolEvents::postGenerateSchemaTable, |
|
| 328 | 259 | new GenerateSchemaTableEventArgs($class, $schema, $table) |
|
| 329 | ); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | 259 | if ( ! $this->platform->supportsSchemas() && ! $this->platform->canEmulateSchemas() ) { |
|
| 334 | 7 | $schema->visit(new RemoveNamespacedAssets()); |
|
| 335 | } |
||
| 336 | |||
| 337 | 259 | if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) { |
|
| 338 | 1 | $eventManager->dispatchEvent( |
|
| 339 | 1 | ToolEvents::postGenerateSchema, |
|
| 340 | 1 | new GenerateSchemaEventArgs($this->em, $schema) |
|
| 341 | ); |
||
| 342 | } |
||
| 343 | |||
| 344 | 259 | return $schema; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Gets a portable column definition as required by the DBAL for the discriminator |
||
| 349 | * column of a class. |
||
| 350 | * |
||
| 351 | * @param ClassMetadata $class |
||
| 352 | * @param Table $table |
||
| 353 | * |
||
| 354 | * @return array The portable column definition of the discriminator column as required by |
||
| 355 | * the DBAL. |
||
| 356 | */ |
||
| 357 | 75 | private function addDiscriminatorColumnDefinition($class, Table $table) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Gathers the column definitions as required by the DBAL of all field mappings |
||
| 382 | * found in the given class. |
||
| 383 | * |
||
| 384 | * @param ClassMetadata $class |
||
| 385 | * @param Table $table |
||
| 386 | * |
||
| 387 | * @return array The list of portable column definitions as required by the DBAL. |
||
| 388 | */ |
||
| 389 | 249 | private function gatherColumns($class, Table $table) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Creates a column definition as required by the DBAL from an ORM field mapping definition. |
||
| 408 | * |
||
| 409 | * @param ClassMetadata $class The class that owns the field mapping. |
||
| 410 | * @param array $mapping The field mapping. |
||
| 411 | * @param Table $table |
||
| 412 | * |
||
| 413 | * @return array The portable column definition as required by the DBAL. |
||
| 414 | */ |
||
| 415 | 259 | private function gatherColumn($class, array $mapping, Table $table) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Gathers the SQL for properly setting up the relations of the given class. |
||
| 486 | * This includes the SQL for foreign key constraints and join tables. |
||
| 487 | * |
||
| 488 | * @param ClassMetadata $class |
||
| 489 | * @param Table $table |
||
| 490 | * @param Schema $schema |
||
| 491 | * @param array $addedFks |
||
| 492 | * @param array $blacklistedFks |
||
| 493 | * |
||
| 494 | * @return void |
||
| 495 | * |
||
| 496 | * @throws \Doctrine\ORM\ORMException |
||
| 497 | */ |
||
| 498 | 259 | private function gatherRelationsSql($class, $table, $schema, &$addedFks, &$blacklistedFks) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Gets the class metadata that is responsible for the definition of the referenced column name. |
||
| 561 | * |
||
| 562 | * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its |
||
| 563 | * not a simple field, go through all identifier field names that are associations recursively and |
||
| 564 | * find that referenced column name. |
||
| 565 | * |
||
| 566 | * TODO: Is there any way to make this code more pleasing? |
||
| 567 | * |
||
| 568 | * @param ClassMetadata $class |
||
| 569 | * @param string $referencedColumnName |
||
| 570 | * |
||
| 571 | * @return array (ClassMetadata, referencedFieldName) |
||
| 572 | */ |
||
| 573 | 188 | private function getDefiningClass($class, $referencedColumnName) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Gathers columns and fk constraints that are required for one part of relationship. |
||
| 599 | * |
||
| 600 | * @param array $joinColumns |
||
| 601 | * @param Table $theJoinTable |
||
| 602 | * @param ClassMetadata $class |
||
| 603 | * @param array $mapping |
||
| 604 | * @param array $primaryKeyColumns |
||
| 605 | * @param array $addedFks |
||
| 606 | * @param array $blacklistedFks |
||
| 607 | * |
||
| 608 | * @return void |
||
| 609 | * |
||
| 610 | * @throws \Doctrine\ORM\ORMException |
||
| 611 | */ |
||
| 612 | 188 | private function gatherRelationJoinColumns( |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Drops the database schema for the given classes. |
||
| 730 | * |
||
| 731 | * In any way when an exception is thrown it is suppressed since drop was |
||
| 732 | * issued for all classes of the schema and some probably just don't exist. |
||
| 733 | * |
||
| 734 | * @param array $classes |
||
| 735 | * |
||
| 736 | * @return void |
||
| 737 | */ |
||
| 738 | 3 | public function dropSchema(array $classes) |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Drops all elements in the database of the current connection. |
||
| 754 | * |
||
| 755 | * @return void |
||
| 756 | */ |
||
| 757 | public function dropDatabase() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Gets the SQL needed to drop the database schema for the connections database. |
||
| 769 | * |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | public function getDropDatabaseSQL() |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Gets SQL to drop the tables defined by the passed classes. |
||
| 785 | * |
||
| 786 | * @param array $classes |
||
| 787 | * |
||
| 788 | * @return array |
||
| 789 | */ |
||
| 790 | 3 | public function getDropSchemaSQL(array $classes) |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Updates the database schema of the given classes by comparing the ClassMetadata |
||
| 838 | * instances to the current database schema that is inspected. |
||
| 839 | * |
||
| 840 | * @param array $classes |
||
| 841 | * @param boolean $saveMode If TRUE, only performs a partial update |
||
| 842 | * without dropping assets which are scheduled for deletion. |
||
| 843 | * |
||
| 844 | * @return void |
||
| 845 | */ |
||
| 846 | public function updateSchema(array $classes, $saveMode = false) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Gets the sequence of SQL statements that need to be performed in order |
||
| 858 | * to bring the given class mappings in-synch with the relational schema. |
||
| 859 | * |
||
| 860 | * @param array $classes The classes to consider. |
||
| 861 | * @param boolean $saveMode If TRUE, only generates SQL for a partial update |
||
| 862 | * that does not include SQL for dropping assets which are scheduled for deletion. |
||
| 863 | * |
||
| 864 | * @return array The sequence of SQL statements. |
||
| 865 | */ |
||
| 866 | 1 | public function getUpdateSchemaSql(array $classes, $saveMode = false) |
|
| 882 | } |
||
| 883 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.