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 | 1 | public function __construct(string $name) |
|
| 31 | /** |
||
| 32 | * Get the table comment |
||
| 33 | * @return string the table comment |
||
| 34 | */ |
||
| 35 | public function getComment() |
||
| 39 | /** |
||
| 40 | * Set the table comment |
||
| 41 | * @param string $comment the table comment |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | 1 | public function setComment(string $comment) |
|
| 49 | /** |
||
| 50 | * Add a column to the definition |
||
| 51 | * @param string $column the column name |
||
| 52 | * @param array $definition optional array of data associated with the column |
||
| 53 | * @return self |
||
| 54 | */ |
||
| 55 | 1 | public function addColumn(string $column, array $definition = []) : Table |
|
| 60 | /** |
||
| 61 | * Add columns to the definition |
||
| 62 | * @param array $columns key - value pairs, where each key is a column name and each value - array of info |
||
| 63 | * @return self |
||
| 64 | */ |
||
| 65 | 1 | public function addColumns(array $columns) : Table |
|
| 76 | /** |
||
| 77 | * Set the primary key |
||
| 78 | * @param array|string $column either a single column name or an array of column names |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | 1 | public function setPrimaryKey($column) : Table |
|
| 89 | /** |
||
| 90 | * Get the table name |
||
| 91 | * @return string the table name |
||
| 92 | */ |
||
| 93 | 12 | public function getName() |
|
| 97 | /** |
||
| 98 | * Get a column definition |
||
| 99 | * @param string $column the column name to search for |
||
| 100 | * @return array|null the column details or `null` if the column does not exist |
||
| 101 | */ |
||
| 102 | 12 | public function getColumn($column) |
|
| 106 | /** |
||
| 107 | * Get all column names |
||
| 108 | * @return array array of strings, where each element is a column name |
||
| 109 | */ |
||
| 110 | 12 | public function getColumns() |
|
| 114 | /** |
||
| 115 | * Get all column definitions |
||
| 116 | * @return array key - value pairs, where each key is a column name and each value - the column data |
||
| 117 | */ |
||
| 118 | 2 | public function getFullColumns() |
|
| 122 | /** |
||
| 123 | * Get the primary key columns |
||
| 124 | * @return array array of column names |
||
| 125 | */ |
||
| 126 | 12 | public function getPrimaryKey() |
|
| 130 | /** |
||
| 131 | * Create a relation where each record has zero or one related rows in another table |
||
| 132 | * @param Table $toTable the related table definition |
||
| 133 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 134 | * @param string|array|null $toTableColumn the remote columns pointing to the PK in the current table |
||
| 135 | * @param string|null $sql additional where clauses to use, default to null |
||
| 136 | * @param array|null $par parameters for the above statement, defaults to null |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function hasOne( |
||
| 185 | /** |
||
| 186 | * Create a relation where each record has zero, one or more related rows in another table |
||
| 187 | * @param Table $toTable the related table definition |
||
| 188 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 189 | * @param string|array|null $toTableColumn the remote columns pointing to the PK in the current table |
||
| 190 | * @param string|null $sql additional where clauses to use, default to null |
||
| 191 | * @param array|null $par parameters for the above statement, defaults to null |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function hasMany( |
||
| 240 | /** |
||
| 241 | * Create a relation where each record belongs to another row in another table |
||
| 242 | * @param Table $toTable the related table definition |
||
| 243 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 244 | * @param string|array|null $localColumn the local columns pointing to the PK of the related table |
||
| 245 | * @param string|null $sql additional where clauses to use, default to null |
||
| 246 | * @param array|null $par parameters for the above statement, defaults to null |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function belongsTo( |
||
| 295 | /** |
||
| 296 | * Create a relation where each record has many linked records in another table but using a liking table |
||
| 297 | * @param Table $toTable the related table definition |
||
| 298 | * @param Table $pivot the pivot table definition |
||
| 299 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
| 300 | * @param string|array|null $toTableColumn the local columns pointing to the pivot table |
||
| 301 | * @param string|array|null $localColumn the pivot columns pointing to the related table PK |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function manyToMany( |
||
| 370 | /** |
||
| 371 | * Create an advanced relation using the internal array format |
||
| 372 | * @param TableRelation $relation the relation definition |
||
| 373 | * @param string|null $name optional name of the relation (defaults to the related table name) |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | 1 | public function addRelation(TableRelation $relation, string $name = null) |
|
| 383 | /** |
||
| 384 | * Does the definition have related tables |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | public function hasRelations() : bool |
||
| 391 | /** |
||
| 392 | * Get all relation definitions |
||
| 393 | * @return TableRelation[] the relation definitions |
||
| 394 | */ |
||
| 395 | 12 | public function getRelations() : array |
|
| 399 | /** |
||
| 400 | * Check if a named relation exists |
||
| 401 | * @param string $name the name to search for |
||
| 402 | * @return boolean does the relation exist |
||
| 403 | */ |
||
| 404 | 6 | public function hasRelation(string $name) : bool |
|
| 408 | /** |
||
| 409 | * Get a relation by name |
||
| 410 | * @param string $name the name to search for |
||
| 411 | * @return TableRelation|null the relation definition |
||
| 412 | */ |
||
| 413 | 4 | public function getRelation(string $name) |
|
| 417 | /** |
||
| 418 | * Rename a relation |
||
| 419 | * @param string $name the name to search for |
||
| 420 | * @param string $new the new name for the relation |
||
| 421 | * @return TableRelation the relation definition |
||
| 422 | */ |
||
| 423 | public function renameRelation(string $name, string $new) : array |
||
| 465 | } |
||
| 466 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.