Complex classes like 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 DatabaseController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DatabaseController |
||
40 | { |
||
41 | /* |
||
42 | * List of all possible update types: |
||
43 | * + add, change, drop, create_table, change_table, drop_table, clear_table |
||
44 | * List of all sensible update types: |
||
45 | * + add, change, create_table, change_table |
||
46 | */ |
||
47 | const UpdateTypes_List = 'add,change,create_table,change_table'; |
||
|
|||
48 | const RemoveTypes_list = 'drop,drop_table,clear_table'; |
||
49 | |||
50 | /** |
||
51 | * @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService |
||
52 | */ |
||
53 | protected $schemaMigrationService; |
||
54 | |||
55 | /** |
||
56 | * @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService |
||
57 | */ |
||
58 | protected $expectedSchemaService; |
||
59 | |||
60 | /** |
||
61 | * @var \TYPO3\CMS\Core\Category\CategoryRegistry |
||
62 | */ |
||
63 | protected $categoryRegistry; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $consideredTypes; |
||
69 | |||
70 | /** |
||
71 | * Creates this object. |
||
72 | */ |
||
73 | public function __construct() |
||
74 | { |
||
75 | /** @var ObjectManager $objectManager */ |
||
76 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
77 | |||
78 | $this->schemaMigrationService = $objectManager->get(SqlSchemaMigrationService::class); |
||
79 | $this->expectedSchemaService = $objectManager->get(SqlExpectedSchemaService::class); |
||
80 | $this->categoryRegistry = $objectManager->get(CategoryRegistry::class); |
||
81 | |||
82 | $this->setConsideredTypes($this->getUpdateTypes()); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Sets the types considered to be executed (updates and/or removal). |
||
87 | * |
||
88 | * @param array $consideredTypes |
||
89 | * @return void |
||
90 | * @see updateStructureAction() |
||
91 | */ |
||
92 | public function setConsideredTypes(array $consideredTypes) |
||
93 | { |
||
94 | $this->consideredTypes = $consideredTypes; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Adds considered types. |
||
99 | * |
||
100 | * @param array $consideredTypes |
||
101 | * @return void |
||
102 | * @see updateStructureAction() |
||
103 | */ |
||
104 | public function addConsideredTypes(array $consideredTypes) |
||
105 | { |
||
106 | $this->consideredTypes = array_unique( |
||
107 | array_merge($this->consideredTypes, $consideredTypes) |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Removes defined types from considered types. |
||
113 | * |
||
114 | * @param array $removals |
||
115 | * @return void |
||
116 | * @see updateStructureAction() |
||
117 | */ |
||
118 | public function removeConsideredTypes(array $removals) |
||
122 | |||
123 | /** |
||
124 | * Updates the database structure. |
||
125 | * |
||
126 | * @param array $arguments Optional arguments passed to this action |
||
127 | * @return string |
||
128 | * @throws \Exception |
||
129 | */ |
||
130 | public function updateStructureAction(array $arguments) |
||
164 | |||
165 | /** |
||
166 | * call executeUpdateStructure until there are no more changes. |
||
167 | * |
||
168 | * The install tool sometimes relies on the user hitting the "update" button multiple times. This method |
||
169 | * encapsulates that behaviour. |
||
170 | * |
||
171 | * @see executeUpdateStructure() |
||
172 | * @param array $arguments |
||
173 | * @param bool $allowKeyModifications |
||
174 | * @return string |
||
175 | * @throws \Exception |
||
176 | */ |
||
177 | protected function executeUpdateStructureUntilNoMoreChanges(array $arguments, $allowKeyModifications = false) |
||
201 | |||
202 | /** |
||
203 | * Executes the database structure updates. |
||
204 | * |
||
205 | * @param array $arguments Optional arguments passed to this action |
||
206 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
207 | * @return string |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | protected function executeUpdateStructure(array $arguments, $allowKeyModifications = false) |
||
269 | |||
270 | /** |
||
271 | * performs some basic checks on the database changes to identify most common errors |
||
272 | * |
||
273 | * @param string $changes the changes to check |
||
274 | * @throws \Exception if the file seems to contain bad data |
||
275 | */ |
||
276 | protected function checkChangesSyntax($changes) |
||
291 | |||
292 | /** |
||
293 | * Removes key modifications that will cause errors. |
||
294 | * |
||
295 | * @param array $differences The differences to be cleaned up |
||
296 | * @return array The cleaned differences |
||
297 | */ |
||
298 | protected function removeKeyModifications(array $differences) |
||
305 | |||
306 | /** |
||
307 | * Unsets a subkey in a given differences array. |
||
308 | * |
||
309 | * @param array $differences |
||
310 | * @param string $type e.g. extra or diff |
||
311 | * @param string $subKey e.g. keys or fields |
||
312 | * @param string $exception e.g. whole_table that stops the removal |
||
313 | * @return array |
||
314 | */ |
||
315 | protected function unsetSubKey(array $differences, $type, $subKey, $exception = '') |
||
328 | |||
329 | /** |
||
330 | * Gets the differences in the database structure by comparing |
||
331 | * the current structure with the SQL definitions of all extensions |
||
332 | * and the TYPO3 core in t3lib/stddb/tables.sql. |
||
333 | * |
||
334 | * This method searches for fields/tables to be added/updated. |
||
335 | * |
||
336 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
337 | * @return array The database statements to update the structure |
||
338 | */ |
||
339 | protected function getStructureDifferencesForUpdate($allowKeyModifications = false) |
||
352 | |||
353 | /** |
||
354 | * Gets the differences in the database structure by comparing |
||
355 | * the current structure with the SQL definitions of all extensions |
||
356 | * and the TYPO3 core in t3lib/stddb/tables.sql. |
||
357 | * |
||
358 | * This method searches for fields/tables to be removed. |
||
359 | * |
||
360 | * @param boolean $allowKeyModifications Whether to allow key modifications |
||
361 | * @return array The database statements to update the structure |
||
362 | */ |
||
363 | protected function getStructureDifferencesForRemoval($allowKeyModifications = false) |
||
376 | |||
377 | /** |
||
378 | * Gets the defined field definitions from the ext_tables.sql files. |
||
379 | * |
||
380 | * @return array The accordant definitions |
||
381 | */ |
||
382 | protected function getDefinedFieldDefinitions() |
||
390 | |||
391 | /** |
||
392 | * Gets all structure definitions of extensions the TYPO3 Core. |
||
393 | * |
||
394 | * @return array All structure definitions |
||
395 | */ |
||
396 | protected function getAllRawStructureDefinitions() |
||
411 | |||
412 | /** |
||
413 | * Gets the defined update types. |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | protected function getUpdateTypes() |
||
421 | |||
422 | /** |
||
423 | * Gets the defined remove types. |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | protected function getRemoveTypes() |
||
431 | |||
432 | /** |
||
433 | * sorts the statements in an array |
||
434 | * |
||
435 | * @param array $statements |
||
436 | * @return array |
||
437 | */ |
||
438 | protected function sortStatements($statements) |
||
456 | |||
457 | /** |
||
458 | * returns true if the given statement looks as if it drops a (primary) key |
||
459 | * |
||
460 | * @param $statement |
||
461 | * @return bool |
||
462 | */ |
||
463 | protected function isDropKeyStatement($statement) |
||
467 | } |
||
468 |