Complex classes like TableState 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 TableState, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class TableState |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $name = ''; |
||
26 | |||
27 | /** |
||
28 | * @var AbstractColumn[] |
||
29 | */ |
||
30 | private $columns = []; |
||
31 | |||
32 | /** |
||
33 | * @var AbstractIndex[] |
||
34 | */ |
||
35 | private $indexes = []; |
||
36 | |||
37 | /** |
||
38 | * @var AbstractReference[] |
||
39 | */ |
||
40 | private $foreigns = []; |
||
41 | |||
42 | /** |
||
43 | * Primary key columns are stored separately from other indexes and can be modified only during |
||
44 | * table creation. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | private $primaryKeys = []; |
||
49 | |||
50 | /** |
||
51 | * @param string $name |
||
52 | */ |
||
53 | public function __construct(string $name) |
||
57 | |||
58 | /** |
||
59 | * Set table name. Operation will be applied at moment of saving. |
||
60 | * |
||
61 | * @param string $name |
||
62 | */ |
||
63 | public function setName(string $name) |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function getName() |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | * |
||
79 | * Array key points to initial element name. |
||
80 | * |
||
81 | * @return AbstractColumn[] |
||
82 | */ |
||
83 | public function getColumns() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | * |
||
91 | * Array key points to initial element name. |
||
92 | * |
||
93 | * @return AbstractIndex[] |
||
94 | */ |
||
95 | public function getIndexes() |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | * |
||
103 | * Array key points to initial element name. |
||
104 | * |
||
105 | * @return AbstractReference[] |
||
106 | */ |
||
107 | public function getForeigns() |
||
111 | |||
112 | /** |
||
113 | * Set table primary keys. |
||
114 | * |
||
115 | * @param array $columns |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setPrimaryKeys(array $columns) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | * |
||
129 | * Method combines primary keys with primary keys automatically calculated based on registred |
||
130 | * columns. |
||
131 | */ |
||
132 | public function getPrimaryKeys() |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function knowsColumn(string $name): bool |
||
156 | |||
157 | /** |
||
158 | * @param string $name |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function knowsIndex(string $name): bool |
||
166 | |||
167 | /** |
||
168 | * @param string $name |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function knowsForeign(string $name): bool |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | * |
||
180 | * Lookup is performed based on initial column name. |
||
181 | */ |
||
182 | public function hasColumn(string $name): bool |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function hasIndex(array $columns = []): bool |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function hasForeign($column): bool |
||
202 | |||
203 | /** |
||
204 | * Register new column element. |
||
205 | * |
||
206 | * @param AbstractColumn $column |
||
207 | * |
||
208 | * @return AbstractColumn |
||
209 | */ |
||
210 | public function registerColumn(AbstractColumn $column): AbstractColumn |
||
216 | |||
217 | /** |
||
218 | * Register new index element. |
||
219 | * |
||
220 | * @param AbstractIndex $index |
||
221 | * |
||
222 | * @return AbstractIndex |
||
223 | */ |
||
224 | public function registerIndex(AbstractIndex $index): AbstractIndex |
||
230 | |||
231 | /** |
||
232 | * Register new foreign key element. |
||
233 | * |
||
234 | * @param AbstractReference $foreign |
||
235 | * |
||
236 | * @return AbstractReference |
||
237 | */ |
||
238 | public function registerReference(AbstractReference $foreign): AbstractReference |
||
244 | |||
245 | /** |
||
246 | * Drop column from table schema. |
||
247 | * |
||
248 | * @param AbstractColumn $column |
||
249 | * |
||
250 | * @return self |
||
251 | */ |
||
252 | public function forgetColumn(AbstractColumn $column): TableState |
||
263 | |||
264 | /** |
||
265 | * Drop index from table schema using it's name or forming columns. |
||
266 | * |
||
267 | * @param AbstractIndex $index |
||
268 | * |
||
269 | * @return self |
||
270 | */ |
||
271 | public function forgetIndex(AbstractIndex $index): TableState |
||
282 | |||
283 | /** |
||
284 | * Drop foreign key from table schema using it's forming column. |
||
285 | * |
||
286 | * @param AbstractReference $foreign |
||
287 | * |
||
288 | * @return self |
||
289 | */ |
||
290 | public function forgetForeign(AbstractReference $foreign): TableState |
||
301 | |||
302 | /** |
||
303 | * Find column by it's name or return null. |
||
304 | * |
||
305 | * @param string $name |
||
306 | * |
||
307 | * @return null|AbstractColumn |
||
308 | */ |
||
309 | public function findColumn(string $name) |
||
319 | |||
320 | /** |
||
321 | * Find index by it's columns or return null. |
||
322 | * |
||
323 | * @param array $columns |
||
324 | * |
||
325 | * @return null|AbstractIndex |
||
326 | */ |
||
327 | public function findIndex(array $columns) |
||
337 | |||
338 | /** |
||
339 | * Find foreign key by it's column or return null. |
||
340 | * |
||
341 | * @param string $column |
||
342 | * |
||
343 | * @return null|AbstractReference |
||
344 | */ |
||
345 | public function findForeign(string $column) |
||
355 | |||
356 | /** |
||
357 | * Remount elements under their current name. |
||
358 | */ |
||
359 | public function remountElements() |
||
380 | |||
381 | /** |
||
382 | * Re-populate schema elements using other state as source. Elements will be cloned under their |
||
383 | * schema name. |
||
384 | * |
||
385 | * @param self $source |
||
386 | * |
||
387 | * @return self |
||
388 | */ |
||
389 | public function syncState(self $source): self |
||
411 | } |
||
412 |