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 | * {@inheritdoc} |
||
| 149 | * |
||
| 150 | * Lookup is performed based on initial column name. |
||
| 151 | */ |
||
| 152 | public function hasColumn(string $name): bool |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function hasIndex(array $columns = []): bool |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function hasForeign($column): bool |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Register new column element. |
||
| 175 | * |
||
| 176 | * @param AbstractColumn $column |
||
| 177 | * |
||
| 178 | * @return AbstractColumn |
||
| 179 | */ |
||
| 180 | public function registerColumn(AbstractColumn $column): AbstractColumn |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Register new index element. |
||
| 189 | * |
||
| 190 | * @param AbstractIndex $index |
||
| 191 | * |
||
| 192 | * @return AbstractIndex |
||
| 193 | */ |
||
| 194 | public function registerIndex(AbstractIndex $index): AbstractIndex |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Register new foreign key element. |
||
| 203 | * |
||
| 204 | * @param AbstractReference $foreign |
||
| 205 | * |
||
| 206 | * @return AbstractReference |
||
| 207 | */ |
||
| 208 | public function registerForeign(AbstractReference $foreign): AbstractReference |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Drop column from table schema. |
||
| 217 | * |
||
| 218 | * @param AbstractColumn $column |
||
| 219 | * |
||
| 220 | * @return self |
||
| 221 | */ |
||
| 222 | public function forgetColumn(AbstractColumn $column): TableState |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Drop index from table schema using it's name or forming columns. |
||
| 236 | * |
||
| 237 | * @param AbstractIndex $index |
||
| 238 | * |
||
| 239 | * @return self |
||
| 240 | */ |
||
| 241 | public function forgetIndex(AbstractIndex $index): TableState |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Drop foreign key from table schema using it's forming column. |
||
| 255 | * |
||
| 256 | * @param AbstractReference $foreign |
||
| 257 | * |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | public function forgetForeign(AbstractReference $foreign): TableState |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Find column by it's name or return null. |
||
| 274 | * |
||
| 275 | * @param string $name |
||
| 276 | * |
||
| 277 | * @return null|AbstractColumn |
||
| 278 | */ |
||
| 279 | public function findColumn(string $name) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Find index by it's columns or return null. |
||
| 292 | * |
||
| 293 | * @param array $columns |
||
| 294 | * |
||
| 295 | * @return null|AbstractIndex |
||
| 296 | */ |
||
| 297 | public function findIndex(array $columns) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Find foreign key by it's column or return null. |
||
| 310 | * |
||
| 311 | * @param string $column |
||
| 312 | * |
||
| 313 | * @return null|AbstractReference |
||
| 314 | */ |
||
| 315 | public function findForeign(string $column) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Remount elements under their current name. |
||
| 328 | */ |
||
| 329 | public function remountElements() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Re-populate schema elements using other state as source. Elements will be cloned under their |
||
| 353 | * schema name. |
||
| 354 | * |
||
| 355 | * @param TableState $source |
||
| 356 | * |
||
| 357 | * @return self |
||
| 358 | */ |
||
| 359 | public function syncState(TableState $source): self |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Cloning all elements. |
||
| 386 | */ |
||
| 387 | public function __clone() |
||
| 401 | } |
||
| 402 |