Complex classes like tx_t3deploy_databaseController 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 tx_t3deploy_databaseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class tx_t3deploy_databaseController { |
||
| 21 | /* |
||
| 22 | * List of all possible update types: |
||
| 23 | * + add, change, drop, create_table, change_table, drop_table, clear_table |
||
| 24 | * List of all sensible update types: |
||
| 25 | * + add, change, create_table, change_table |
||
| 26 | */ |
||
| 27 | const UpdateTypes_List = 'add,change,create_table,change_table'; |
||
|
|
|||
| 28 | const RemoveTypes_list = 'drop,drop_table,clear_table'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService |
||
| 32 | */ |
||
| 33 | protected $schemaMigrationService; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService |
||
| 37 | */ |
||
| 38 | protected $expectedSchemaService; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \TYPO3\CMS\Core\Category\CategoryRegistry |
||
| 42 | */ |
||
| 43 | protected $categoryRegistry; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $consideredTypes; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates this object. |
||
| 52 | */ |
||
| 53 | 5 | public function __construct() { |
|
| 54 | /** @var TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */ |
||
| 55 | 5 | $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); |
|
| 56 | |||
| 57 | 5 | $this->schemaMigrationService = $objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService'); |
|
| 58 | 5 | $this->expectedSchemaService = $objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlExpectedSchemaService'); |
|
| 59 | 5 | $this->categoryRegistry = $objectManager->get('TYPO3\\CMS\\Core\\Category\\CategoryRegistry'); |
|
| 60 | |||
| 61 | 5 | $this->setConsideredTypes($this->getUpdateTypes()); |
|
| 62 | 5 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Sets the types considered to be executed (updates and/or removal). |
||
| 66 | * |
||
| 67 | * @param array $consideredTypes |
||
| 68 | * @return void |
||
| 69 | * @see updateStructureAction() |
||
| 70 | */ |
||
| 71 | 5 | public function setConsideredTypes(array $consideredTypes) { |
|
| 72 | 5 | $this->consideredTypes = $consideredTypes; |
|
| 73 | 5 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Adds considered types. |
||
| 77 | * |
||
| 78 | * @param array $consideredTypes |
||
| 79 | * @return void |
||
| 80 | * @see updateStructureAction() |
||
| 81 | */ |
||
| 82 | 2 | public function addConsideredTypes(array $consideredTypes) { |
|
| 83 | 2 | $this->consideredTypes = array_unique( |
|
| 84 | 2 | array_merge($this->consideredTypes, $consideredTypes) |
|
| 85 | ); |
||
| 86 | 2 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Removes defined types from considered types. |
||
| 90 | * |
||
| 91 | * @param array $removals |
||
| 92 | * @return void |
||
| 93 | * @see updateStructureAction() |
||
| 94 | */ |
||
| 95 | public function removeConsideredTypes(array $removals) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Updates the database structure. |
||
| 101 | * |
||
| 102 | * @param array $arguments Optional arguments passed to this action |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 5 | public function updateStructureAction(array $arguments) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * call executeUpdateStructure until there are no more changes. |
||
| 137 | * |
||
| 138 | * The install tool sometimes relies on the user hitting the "update" button multiple times. This method |
||
| 139 | * encapsulates that behaviour. |
||
| 140 | * |
||
| 141 | * @see executeUpdateStructure() |
||
| 142 | * @param array $arguments |
||
| 143 | * @param bool $allowKeyModifications |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 5 | protected function executeUpdateStructureUntilNoMoreChanges(array $arguments, $allowKeyModifications = FALSE) { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Executes the database structure updates. |
||
| 170 | * |
||
| 171 | * @param array $arguments Optional arguments passed to this action |
||
| 172 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 5 | protected function executeUpdateStructure(array $arguments, $allowKeyModifications = FALSE) { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * performs some basic checks on the database changes to identify most common errors |
||
| 236 | * |
||
| 237 | * @param string $changes the changes to check |
||
| 238 | * @throws Exception if the file seems to contain bad data |
||
| 239 | */ |
||
| 240 | 5 | protected function checkChangesSyntax($changes) { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Removes key modifications that will cause errors. |
||
| 250 | * |
||
| 251 | * @param array $differences The differences to be cleaned up |
||
| 252 | * @return array The cleaned differences |
||
| 253 | */ |
||
| 254 | 5 | protected function removeKeyModifications(array $differences) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Unsets a subkey in a given differences array. |
||
| 263 | * |
||
| 264 | * @param array $differences |
||
| 265 | * @param string $type e.g. extra or diff |
||
| 266 | * @param string $subKey e.g. keys or fields |
||
| 267 | * @param string $exception e.g. whole_table that stops the removal |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | 5 | protected function unsetSubKey(array $differences, $type, $subKey, $exception = '') { |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Gets the differences in the database structure by comparing |
||
| 285 | * the current structure with the SQL definitions of all extensions |
||
| 286 | * and the TYPO3 core in t3lib/stddb/tables.sql. |
||
| 287 | * |
||
| 288 | * This method searches for fields/tables to be added/updated. |
||
| 289 | * |
||
| 290 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
| 291 | * @return array The database statements to update the structure |
||
| 292 | */ |
||
| 293 | 5 | protected function getStructureDifferencesForUpdate($allowKeyModifications = FALSE) { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Gets the differences in the database structure by comparing |
||
| 308 | * the current structure with the SQL definitions of all extensions |
||
| 309 | * and the TYPO3 core in t3lib/stddb/tables.sql. |
||
| 310 | * |
||
| 311 | * This method searches for fields/tables to be removed. |
||
| 312 | * |
||
| 313 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
| 314 | * @return array The database statements to update the structure |
||
| 315 | */ |
||
| 316 | 2 | protected function getStructureDifferencesForRemoval($allowKeyModifications = FALSE) { |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Gets the defined field definitions from the ext_tables.sql files. |
||
| 331 | * |
||
| 332 | * @return array The accordant definitions |
||
| 333 | */ |
||
| 334 | 5 | protected function getDefinedFieldDefinitions() { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Gets all structure definitions of extensions the TYPO3 Core. |
||
| 344 | * |
||
| 345 | * @return array All structure definitions |
||
| 346 | */ |
||
| 347 | 5 | protected function getAllRawStructureDefinitions() { |
|
| 348 | |||
| 349 | 5 | $packageStates = include(PATH_typo3conf .'PackageStates.php'); |
|
| 350 | |||
| 351 | 5 | $tmp = $GLOBALS['TYPO3_LOADED_EXT']; |
|
| 352 | |||
| 353 | 5 | $GLOBALS['TYPO3_LOADED_EXT'] = array_merge($packageStates['packages'], $GLOBALS['TYPO3_LOADED_EXT']); |
|
| 354 | |||
| 355 | 5 | $expectedSchemaString = $this->expectedSchemaService->getTablesDefinitionString(TRUE); |
|
| 356 | 5 | $rawDefinitions = $this->schemaMigrationService->getStatementArray($expectedSchemaString, TRUE); |
|
| 357 | |||
| 358 | 5 | $GLOBALS['TYPO3_LOADED_EXT'] = $tmp; |
|
| 359 | |||
| 360 | 5 | return $rawDefinitions; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Gets the defined update types. |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 5 | protected function getUpdateTypes() { |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Gets the defined remove types. |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | 2 | protected function getRemoveTypes() { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * sorts the statements in an array |
||
| 383 | * |
||
| 384 | * @param array $statements |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | 5 | protected function sortStatements($statements) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * returns true if the given statement looks as if it drops a (primary) key |
||
| 405 | * |
||
| 406 | * @param $statement |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | 5 | protected function isDropKeyStatement($statement) { |
|
| 412 | } |
||
| 413 |