Complex classes like AbstractHandler 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 AbstractHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | abstract class AbstractHandler |
||
27 | { |
||
28 | //Foreign key modification behaviours |
||
29 | const DROP_FOREIGNS = 0b000000001; |
||
30 | const CREATE_FOREIGNS = 0b000000010; |
||
31 | const ALTER_FOREIGNS = 0b000000100; |
||
32 | |||
33 | //All foreign keys related operations |
||
34 | const DO_FOREIGNS = self::DROP_FOREIGNS | self::ALTER_FOREIGNS | self::CREATE_FOREIGNS; |
||
35 | |||
36 | //Column modification behaviours |
||
37 | const DROP_COLUMNS = 0b000001000; |
||
38 | const CREATE_COLUMNS = 0b000010000; |
||
39 | const ALTER_COLUMNS = 0b000100000; |
||
40 | |||
41 | //All columns related operations |
||
42 | const DO_COLUMNS = self::DROP_COLUMNS | self::ALTER_COLUMNS | self::CREATE_COLUMNS; |
||
43 | |||
44 | //Index modification behaviours |
||
45 | const DROP_INDEXES = 0b001000000; |
||
46 | const CREATE_INDEXES = 0b010000000; |
||
47 | const ALTER_INDEXES = 0b100000000; |
||
48 | |||
49 | //All index related operations |
||
50 | const DO_INDEXES = self::DROP_INDEXES | self::ALTER_INDEXES | self::CREATE_INDEXES; |
||
51 | |||
52 | //General purpose schema operations |
||
53 | const DO_RENAME = 0b10000000000; |
||
54 | const DO_DROP = 0b01000000000; |
||
55 | |||
56 | //All operations |
||
57 | const DO_ALL = self::DO_FOREIGNS | self::DO_INDEXES | self::DO_COLUMNS | self::DO_DROP | self::DO_RENAME; |
||
58 | |||
59 | /** |
||
60 | * @var LoggerInterface|null |
||
61 | */ |
||
62 | private $logger = null; |
||
63 | |||
64 | /** |
||
65 | * @var Driver |
||
66 | */ |
||
67 | protected $driver; |
||
68 | |||
69 | /** |
||
70 | * @param Driver $driver |
||
71 | * @param LoggerInterface|null $logger |
||
72 | */ |
||
73 | public function __construct(Driver $driver, LoggerInterface $logger = null) |
||
78 | |||
79 | /** |
||
80 | * Associated driver. |
||
81 | * |
||
82 | * @return Driver |
||
83 | */ |
||
84 | public function getDriver(): Driver |
||
88 | |||
89 | /** |
||
90 | * Create table based on a given schema. |
||
91 | * |
||
92 | * @param AbstractTable $table |
||
93 | * |
||
94 | * @throws SchemaHandlerException |
||
95 | */ |
||
96 | public function createTable(AbstractTable $table) |
||
108 | |||
109 | /** |
||
110 | * Drop table from database. |
||
111 | * |
||
112 | * @param AbstractTable $table |
||
113 | * |
||
114 | * @throws SchemaHandlerException |
||
115 | */ |
||
116 | public function dropTable(AbstractTable $table) |
||
121 | |||
122 | /** |
||
123 | * Sync given table schema. |
||
124 | * |
||
125 | * @param AbstractTable $table |
||
126 | * @param int $behaviour See behaviour constants. |
||
127 | */ |
||
128 | public function syncTable(AbstractTable $table, int $behaviour = self::DO_ALL) |
||
152 | |||
153 | /** |
||
154 | * Rename table from one name to another. |
||
155 | * |
||
156 | * @param string $table |
||
157 | * @param string $name |
||
158 | * |
||
159 | * @throws SchemaHandlerException |
||
160 | */ |
||
161 | public function renameTable(string $table, string $name) |
||
165 | |||
166 | /** |
||
167 | * Driver specific column add command. |
||
168 | * |
||
169 | * @param AbstractTable $table |
||
170 | * @param AbstractColumn $column |
||
171 | * |
||
172 | * @throws SchemaHandlerException |
||
173 | */ |
||
174 | public function createColumn(AbstractTable $table, AbstractColumn $column) |
||
178 | |||
179 | /** |
||
180 | * Driver specific column remove (drop) command. |
||
181 | * |
||
182 | * @param AbstractTable $table |
||
183 | * @param AbstractColumn $column |
||
184 | * |
||
185 | * @return AbstractHandler |
||
186 | */ |
||
187 | public function dropColumn(AbstractTable $table, AbstractColumn $column) |
||
196 | |||
197 | /** |
||
198 | * Driver specific column alter command. |
||
199 | * |
||
200 | * @param AbstractTable $table |
||
201 | * @param AbstractColumn $initial |
||
202 | * @param AbstractColumn $column |
||
203 | * |
||
204 | * @throws SchemaHandlerException |
||
205 | */ |
||
206 | abstract public function alterColumn( |
||
211 | |||
212 | /** |
||
213 | * Driver specific index adding command. |
||
214 | * |
||
215 | * @param AbstractTable $table |
||
216 | * @param AbstractIndex $index |
||
217 | * |
||
218 | * @throws SchemaHandlerException |
||
219 | */ |
||
220 | public function createIndex(AbstractTable $table, AbstractIndex $index) |
||
224 | |||
225 | /** |
||
226 | * Driver specific index remove (drop) command. |
||
227 | * |
||
228 | * @param AbstractTable $table |
||
229 | * @param AbstractIndex $index |
||
230 | * |
||
231 | * @throws SchemaHandlerException |
||
232 | */ |
||
233 | public function dropIndex(AbstractTable $table, AbstractIndex $index) |
||
237 | |||
238 | /** |
||
239 | * Driver specific index alter command, by default it will remove and add index. |
||
240 | * |
||
241 | * @param AbstractTable $table |
||
242 | * @param AbstractIndex $initial |
||
243 | * @param AbstractIndex $index |
||
244 | * |
||
245 | * @throws SchemaHandlerException |
||
246 | */ |
||
247 | public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
||
252 | |||
253 | /** |
||
254 | * Driver specific foreign key adding command. |
||
255 | * |
||
256 | * @param AbstractTable $table |
||
257 | * @param AbstractReference $foreign |
||
258 | * |
||
259 | * @throws SchemaHandlerException |
||
260 | */ |
||
261 | public function createForeign(AbstractTable $table, AbstractReference $foreign) |
||
265 | |||
266 | /** |
||
267 | * Driver specific foreign key remove (drop) command. |
||
268 | * |
||
269 | * @param AbstractTable $table |
||
270 | * @param AbstractReference $foreign |
||
271 | * |
||
272 | * @throws SchemaHandlerException |
||
273 | */ |
||
274 | public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
||
278 | |||
279 | /** |
||
280 | * Driver specific foreign key alter command, by default it will remove and add foreign key. |
||
281 | * |
||
282 | * @param AbstractTable $table |
||
283 | * @param AbstractReference $initial |
||
284 | * @param AbstractReference $foreign |
||
285 | * |
||
286 | * @throws SchemaHandlerException |
||
287 | */ |
||
288 | public function alterForeign( |
||
296 | |||
297 | /** |
||
298 | * Drop column constraint using it's name. |
||
299 | * |
||
300 | * @param AbstractTable $table |
||
301 | * @param string $constraint |
||
302 | * |
||
303 | * @throws SchemaHandlerException |
||
304 | */ |
||
305 | public function dropConstrain(AbstractTable $table, $constraint) |
||
309 | |||
310 | /** |
||
311 | * Get statement needed to create table. Indexes will be created separately. |
||
312 | * |
||
313 | * @param AbstractTable $table |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | protected function createStatement(AbstractTable $table) |
||
345 | |||
346 | /** |
||
347 | * @param AbstractTable $table |
||
348 | * @param int $behaviour |
||
349 | * @param StateComparator $comparator |
||
350 | */ |
||
351 | protected function executeChanges( |
||
372 | |||
373 | /** |
||
374 | * Execute statement. |
||
375 | * |
||
376 | * @param string $statement |
||
377 | * @param array $parameters |
||
378 | * |
||
379 | * @return \PDOStatement |
||
380 | * |
||
381 | * @throws SchemaHandlerException |
||
382 | */ |
||
383 | protected function run(string $statement, array $parameters = []): \PDOStatement |
||
391 | |||
392 | /** |
||
393 | * Helper function, saves log message into logger if any attached. |
||
394 | * |
||
395 | * @param string $message |
||
396 | * @param array $context |
||
397 | */ |
||
398 | protected function log(string $message, array $context = []) |
||
404 | |||
405 | /** |
||
406 | * Create element identifier. |
||
407 | * |
||
408 | * @param AbstractElement|AbstractTable|string $element |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | protected function identify($element) |
||
424 | |||
425 | /** |
||
426 | * @param AbstractTable $table |
||
427 | * @param StateComparator $comparator |
||
428 | */ |
||
429 | protected function alterForeigns(AbstractTable $table, StateComparator $comparator) |
||
446 | |||
447 | /** |
||
448 | * @param AbstractTable $table |
||
449 | * @param StateComparator $comparator |
||
450 | */ |
||
451 | protected function createForeigns(AbstractTable $table, StateComparator $comparator) |
||
462 | |||
463 | /** |
||
464 | * @param AbstractTable $table |
||
465 | * @param StateComparator $comparator |
||
466 | */ |
||
467 | protected function alterIndexes(AbstractTable $table, StateComparator $comparator) |
||
485 | |||
486 | /** |
||
487 | * @param AbstractTable $table |
||
488 | * @param StateComparator $comparator |
||
489 | */ |
||
490 | protected function createIndexes(AbstractTable $table, StateComparator $comparator) |
||
501 | |||
502 | /** |
||
503 | * @param AbstractTable $table |
||
504 | * @param StateComparator $comparator |
||
505 | */ |
||
506 | protected function alterColumns(AbstractTable $table, StateComparator $comparator) |
||
525 | |||
526 | /** |
||
527 | * @param AbstractTable $table |
||
528 | * @param StateComparator $comparator |
||
529 | */ |
||
530 | protected function createColumns(AbstractTable $table, StateComparator $comparator) |
||
542 | |||
543 | /** |
||
544 | * @param AbstractTable $table |
||
545 | * @param StateComparator $comparator |
||
546 | */ |
||
547 | protected function dropColumns(AbstractTable $table, StateComparator $comparator) |
||
558 | |||
559 | /** |
||
560 | * @param AbstractTable $table |
||
561 | * @param StateComparator $comparator |
||
562 | */ |
||
563 | protected function dropIndexes(AbstractTable $table, StateComparator $comparator) |
||
574 | |||
575 | /** |
||
576 | * @param AbstractTable $table |
||
577 | * @param StateComparator $comparator |
||
578 | */ |
||
579 | protected function dropForeigns(AbstractTable $table, $comparator) |
||
590 | |||
591 | /** |
||
592 | * Applied to every column in order to make sure that driver support it. |
||
593 | * |
||
594 | * @param AbstractColumn $column |
||
595 | * |
||
596 | * @throws DriverException |
||
597 | */ |
||
598 | protected function assertValid(AbstractColumn $column) |
||
602 | |||
603 | /** |
||
604 | * @param AbstractTable $table |
||
605 | * @param int $behaviour |
||
606 | * @param StateComparator $comparator |
||
607 | */ |
||
608 | protected function dropConstrains( |
||
625 | |||
626 | /** |
||
627 | * @param AbstractTable $table |
||
628 | * @param int $behaviour |
||
629 | * @param StateComparator $comparator |
||
630 | */ |
||
631 | protected function setConstrains( |
||
652 | } |