Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Table |
||
| 11 | { |
||
| 12 | protected $data = []; |
||
| 13 | /** |
||
| 14 | * @var TableRelation[] |
||
| 15 | */ |
||
| 16 | protected $relations = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Create a new instance |
||
| 20 | * @param string $name the table name |
||
| 21 | */ |
||
| 22 | 4 | public function __construct(string $name, string $schema = '') |
|
| 23 | { |
||
| 24 | 4 | $this->data = [ |
|
| 25 | 4 | 'name' => $name, |
|
| 26 | 4 | 'schema' => $schema, |
|
| 27 | 'columns' => [], |
||
| 28 | 'primary' => [], |
||
| 29 | 4 | 'comment' => '' |
|
| 30 | ]; |
||
| 31 | 4 | } |
|
| 32 | /** |
||
| 33 | * Get the table comment |
||
| 34 | * @return string the table comment |
||
| 35 | */ |
||
| 36 | public function getComment() |
||
| 37 | { |
||
| 38 | return $this->data['comment']; |
||
| 39 | } |
||
| 40 | /** |
||
| 41 | * Set the table comment |
||
| 42 | * @param string $comment the table comment |
||
| 43 | * @return $this |
||
| 44 | */ |
||
| 45 | 4 | public function setComment(string $comment) |
|
| 46 | { |
||
| 47 | 4 | $this->data['comment'] = $comment; |
|
| 48 | 4 | return $this; |
|
| 49 | } |
||
| 50 | /** |
||
| 51 | * Add a column to the definition |
||
| 52 | * @param string $column the column name |
||
| 53 | * @param array $definition optional array of data associated with the column |
||
| 54 | * @return self |
||
| 55 | */ |
||
| 56 | 4 | public function addColumn(string $column, array $definition = []) : Table |
|
| 57 | { |
||
| 58 | 4 | $this->data['columns'][$column] = TableColumn::fromArray($column, $definition); |
|
| 59 | 4 | return $this; |
|
| 60 | } |
||
| 61 | /** |
||
| 62 | * Add columns to the definition |
||
| 63 | * @param array $columns key - value pairs, where each key is a column name and each value - array of info |
||
| 64 | * @return self |
||
| 65 | */ |
||
| 66 | 4 | public function addColumns(array $columns) : Table |
|
| 67 | { |
||
| 68 | 4 | foreach ($columns as $column => $definition) { |
|
| 69 | 4 | if (is_numeric($column) && is_string($definition)) { |
|
| 70 | $this->addColumn($definition, []); |
||
| 71 | } else { |
||
| 72 | 4 | $this->addColumn($column, $definition); |
|
| 73 | } |
||
| 74 | } |
||
| 75 | 4 | return $this; |
|
| 76 | } |
||
| 77 | /** |
||
| 78 | * Set the primary key |
||
| 79 | * @param array|string $column either a single column name or an array of column names |
||
| 80 | * @return self |
||
| 81 | */ |
||
| 82 | 4 | public function setPrimaryKey($column) : Table |
|
| 83 | { |
||
| 84 | 4 | if (!is_array($column)) { |
|
| 85 | $column = [ $column ]; |
||
| 86 | } |
||
| 87 | 4 | $this->data['primary'] = $column; |
|
| 88 | 4 | return $this; |
|
| 89 | } |
||
| 90 | /** |
||
| 91 | * Get the table schema |
||
| 92 | * @return string the table name |
||
| 93 | */ |
||
| 94 | public function getSchema() |
||
| 95 | { |
||
| 96 | return $this->data['schema']; |
||
| 97 | } |
||
| 98 | /** |
||
| 99 | * Get the table name |
||
| 100 | * @return string the table name |
||
| 101 | */ |
||
| 102 | 136 | public function getName() |
|
| 106 | /** |
||
| 107 | * Get the table name with the schema if available |
||
| 108 | * @return string the table name |
||
| 109 | */ |
||
| 110 | 156 | public function getFullName() |
|
| 114 | /** |
||
| 115 | * Get a column definition |
||
| 116 | * @param string $column the column name to search for |
||
| 117 | * @return array|null the column details or `null` if the column does not exist |
||
| 118 | */ |
||
| 119 | 156 | public function getColumn($column) |
|
| 120 | { |
||
| 121 | 156 | return $this->data['columns'][$column] ?? null; |
|
| 122 | } |
||
| 123 | /** |
||
| 124 | * Get all column names |
||
| 125 | * @return array array of strings, where each element is a column name |
||
| 126 | */ |
||
| 127 | 156 | public function getColumns() |
|
| 128 | { |
||
| 129 | 156 | return array_keys($this->data['columns']); |
|
| 130 | } |
||
| 131 | /** |
||
| 132 | * Get all column definitions |
||
| 133 | * @return array key - value pairs, where each key is a column name and each value - the column data |
||
| 134 | */ |
||
| 135 | 28 | public function getFullColumns() |
|
| 136 | { |
||
| 137 | 28 | return $this->data['columns']; |
|
| 138 | } |
||
| 139 | /** |
||
| 140 | * Get the primary key columns |
||
| 141 | * @return array array of column names |
||
| 142 | */ |
||
| 143 | 156 | public function getPrimaryKey() |
|
| 144 | { |
||
| 145 | 156 | return $this->data['primary']; |
|
| 146 | } |
||
| 147 | /** |
||
| 148 | * Create a relation where each record has zero or one related rows in another table |
||
| 149 | * @param Table $toTable the related table definition |
||
| 150 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 151 | * @param string|array|null $toTableColumn the remote columns pointing to the PK in the current table |
||
| 152 | * @param string|null $sql additional where clauses to use, default to null |
||
| 153 | * @param array $par parameters for the above statement, defaults to [] |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function hasOne( |
||
| 157 | Table $toTable, |
||
| 158 | string $name = null, |
||
| 159 | $toTableColumn = null, |
||
| 160 | string $sql = null, |
||
| 161 | array $par = [] |
||
| 162 | ) : Table { |
||
| 163 | $columns = $toTable->getColumns(); |
||
| 164 | |||
| 165 | $keymap = []; |
||
| 166 | if (!isset($toTableColumn)) { |
||
| 167 | $toTableColumn = []; |
||
| 168 | } |
||
| 169 | if (!is_array($toTableColumn)) { |
||
| 170 | $toTableColumn = [$toTableColumn]; |
||
| 171 | } |
||
| 172 | foreach ($this->getPrimaryKey() as $k => $pkField) { |
||
| 173 | if (isset($toTableColumn[$pkField])) { |
||
| 174 | $key = $toTableColumn[$pkField]; |
||
| 175 | } elseif (isset($toTableColumn[$k])) { |
||
| 176 | $key = $toTableColumn[$k]; |
||
| 177 | } else { |
||
| 178 | $key = $this->getName().'_'.$pkField; |
||
| 179 | } |
||
| 180 | if (!in_array($key, $columns)) { |
||
| 181 | throw new DBException('Missing foreign key mapping'); |
||
| 182 | } |
||
| 183 | $keymap[$pkField] = $key; |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!isset($name)) { |
||
| 187 | $name = $toTable->getName() . '_' . implode('_', array_keys($keymap)); |
||
| 188 | } |
||
| 189 | $this->addRelation(new TableRelation( |
||
| 190 | $name, |
||
| 191 | $toTable, |
||
| 192 | $keymap, |
||
| 193 | false, |
||
| 194 | null, |
||
| 195 | null, |
||
| 196 | $sql, |
||
| 197 | $par |
||
| 198 | )); |
||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | /** |
||
| 202 | * Create a relation where each record has zero, one or more related rows in another table |
||
| 203 | * @param Table $toTable the related table definition |
||
| 204 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 205 | * @param string|array|null $toTableColumn the remote columns pointing to the PK in the current table |
||
| 206 | * @param string|null $sql additional where clauses to use, default to null |
||
| 207 | * @param array $par parameters for the above statement, defaults to [] |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function hasMany( |
||
| 211 | Table $toTable, |
||
| 212 | string $name = null, |
||
| 213 | $toTableColumn = null, |
||
| 214 | $sql = null, |
||
| 215 | array $par = [] |
||
| 216 | ) : Table { |
||
| 217 | $columns = $toTable->getColumns(); |
||
| 218 | |||
| 219 | $keymap = []; |
||
| 220 | if (!isset($toTableColumn)) { |
||
| 221 | $toTableColumn = []; |
||
| 222 | } |
||
| 223 | if (!is_array($toTableColumn)) { |
||
| 224 | $toTableColumn = [$toTableColumn]; |
||
| 225 | } |
||
| 226 | foreach ($this->getPrimaryKey() as $k => $pkField) { |
||
| 227 | if (isset($toTableColumn[$pkField])) { |
||
| 228 | $key = $toTableColumn[$pkField]; |
||
| 229 | } elseif (isset($toTableColumn[$k])) { |
||
| 230 | $key = $toTableColumn[$k]; |
||
| 231 | } else { |
||
| 232 | $key = $this->getName().'_'.$pkField; |
||
| 233 | } |
||
| 234 | if (!in_array($key, $columns)) { |
||
| 235 | throw new DBException('Missing foreign key mapping'); |
||
| 236 | } |
||
| 237 | $keymap[$pkField] = $key; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (!isset($name)) { |
||
| 241 | $name = $toTable->getName().'_'.implode('_', array_keys($keymap)); |
||
| 242 | } |
||
| 243 | $this->addRelation(new TableRelation( |
||
| 244 | $name, |
||
| 245 | $toTable, |
||
| 246 | $keymap, |
||
| 247 | true, |
||
| 248 | null, |
||
| 249 | null, |
||
| 250 | $sql, |
||
| 251 | $par |
||
| 252 | )); |
||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | /** |
||
| 256 | * Create a relation where each record belongs to another row in another table |
||
| 257 | * @param Table $toTable the related table definition |
||
| 258 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 259 | * @param string|array|null $localColumn the local columns pointing to the PK of the related table |
||
| 260 | * @param string|null $sql additional where clauses to use, default to null |
||
| 261 | * @param array $par parameters for the above statement, defaults to [] |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function belongsTo( |
||
| 265 | Table $toTable, |
||
| 266 | string $name = null, |
||
| 267 | $localColumn = null, |
||
| 268 | $sql = null, |
||
| 269 | array $par = [] |
||
| 270 | ) : Table { |
||
| 271 | $columns = $this->getColumns(); |
||
| 272 | |||
| 273 | $keymap = []; |
||
| 274 | if (!isset($localColumn)) { |
||
| 275 | $localColumn = []; |
||
| 276 | } |
||
| 277 | if (!is_array($localColumn)) { |
||
| 278 | $localColumn = [$localColumn]; |
||
| 279 | } |
||
| 280 | foreach ($toTable->getPrimaryKey() as $k => $pkField) { |
||
| 281 | if (isset($localColumn[$pkField])) { |
||
| 282 | $key = $localColumn[$pkField]; |
||
| 283 | } elseif (isset($localColumn[$k])) { |
||
| 284 | $key = $localColumn[$k]; |
||
| 285 | } else { |
||
| 286 | $key = $toTable->getName().'_'.$pkField; |
||
| 287 | } |
||
| 288 | if (!in_array($key, $columns)) { |
||
| 289 | throw new DBException('Missing foreign key mapping'); |
||
| 290 | } |
||
| 291 | $keymap[$key] = $pkField; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (!isset($name)) { |
||
| 295 | $name = $toTable->getName().'_'.implode('_', array_keys($keymap)); |
||
| 296 | } |
||
| 297 | $this->addRelation(new TableRelation( |
||
| 298 | $name, |
||
| 299 | $toTable, |
||
| 300 | $keymap, |
||
| 301 | false, |
||
| 302 | null, |
||
| 303 | null, |
||
| 304 | $sql, |
||
| 305 | $par |
||
| 306 | )); |
||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | /** |
||
| 310 | * Create a relation where each record has many linked records in another table but using a liking table |
||
| 311 | * @param Table $toTable the related table definition |
||
| 312 | * @param Table $pivot the pivot table definition |
||
| 313 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 314 | * @param string|array|null $toTableColumn the local columns pointing to the pivot table |
||
| 315 | * @param string|array|null $localColumn the pivot columns pointing to the related table PK |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function manyToMany( |
||
| 319 | Table $toTable, |
||
| 320 | Table $pivot, |
||
| 321 | $name = null, |
||
| 322 | $toTableColumn = null, |
||
| 323 | $localColumn = null |
||
| 324 | ) : Table { |
||
| 325 | $pivotColumns = $pivot->getColumns(); |
||
| 326 | |||
| 327 | $keymap = []; |
||
| 328 | if (!isset($toTableColumn)) { |
||
| 329 | $toTableColumn = []; |
||
| 330 | } |
||
| 331 | if (!is_array($toTableColumn)) { |
||
| 332 | $toTableColumn = [$toTableColumn]; |
||
| 333 | } |
||
| 334 | foreach ($this->getPrimaryKey() as $k => $pkField) { |
||
| 335 | if (isset($toTableColumn[$pkField])) { |
||
| 336 | $key = $toTableColumn[$pkField]; |
||
| 337 | } elseif (isset($toTableColumn[$k])) { |
||
| 338 | $key = $toTableColumn[$k]; |
||
| 339 | } else { |
||
| 340 | $key = $this->getName().'_'.$pkField; |
||
| 341 | } |
||
| 342 | if (!in_array($key, $pivotColumns)) { |
||
| 343 | throw new DBException('Missing foreign key mapping'); |
||
| 344 | } |
||
| 345 | $keymap[$pkField] = $key; |
||
| 346 | } |
||
| 347 | |||
| 348 | $pivotKeymap = []; |
||
| 349 | if (!isset($localColumn)) { |
||
| 350 | $localColumn = []; |
||
| 351 | } |
||
| 352 | if (!is_array($localColumn)) { |
||
| 353 | $localColumn = [$localColumn]; |
||
| 354 | } |
||
| 355 | foreach ($toTable->getPrimaryKey() as $k => $pkField) { |
||
| 356 | if (isset($localColumn[$pkField])) { |
||
| 357 | $key = $localColumn[$pkField]; |
||
| 358 | } elseif (isset($localColumn[$k])) { |
||
| 359 | $key = $localColumn[$k]; |
||
| 360 | } else { |
||
| 361 | $key = $toTable->getName().'_'.$pkField; |
||
| 362 | } |
||
| 363 | if (!in_array($key, $pivotColumns)) { |
||
| 364 | throw new DBException('Missing foreign key mapping'); |
||
| 365 | } |
||
| 366 | $pivotKeymap[$key] = $pkField; |
||
| 367 | } |
||
| 368 | |||
| 369 | if (!isset($name)) { |
||
| 370 | $name = $toTable->getName().'_'.implode('_', array_keys($keymap)); |
||
| 371 | } |
||
| 372 | $this->addRelation(new TableRelation( |
||
| 373 | $name, |
||
| 374 | $toTable, |
||
| 375 | $keymap, |
||
| 376 | true, |
||
| 377 | $pivot, |
||
| 378 | $pivotKeymap |
||
| 379 | )); |
||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | /** |
||
| 383 | * Create an advanced relation using the internal array format |
||
| 384 | * @param TableRelation $relation the relation definition |
||
| 385 | * @param string|null $name optional name of the relation (defaults to the related table name) |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | 4 | public function addRelation(TableRelation $relation, string $name = null) |
|
| 389 | { |
||
| 390 | 4 | $name = $name ?? $relation->name; |
|
| 391 | 4 | $relation->name = $name; |
|
| 392 | 4 | $this->relations[$name] = $relation; |
|
| 393 | 4 | return $this; |
|
| 394 | } |
||
| 395 | /** |
||
| 396 | * Does the definition have related tables |
||
| 397 | * @return boolean |
||
| 398 | */ |
||
| 399 | public function hasRelations() : bool |
||
| 400 | { |
||
| 401 | return count($this->relations) > 0; |
||
| 402 | } |
||
| 403 | /** |
||
| 404 | * Get all relation definitions |
||
| 405 | * @return TableRelation[] the relation definitions |
||
| 406 | */ |
||
| 407 | 152 | public function getRelations() : array |
|
| 411 | /** |
||
| 412 | * Check if a named relation exists |
||
| 413 | * @param string $name the name to search for |
||
| 414 | * @return boolean does the relation exist |
||
| 415 | */ |
||
| 416 | 56 | public function hasRelation(string $name) : bool |
|
| 417 | { |
||
| 418 | 56 | return isset($this->relations[$name]); |
|
| 419 | } |
||
| 420 | /** |
||
| 421 | * Get a relation by name |
||
| 422 | * @param string $name the name to search for |
||
| 423 | * @return TableRelation the relation definition |
||
| 424 | */ |
||
| 425 | 48 | public function getRelation(string $name) |
|
| 426 | { |
||
| 427 | 48 | return $this->relations[$name]; |
|
| 428 | } |
||
| 429 | /** |
||
| 430 | * Rename a relation |
||
| 431 | * @param string $name the name to search for |
||
| 432 | * @param string $new the new name for the relation |
||
| 433 | * @return mixed the relation definition |
||
| 434 | */ |
||
| 435 | public function renameRelation(string $name, string $new) |
||
| 479 | } |
||
| 480 |