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 |
||
15 | class TableState extends Component |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $name = ''; |
||
21 | |||
22 | /** |
||
23 | * @var AbstractColumn[] |
||
24 | */ |
||
25 | private $columns = []; |
||
26 | |||
27 | /** |
||
28 | * @var AbstractIndex[] |
||
29 | */ |
||
30 | private $indexes = []; |
||
31 | |||
32 | /** |
||
33 | * @var AbstractReference[] |
||
34 | */ |
||
35 | private $foreigns = []; |
||
36 | |||
37 | /** |
||
38 | * Primary key columns are stored separately from other indexes and can be modified only during |
||
39 | * table creation. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $primaryKeys = []; |
||
44 | |||
45 | /** |
||
46 | * @param string $name |
||
47 | */ |
||
48 | public function __construct($name) |
||
52 | |||
53 | /** |
||
54 | * Set table name. Operation will be applied at moment of saving. |
||
55 | * |
||
56 | * @param string $name |
||
57 | */ |
||
58 | public function setName($name) |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function getName() |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | * |
||
74 | * Array key points to initial element name. |
||
75 | * |
||
76 | * @return AbstractColumn[] |
||
77 | */ |
||
78 | public function getColumns() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | * |
||
86 | * Array key points to initial element name. |
||
87 | * |
||
88 | * @return AbstractIndex[] |
||
89 | */ |
||
90 | public function getIndexes() |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | * |
||
98 | * Array key points to initial element name. |
||
99 | * |
||
100 | * @return AbstractReference[] |
||
101 | */ |
||
102 | public function getForeigns() |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getPrimaryKeys() |
||
114 | |||
115 | /** |
||
116 | * Set table primary keys. |
||
117 | * |
||
118 | * @param array $columns |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setPrimaryKeys(array $columns) |
||
127 | |||
128 | /** |
||
129 | * @param string $name |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function knowsColumn($name) |
||
136 | |||
137 | /** |
||
138 | * @param string $name |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function knowsIndex($name) |
||
145 | |||
146 | /** |
||
147 | * @param string $name |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function knowsForeign($name) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | * |
||
158 | * Lookup is performed based on initial column name. |
||
159 | */ |
||
160 | public function hasColumn($name) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function hasIndex(array $columns = []) |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function hasForeign($column) |
||
180 | |||
181 | /** |
||
182 | * Register new column element. |
||
183 | * |
||
184 | * @param AbstractColumn $column |
||
185 | * @return AbstractColumn |
||
186 | */ |
||
187 | protected function registerColumn(AbstractColumn $column) |
||
193 | |||
194 | /** |
||
195 | * Register new index element. |
||
196 | * |
||
197 | * @param AbstractIndex $index |
||
198 | * @return AbstractIndex |
||
199 | */ |
||
200 | protected function registerIndex(AbstractIndex $index) |
||
206 | |||
207 | /** |
||
208 | * Register new foreign key element. |
||
209 | * |
||
210 | * @param AbstractReference $foreign |
||
211 | * @return AbstractReference |
||
212 | */ |
||
213 | protected function registerReference(AbstractReference $foreign) |
||
219 | |||
220 | /** |
||
221 | * Drop column from table schema. |
||
222 | * |
||
223 | * @param AbstractColumn $column |
||
224 | * @return $this |
||
225 | */ |
||
226 | protected function forgetColumn(AbstractColumn $column) |
||
237 | |||
238 | /** |
||
239 | * Drop index from table schema using it's name or forming columns. |
||
240 | * |
||
241 | * @param AbstractIndex $index |
||
242 | * @return $this |
||
243 | */ |
||
244 | protected function forgetIndex(AbstractIndex $index) |
||
255 | |||
256 | /** |
||
257 | * Drop foreign key from table schema using it's forming column. |
||
258 | * |
||
259 | * @param AbstractReference $foreign |
||
260 | * @return $this |
||
261 | */ |
||
262 | protected function forgetForeign(AbstractReference $foreign) |
||
273 | |||
274 | /** |
||
275 | * Find column by it's name or return null. |
||
276 | * |
||
277 | * @param string $name |
||
278 | * @return null|AbstractColumn |
||
279 | */ |
||
280 | protected function findColumn($name) |
||
290 | |||
291 | /** |
||
292 | * Find index by it's columns or return null. |
||
293 | * |
||
294 | * @param array $columns |
||
295 | * @return null|AbstractIndex |
||
296 | */ |
||
297 | protected function findIndex(array $columns) |
||
307 | |||
308 | /** |
||
309 | * Find foreign key by it's column or return null. |
||
310 | * |
||
311 | * @param string $column |
||
312 | * @return null|AbstractReference |
||
313 | */ |
||
314 | protected function findForeign($column) |
||
324 | |||
325 | /** |
||
326 | * Remount elements under their current name. |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | protected function remountElements() |
||
351 | |||
352 | /** |
||
353 | * Re-populate schema elements using other state as source. Elements will be cloned under their |
||
354 | * schema name. |
||
355 | * |
||
356 | * @param TableState $source |
||
357 | * @return self |
||
358 | */ |
||
359 | protected function syncSchema(TableState $source) |
||
381 | } |