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 |
||
52 | abstract class AbstractTable implements TableInterface |
||
53 | { |
||
54 | /** |
||
55 | * Table states. |
||
56 | */ |
||
57 | const STATUS_NEW = 0; |
||
58 | const STATUS_EXISTS = 1; |
||
59 | const STATUS_DROPPED = 2; |
||
60 | |||
61 | /** |
||
62 | * Indication that table is exists and current schema is fetched from database. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | private $status = self::STATUS_NEW; |
||
67 | |||
68 | /** |
||
69 | * Database specific tablePrefix. Required for table renames. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $prefix = ''; |
||
74 | |||
75 | /** |
||
76 | * @invisible |
||
77 | * |
||
78 | * @var Driver |
||
79 | */ |
||
80 | protected $driver = null; |
||
81 | |||
82 | /** |
||
83 | * Initial table state. |
||
84 | * |
||
85 | * @invisible |
||
86 | * @var TableState |
||
87 | */ |
||
88 | protected $initial = null; |
||
89 | |||
90 | /** |
||
91 | * Currently defined table state. |
||
92 | * |
||
93 | * @invisible |
||
94 | * @var TableState |
||
95 | */ |
||
96 | protected $current = null; |
||
97 | |||
98 | /** |
||
99 | * @param Driver $driver Parent driver. |
||
100 | * @param string $name Table name, must include table prefix. |
||
101 | * @param string $prefix Database specific table prefix. |
||
102 | */ |
||
103 | public function __construct(Driver $driver, string $name, string $prefix) |
||
104 | { |
||
105 | $this->driver = $driver; |
||
106 | $this->prefix = $prefix; |
||
107 | |||
108 | //Initializing states |
||
109 | $this->initial = new TableState($this->prefix . $name); |
||
110 | $this->current = new TableState($this->prefix . $name); |
||
111 | |||
112 | if ($this->driver->hasTable($this->getName())) { |
||
113 | $this->status = self::STATUS_EXISTS; |
||
114 | } |
||
115 | |||
116 | if ($this->exists()) { |
||
117 | //Initiating table schema |
||
118 | $this->initSchema($this->initial); |
||
119 | } |
||
120 | |||
121 | $this->setState($this->initial); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get instance of associated driver. |
||
126 | * |
||
127 | * @return Driver |
||
128 | */ |
||
129 | public function getDriver(): Driver |
||
130 | { |
||
131 | return $this->driver; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Return database specific table prefix. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getPrefix(): string |
||
140 | { |
||
141 | return $this->prefix; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return StateComparator |
||
146 | */ |
||
147 | public function getComparator(): StateComparator |
||
148 | { |
||
149 | return new StateComparator($this->initial, $this->current); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Check if table schema has been modified since synchronization. |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | protected function hasChanges(): bool |
||
158 | { |
||
159 | return $this->getComparator()->hasChanges(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function exists(): bool |
||
166 | { |
||
167 | //Derlared as dropped != actually dropped |
||
168 | return $this->status == self::STATUS_EXISTS || $this->status == self::STATUS_DROPPED; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Table status (see codes above). |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function getStatus(): int |
||
177 | { |
||
178 | return $this->status; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Sets table name. Use this function in combination with save to rename table. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * |
||
186 | * @return string Prefixed table name. |
||
187 | */ |
||
188 | public function setName(string $name): string |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function getName(): string |
||
202 | |||
203 | /** |
||
204 | * Table name before rename. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getInitialName(): string |
||
212 | |||
213 | /** |
||
214 | * Declare table as dropped, you have to sync table using "save" method in order to apply this |
||
215 | * change. |
||
216 | */ |
||
217 | public function declareDropped() |
||
226 | |||
227 | /** |
||
228 | * Set table primary keys. Operation can only be applied for newly created tables. Now every |
||
229 | * database might support compound indexes. |
||
230 | * |
||
231 | * @param array $columns |
||
232 | * |
||
233 | * @return self |
||
234 | */ |
||
235 | public function setPrimaryKeys(array $columns): AbstractTable |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function getPrimaryKeys(): array |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function hasColumn(string $name): bool |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | * |
||
265 | * @return AbstractColumn[] |
||
266 | */ |
||
267 | public function getColumns(): array |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function hasIndex(array $columns = []): bool |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | * |
||
283 | * @return AbstractIndex[] |
||
284 | */ |
||
285 | public function getIndexes(): array |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function hasForeign(string $column): bool |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | * |
||
301 | * @return AbstractReference[] |
||
302 | */ |
||
303 | public function getForeigns(): array |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function getDependencies(): array |
||
320 | |||
321 | /** |
||
322 | * Get/create instance of AbstractColumn associated with current table. |
||
323 | * |
||
324 | * Attention, renamed column will be available by it's old name until being synced! |
||
325 | * |
||
326 | * Examples: |
||
327 | * $table->column('name')->string(); |
||
328 | * |
||
329 | * @param string $name |
||
330 | * |
||
331 | * @return AbstractColumn |
||
332 | */ |
||
333 | public function column(string $name): AbstractColumn |
||
345 | |||
346 | /** |
||
347 | * Shortcut for column() method. |
||
348 | * |
||
349 | * @param string $column |
||
350 | * |
||
351 | * @return AbstractColumn |
||
352 | */ |
||
353 | public function __get(string $column) |
||
357 | |||
358 | /** |
||
359 | * Column creation/altering shortcut, call chain is identical to: |
||
360 | * AbstractTable->column($name)->$type($arguments). |
||
361 | * |
||
362 | * Example: |
||
363 | * $table->string("name"); |
||
364 | * $table->text("some_column"); |
||
365 | * |
||
366 | * @param string $type |
||
367 | * @param array $arguments Type specific parameters. |
||
368 | * |
||
369 | * @return AbstractColumn |
||
370 | */ |
||
371 | public function __call(string $type, array $arguments) |
||
378 | |||
379 | /** |
||
380 | * Get/create instance of AbstractIndex associated with current table based on list of forming |
||
381 | * column names. |
||
382 | * |
||
383 | * Example: |
||
384 | * $table->index(['key']); |
||
385 | * $table->index(['key', 'key2']); |
||
386 | * |
||
387 | * @param array $columns List of index columns. |
||
388 | * |
||
389 | * @return AbstractIndex |
||
390 | * |
||
391 | * @throws SchemaException |
||
392 | */ |
||
393 | public function index(array $columns): AbstractIndex |
||
419 | |||
420 | /** |
||
421 | * Get/create instance of AbstractReference associated with current table based on local column |
||
422 | * name. |
||
423 | * |
||
424 | * @param string $column |
||
425 | * |
||
426 | * @return AbstractReference |
||
427 | * |
||
428 | * @throws SchemaException |
||
429 | */ |
||
430 | public function foreign(string $column): AbstractReference |
||
457 | |||
458 | /** |
||
459 | * Rename column (only if column exists). |
||
460 | * |
||
461 | * @param string $column |
||
462 | * @param string $name New column name. |
||
463 | * |
||
464 | * @return self |
||
465 | * |
||
466 | * @throws SchemaException |
||
467 | */ |
||
468 | public function renameColumn(string $column, string $name): AbstractTable |
||
479 | |||
480 | /** |
||
481 | * Rename index (only if index exists). |
||
482 | * |
||
483 | * @param array $columns Index forming columns. |
||
484 | * @param string $name New index name. |
||
485 | * |
||
486 | * @return self |
||
487 | * |
||
488 | * @throws SchemaException |
||
489 | */ |
||
490 | public function renameIndex(array $columns, string $name): AbstractTable |
||
503 | |||
504 | /** |
||
505 | * Drop column by it's name. |
||
506 | * |
||
507 | * @param string $column |
||
508 | * |
||
509 | * @return self |
||
510 | * |
||
511 | * @throws SchemaException |
||
512 | */ |
||
513 | public function dropColumn(string $column): AbstractTable |
||
524 | |||
525 | /** |
||
526 | * Drop index by it's forming columns. |
||
527 | * |
||
528 | * @param array $columns |
||
529 | * |
||
530 | * @return self |
||
531 | * |
||
532 | * @throws SchemaException |
||
533 | */ |
||
534 | public function dropIndex(array $columns): AbstractTable |
||
547 | |||
548 | /** |
||
549 | * Drop foreign key by it's name. |
||
550 | * |
||
551 | * @param string $column |
||
552 | * |
||
553 | * @return self |
||
554 | * |
||
555 | * @throws SchemaException |
||
556 | */ |
||
557 | public function dropForeign($column): AbstractTable |
||
570 | |||
571 | /** |
||
572 | * Get current table state (detached). |
||
573 | * |
||
574 | * @return TableState |
||
575 | */ |
||
576 | public function getState(): TableState |
||
583 | |||
584 | /** |
||
585 | * Reset table state to new form. |
||
586 | * |
||
587 | * @param TableState $state Use null to flush table schema. |
||
588 | * |
||
589 | * @return self|$this |
||
590 | */ |
||
591 | public function setState(TableState $state = null): AbstractTable |
||
602 | |||
603 | /** |
||
604 | * Reset table state to it initial form. |
||
605 | * |
||
606 | * @return self|$this |
||
607 | */ |
||
608 | public function resetState(): AbstractTable |
||
614 | |||
615 | /** |
||
616 | * Save table schema including every column, index, foreign key creation/altering. If table |
||
617 | * does not exist it must be created. If table declared as dropped it will be removed from |
||
618 | * the database. |
||
619 | * |
||
620 | * @param int $behaviour Operation to be performed while table being saved. In some |
||
621 | * cases (when multiple tables are being updated) it is |
||
622 | * reasonable to drop foreing keys and indexes prior to |
||
623 | * dropping related columns. See sync bus class to get more |
||
624 | * details. |
||
625 | * @param LoggerInterface $logger Optional, aggregates messages for data syncing. |
||
626 | * @param bool $reset When true schema will be marked as synced. |
||
627 | * |
||
628 | * @throws HandlerException |
||
629 | * |
||
630 | * @throws SchemaException |
||
631 | */ |
||
632 | public function save( |
||
669 | |||
670 | /** |
||
671 | * Ensure that no wrong indexes left in table. |
||
672 | * |
||
673 | * @param bool $withForeigns |
||
674 | * |
||
675 | * @return AbstractTable |
||
676 | */ |
||
677 | protected function prepareSchema(bool $withForeigns = true) |
||
741 | |||
742 | /** |
||
743 | * @return AbstractColumn|string |
||
744 | */ |
||
745 | public function __toString(): string |
||
749 | |||
750 | /** |
||
751 | * Cloning schemas as well. |
||
752 | */ |
||
753 | public function __clone() |
||
758 | |||
759 | /** |
||
760 | * @return array |
||
761 | */ |
||
762 | public function __debugInfo() |
||
772 | |||
773 | /** |
||
774 | * Populate table schema with values from database. |
||
775 | * |
||
776 | * @param TableState $state |
||
777 | */ |
||
778 | protected function initSchema(TableState $state) |
||
796 | |||
797 | /** |
||
798 | * Fetch index declarations from database. |
||
799 | * |
||
800 | * @return AbstractColumn[] |
||
801 | */ |
||
802 | abstract protected function fetchColumns(): array; |
||
803 | |||
804 | /** |
||
805 | * Fetch index declarations from database. |
||
806 | * |
||
807 | * @return AbstractIndex[] |
||
808 | */ |
||
809 | abstract protected function fetchIndexes(): array; |
||
810 | |||
811 | /** |
||
812 | * Fetch references declaration from database. |
||
813 | * |
||
814 | * @return AbstractReference[] |
||
815 | */ |
||
816 | abstract protected function fetchReferences(): array; |
||
817 | |||
818 | /** |
||
819 | * Fetch names of primary keys from table. |
||
820 | * |
||
821 | * @return array |
||
822 | */ |
||
823 | abstract protected function fetchPrimaryKeys(): array; |
||
824 | |||
825 | /** |
||
826 | * Create column with a given name. |
||
827 | * |
||
828 | * @param string $name |
||
829 | * |
||
830 | * @return AbstractColumn |
||
831 | */ |
||
832 | abstract protected function createColumn(string $name): AbstractColumn; |
||
833 | |||
834 | /** |
||
835 | * Create index for a given set of columns. |
||
836 | * |
||
837 | * @param string $name |
||
838 | * |
||
839 | * @return AbstractIndex |
||
840 | */ |
||
841 | abstract protected function createIndex(string $name): AbstractIndex; |
||
842 | |||
843 | /** |
||
844 | * Create reference on a given column set. |
||
845 | * |
||
846 | * @param string $name |
||
847 | * |
||
848 | * @return AbstractReference |
||
849 | */ |
||
850 | abstract protected function createForeign(string $name): AbstractReference; |
||
851 | |||
852 | /** |
||
853 | * Generate unique name for indexes and foreign keys. |
||
854 | * |
||
855 | * @param string $type |
||
856 | * @param array $columns |
||
857 | * |
||
858 | * @return string |
||
859 | */ |
||
860 | protected function createIdentifier(string $type, array $columns): string |
||
871 | } |
||
872 |