Complex classes like AbstractTable 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 AbstractTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | abstract class AbstractTable implements TableInterface |
||
51 | { |
||
52 | /** |
||
53 | * Table states. |
||
54 | */ |
||
55 | const STATUS_NEW = 0; |
||
56 | const STATUS_EXISTS = 1; |
||
57 | const STATUS_DROPPED = 2; |
||
58 | |||
59 | /** |
||
60 | * Indication that table is exists and current schema is fetched from database. |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | private $status = self::STATUS_NEW; |
||
65 | |||
66 | /** |
||
67 | * Database specific tablePrefix. Required for table renames. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $prefix = ''; |
||
72 | |||
73 | /** |
||
74 | * @invisible |
||
75 | * |
||
76 | * @var Driver |
||
77 | */ |
||
78 | protected $driver = null; |
||
79 | |||
80 | /** |
||
81 | * Initial table state. |
||
82 | * |
||
83 | * @invisible |
||
84 | * @var TableState |
||
85 | */ |
||
86 | protected $initial = null; |
||
87 | |||
88 | /** |
||
89 | * Currently defined table state. |
||
90 | * |
||
91 | * @invisible |
||
92 | * @var TableState |
||
93 | */ |
||
94 | protected $current = null; |
||
95 | |||
96 | /** |
||
97 | * @param Driver $driver Parent driver. |
||
98 | * @param string $name Table name, must include table prefix. |
||
99 | * @param string $prefix Database specific table prefix. |
||
100 | */ |
||
101 | public function __construct(Driver $driver, string $name, string $prefix) |
||
121 | |||
122 | /** |
||
123 | * Get instance of associated driver. |
||
124 | * |
||
125 | * @return Driver |
||
126 | */ |
||
127 | public function getDriver(): Driver |
||
131 | |||
132 | /** |
||
133 | * Return database specific table prefix. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getPrefix(): string |
||
141 | |||
142 | /** |
||
143 | * @return StateComparator |
||
144 | */ |
||
145 | public function getComparator(): StateComparator |
||
149 | |||
150 | /** |
||
151 | * Check if table schema has been modified since synchronization. |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | protected function hasChanges(): bool |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function exists(): bool |
||
167 | |||
168 | /** |
||
169 | * Table status (see codes above). |
||
170 | * |
||
171 | * @return int |
||
172 | */ |
||
173 | public function getStatus(): int |
||
177 | |||
178 | /** |
||
179 | * Sets table name. Use this function in combination with save to rename table. |
||
180 | * |
||
181 | * @param string $name |
||
182 | * |
||
183 | * @return string Prefixed table name. |
||
184 | */ |
||
185 | public function setName(string $name): string |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function getName(): string |
||
199 | |||
200 | /** |
||
201 | * Table name before rename. |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getInitialName(): string |
||
209 | |||
210 | /** |
||
211 | * Declare table as dropped, you have to sync table using "save" method in order to apply this |
||
212 | * change. |
||
213 | */ |
||
214 | public function declareDropped() |
||
224 | |||
225 | /** |
||
226 | * Set table primary keys. Operation can only be applied for newly created tables. Now every |
||
227 | * database might support compound indexes. |
||
228 | * |
||
229 | * @param array $columns |
||
230 | * |
||
231 | * @return self |
||
232 | */ |
||
233 | public function setPrimaryKeys(array $columns): AbstractTable |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function getPrimaryKeys(): array |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function hasColumn(string $name): bool |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | * |
||
263 | * @return AbstractColumn[] |
||
264 | */ |
||
265 | public function getColumns(): array |
||
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | public function hasIndex(array $columns = []): bool |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | * |
||
281 | * @return AbstractIndex[] |
||
282 | */ |
||
283 | public function getIndexes(): array |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function hasForeign(string $column): bool |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | * |
||
299 | * @return AbstractReference[] |
||
300 | */ |
||
301 | public function getForeigns(): array |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function getDependencies(): array |
||
318 | |||
319 | /** |
||
320 | * Get/create instance of AbstractColumn associated with current table. |
||
321 | * |
||
322 | * Attention, renamed column will be available by it's old name until being synced! |
||
323 | * |
||
324 | * Examples: |
||
325 | * $table->column('name')->string(); |
||
326 | * |
||
327 | * @param string $name |
||
328 | * |
||
329 | * @return AbstractColumn |
||
330 | */ |
||
331 | public function column(string $name): AbstractColumn |
||
343 | |||
344 | /** |
||
345 | * Shortcut for column() method. |
||
346 | * |
||
347 | * @param string $column |
||
348 | * |
||
349 | * @return AbstractColumn |
||
350 | */ |
||
351 | public function __get(string $column) |
||
355 | |||
356 | /** |
||
357 | * Column creation/altering shortcut, call chain is identical to: |
||
358 | * AbstractTable->column($name)->$type($arguments). |
||
359 | * |
||
360 | * Example: |
||
361 | * $table->string("name"); |
||
362 | * $table->text("some_column"); |
||
363 | * |
||
364 | * @param string $type |
||
365 | * @param array $arguments Type specific parameters. |
||
366 | * |
||
367 | * @return AbstractColumn |
||
368 | */ |
||
369 | public function __call(string $type, array $arguments) |
||
376 | |||
377 | /** |
||
378 | * Get/create instance of AbstractIndex associated with current table based on list of forming |
||
379 | * column names. |
||
380 | * |
||
381 | * Example: |
||
382 | * $table->index(['key']); |
||
383 | * $table->index(['key', 'key2']); |
||
384 | * |
||
385 | * @param array $columns List of index columns. |
||
386 | * |
||
387 | * @return AbstractIndex |
||
388 | * |
||
389 | * @throws SchemaException |
||
390 | */ |
||
391 | public function index(array $columns): AbstractIndex |
||
409 | |||
410 | /** |
||
411 | * Get/create instance of AbstractReference associated with current table based on local column |
||
412 | * name. |
||
413 | * |
||
414 | * @param string $column |
||
415 | * |
||
416 | * @return AbstractReference |
||
417 | * |
||
418 | * @throws SchemaException |
||
419 | */ |
||
420 | public function foreign(string $column): AbstractReference |
||
440 | |||
441 | /** |
||
442 | * Rename column (only if column exists). |
||
443 | * |
||
444 | * @param string $column |
||
445 | * @param string $name New column name. |
||
446 | * |
||
447 | * @return self |
||
448 | * |
||
449 | * @throws SchemaException |
||
450 | */ |
||
451 | public function renameColumn(string $column, string $name): AbstractTable |
||
462 | |||
463 | /** |
||
464 | * Rename index (only if index exists). |
||
465 | * |
||
466 | * @param array $columns Index forming columns. |
||
467 | * @param string $name New index name. |
||
468 | * |
||
469 | * @return self |
||
470 | * |
||
471 | * @throws SchemaException |
||
472 | */ |
||
473 | public function renameIndex(array $columns, string $name): AbstractTable |
||
486 | |||
487 | /** |
||
488 | * Drop column by it's name. |
||
489 | * |
||
490 | * @param string $column |
||
491 | * |
||
492 | * @return self |
||
493 | * |
||
494 | * @throws SchemaException |
||
495 | */ |
||
496 | public function dropColumn(string $column): AbstractTable |
||
507 | |||
508 | /** |
||
509 | * Drop index by it's forming columns. |
||
510 | * |
||
511 | * @param array $columns |
||
512 | * |
||
513 | * @return self |
||
514 | * |
||
515 | * @throws SchemaException |
||
516 | */ |
||
517 | public function dropIndex(array $columns): AbstractTable |
||
530 | |||
531 | /** |
||
532 | * Drop foreign key by it's name. |
||
533 | * |
||
534 | * @param string $column |
||
535 | * |
||
536 | * @return self |
||
537 | * |
||
538 | * @throws SchemaException |
||
539 | */ |
||
540 | public function dropForeign($column): AbstractTable |
||
553 | |||
554 | /** |
||
555 | * Get current table state (detached). |
||
556 | * |
||
557 | * @return TableState |
||
558 | */ |
||
559 | public function getState(): TableState |
||
566 | |||
567 | /** |
||
568 | * Reset table state to new form. |
||
569 | * |
||
570 | * @param TableState $state Use null to flush table schema. |
||
571 | * |
||
572 | * @return self|$this |
||
573 | */ |
||
574 | public function setState(TableState $state = null): AbstractTable |
||
585 | |||
586 | /** |
||
587 | * Reset table state to it initial form. |
||
588 | * |
||
589 | * @return self|$this |
||
590 | */ |
||
591 | public function resetState(): AbstractTable |
||
597 | |||
598 | /** |
||
599 | * Save table schema including every column, index, foreign key creation/altering. If table |
||
600 | * does not exist it must be created. If table declared as dropped it will be removed from |
||
601 | * the database. |
||
602 | * |
||
603 | * @param int $behaviour Operation to be performed while table being saved. In some |
||
604 | * cases (when multiple tables are being updated) it is |
||
605 | * reasonable to drop foreing keys and indexes prior to |
||
606 | * dropping related columns. See sync bus class to get more |
||
607 | * details. |
||
608 | * @param LoggerInterface $logger Optional, aggregates messages for data syncing. |
||
609 | * @param bool $reset When true schema will be marked as synced. |
||
610 | * |
||
611 | * @throws HandlerException |
||
612 | * |
||
613 | * @throws SchemaException |
||
614 | */ |
||
615 | public function save( |
||
652 | |||
653 | /** |
||
654 | * Ensure that no wrong indexes left in table. |
||
655 | * |
||
656 | * @param bool $withForeigns |
||
657 | * |
||
658 | * @return AbstractTable |
||
659 | */ |
||
660 | protected function prepareSchema(bool $withForeigns = true) |
||
724 | |||
725 | /** |
||
726 | * @return AbstractColumn|string |
||
727 | */ |
||
728 | public function __toString(): string |
||
732 | |||
733 | /** |
||
734 | * Cloning schemas as well. |
||
735 | */ |
||
736 | public function __clone() |
||
741 | |||
742 | /** |
||
743 | * @return array |
||
744 | */ |
||
745 | public function __debugInfo() |
||
755 | |||
756 | /** |
||
757 | * Populate table schema with values from database. |
||
758 | * |
||
759 | * @param TableState $state |
||
760 | */ |
||
761 | protected function initSchema(TableState $state) |
||
779 | |||
780 | /** |
||
781 | * Fetch index declarations from database. |
||
782 | * |
||
783 | * @return AbstractColumn[] |
||
784 | */ |
||
785 | abstract protected function fetchColumns(): array; |
||
786 | |||
787 | /** |
||
788 | * Fetch index declarations from database. |
||
789 | * |
||
790 | * @return AbstractIndex[] |
||
791 | */ |
||
792 | abstract protected function fetchIndexes(): array; |
||
793 | |||
794 | /** |
||
795 | * Fetch references declaration from database. |
||
796 | * |
||
797 | * @return AbstractReference[] |
||
798 | */ |
||
799 | abstract protected function fetchReferences(): array; |
||
800 | |||
801 | /** |
||
802 | * Fetch names of primary keys from table. |
||
803 | * |
||
804 | * @return array |
||
805 | */ |
||
806 | abstract protected function fetchPrimaryKeys(): array; |
||
807 | |||
808 | /** |
||
809 | * Create column with a given name. |
||
810 | * |
||
811 | * @param string $name |
||
812 | * |
||
813 | * @return AbstractColumn |
||
814 | */ |
||
815 | abstract protected function createColumn(string $name): AbstractColumn; |
||
816 | |||
817 | /** |
||
818 | * Create index for a given set of columns. |
||
819 | * |
||
820 | * @param string $name |
||
821 | * |
||
822 | * @return AbstractIndex |
||
823 | */ |
||
824 | abstract protected function createIndex(string $name): AbstractIndex; |
||
825 | |||
826 | /** |
||
827 | * Create reference on a given column set. |
||
828 | * |
||
829 | * @param string $name |
||
830 | * |
||
831 | * @return AbstractReference |
||
832 | */ |
||
833 | abstract protected function createForeign(string $name): AbstractReference; |
||
834 | |||
835 | /** |
||
836 | * Generate unique name for indexes and foreign keys. |
||
837 | * |
||
838 | * @param string $type |
||
839 | * @param array $columns |
||
840 | * |
||
841 | * @return string |
||
842 | */ |
||
843 | protected function createIdentifier(string $type, array $columns): string |
||
854 | } |