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 |
||
| 16 | class TableState |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $name = ''; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var ColumnInterface[] |
||
| 25 | */ |
||
| 26 | private $columns = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var IndexInterface[] |
||
| 30 | */ |
||
| 31 | private $indexes = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ReferenceInterface[] |
||
| 35 | */ |
||
| 36 | private $foreigns = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Primary key columns are stored separately from other indexes and can be modified only during |
||
| 40 | * table creation. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $primaryKeys = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $name |
||
| 48 | */ |
||
| 49 | public function __construct(string $name) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set table name. Operation will be applied at moment of saving. |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | */ |
||
| 59 | public function setName(string $name) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | public function getName() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | * |
||
| 75 | * Array key points to initial element name. |
||
| 76 | * |
||
| 77 | * @return ColumnInterface[] |
||
| 78 | */ |
||
| 79 | public function getColumns() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | * |
||
| 87 | * Array key points to initial element name. |
||
| 88 | * |
||
| 89 | * @return IndexInterface[] |
||
| 90 | */ |
||
| 91 | public function getIndexes() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | * |
||
| 99 | * Array key points to initial element name. |
||
| 100 | * |
||
| 101 | * @return ReferenceInterface[] |
||
| 102 | */ |
||
| 103 | public function getForeigns() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set table primary keys. |
||
| 110 | * |
||
| 111 | * @param array $columns |
||
| 112 | * |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setPrimaryKeys(array $columns) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function getPrimaryKeys() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $name |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function knowsColumn(string $name): bool |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function knowsIndex(string $name): bool |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $name |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function knowsForeign(string $name): bool |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | * |
||
| 163 | * Lookup is performed based on initial column name. |
||
| 164 | */ |
||
| 165 | public function hasColumn(string $name): bool |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function hasIndex(array $columns = []): bool |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function hasForeign($column): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Register new column element. |
||
| 188 | * |
||
| 189 | * @param ColumnInterface $column |
||
| 190 | * |
||
| 191 | * @return ColumnInterface |
||
| 192 | */ |
||
| 193 | public function registerColumn(ColumnInterface $column): ColumnInterface |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Register new index element. |
||
| 202 | * |
||
| 203 | * @param IndexInterface $index |
||
| 204 | * |
||
| 205 | * @return IndexInterface |
||
| 206 | */ |
||
| 207 | public function registerIndex(IndexInterface $index): IndexInterface |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Register new foreign key element. |
||
| 216 | * |
||
| 217 | * @param ReferenceInterface $foreign |
||
| 218 | * |
||
| 219 | * @return ReferenceInterface |
||
| 220 | */ |
||
| 221 | public function registerReference(ReferenceInterface $foreign): ReferenceInterface |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Drop column from table schema. |
||
| 230 | * |
||
| 231 | * @param ColumnInterface $column |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | public function forgetColumn(ColumnInterface $column): TableState |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Drop index from table schema using it's name or forming columns. |
||
| 249 | * |
||
| 250 | * @param IndexInterface $index |
||
| 251 | * |
||
| 252 | * @return self |
||
| 253 | */ |
||
| 254 | public function forgetIndex(IndexInterface $index): TableState |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Drop foreign key from table schema using it's forming column. |
||
| 268 | * |
||
| 269 | * @param ReferenceInterface $foreign |
||
| 270 | * |
||
| 271 | * @return self |
||
| 272 | */ |
||
| 273 | public function forgetForeign(ReferenceInterface $foreign): TableState |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Find column by it's name or return null. |
||
| 287 | * |
||
| 288 | * @param string $name |
||
| 289 | * |
||
| 290 | * @return null|ColumnInterface |
||
| 291 | */ |
||
| 292 | public function findColumn(string $name) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Find index by it's columns or return null. |
||
| 305 | * |
||
| 306 | * @param array $columns |
||
| 307 | * |
||
| 308 | * @return null|IndexInterface |
||
| 309 | */ |
||
| 310 | public function findIndex(array $columns) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Find foreign key by it's column or return null. |
||
| 323 | * |
||
| 324 | * @param string $column |
||
| 325 | * |
||
| 326 | * @return null|ReferenceInterface |
||
| 327 | */ |
||
| 328 | public function findForeign(string $column) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Remount elements under their current name. |
||
| 341 | */ |
||
| 342 | public function remountElements() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Re-populate schema elements using other state as source. Elements will be cloned under their |
||
| 366 | * schema name. |
||
| 367 | * |
||
| 368 | * @param self $source |
||
| 369 | * |
||
| 370 | * @return self |
||
| 371 | */ |
||
| 372 | public function syncState(self $source): self |
||
| 394 | } |
||
| 395 |