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 |
||
| 18 | class TableState extends Component |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $name = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var AbstractColumn[] |
||
| 27 | */ |
||
| 28 | private $columns = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var AbstractIndex[] |
||
| 32 | */ |
||
| 33 | private $indexes = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var AbstractReference[] |
||
| 37 | */ |
||
| 38 | private $foreigns = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Primary key columns are stored separately from other indexes and can be modified only during |
||
| 42 | * table creation. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $primaryKeys = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $name |
||
| 50 | */ |
||
| 51 | public function __construct($name) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set table name. Operation will be applied at moment of saving. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | */ |
||
| 61 | public function setName($name) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function getName() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | * |
||
| 77 | * Array key points to initial element name. |
||
| 78 | * |
||
| 79 | * @return AbstractColumn[] |
||
| 80 | */ |
||
| 81 | public function getColumns() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | * |
||
| 89 | * Array key points to initial element name. |
||
| 90 | * |
||
| 91 | * @return AbstractIndex[] |
||
| 92 | */ |
||
| 93 | public function getIndexes() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | * |
||
| 101 | * Array key points to initial element name. |
||
| 102 | * |
||
| 103 | * @return AbstractReference[] |
||
| 104 | */ |
||
| 105 | public function getForeigns() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set table primary keys. |
||
| 112 | * |
||
| 113 | * @param array $columns |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function setPrimaryKeys(array $columns) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function getPrimaryKeys() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $name |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function knowsColumn($name) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $name |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function knowsIndex($name) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $name |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function knowsForeign($name) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | * |
||
| 165 | * Lookup is performed based on initial column name. |
||
| 166 | */ |
||
| 167 | public function hasColumn($name) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function hasIndex(array $columns = []) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function hasForeign($column) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Register new column element. |
||
| 190 | * |
||
| 191 | * @param AbstractColumn $column |
||
| 192 | * |
||
| 193 | * @return AbstractColumn |
||
| 194 | */ |
||
| 195 | protected function registerColumn(AbstractColumn $column) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Register new index element. |
||
| 204 | * |
||
| 205 | * @param AbstractIndex $index |
||
| 206 | * |
||
| 207 | * @return AbstractIndex |
||
| 208 | */ |
||
| 209 | protected function registerIndex(AbstractIndex $index) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Register new foreign key element. |
||
| 218 | * |
||
| 219 | * @param AbstractReference $foreign |
||
| 220 | * |
||
| 221 | * @return AbstractReference |
||
| 222 | */ |
||
| 223 | protected function registerReference(AbstractReference $foreign) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Drop column from table schema. |
||
| 232 | * |
||
| 233 | * @param AbstractColumn $column |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | protected function forgetColumn(AbstractColumn $column) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Drop index from table schema using it's name or forming columns. |
||
| 251 | * |
||
| 252 | * @param AbstractIndex $index |
||
| 253 | * |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | protected function forgetIndex(AbstractIndex $index) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Drop foreign key from table schema using it's forming column. |
||
| 270 | * |
||
| 271 | * @param AbstractReference $foreign |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | protected function forgetForeign(AbstractReference $foreign) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Find column by it's name or return null. |
||
| 289 | * |
||
| 290 | * @param string $name |
||
| 291 | * |
||
| 292 | * @return null|AbstractColumn |
||
| 293 | */ |
||
| 294 | protected function findColumn($name) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Find index by it's columns or return null. |
||
| 307 | * |
||
| 308 | * @param array $columns |
||
| 309 | * |
||
| 310 | * @return null|AbstractIndex |
||
| 311 | */ |
||
| 312 | protected function findIndex(array $columns) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Find foreign key by it's column or return null. |
||
| 325 | * |
||
| 326 | * @param string $column |
||
| 327 | * |
||
| 328 | * @return null|AbstractReference |
||
| 329 | */ |
||
| 330 | protected function findForeign($column) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Remount elements under their current name. |
||
| 343 | * |
||
| 344 | * @return self |
||
| 345 | */ |
||
| 346 | protected function remountElements() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Re-populate schema elements using other state as source. Elements will be cloned under their |
||
| 370 | * schema name. |
||
| 371 | * |
||
| 372 | * @param TableState $source |
||
| 373 | * |
||
| 374 | * @return self |
||
| 375 | */ |
||
| 376 | protected function syncSchema(TableState $source) |
||
| 398 | } |
||
| 399 |