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 = '') |
|
32 | /** |
||
33 | * Get the table comment |
||
34 | * @return string the table comment |
||
35 | */ |
||
36 | public function getComment() |
||
40 | /** |
||
41 | * Set the table comment |
||
42 | * @param string $comment the table comment |
||
43 | * @return $this |
||
44 | 4 | */ |
|
45 | public function setComment(string $comment) |
||
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 | 4 | */ |
|
56 | public function addColumn(string $column, array $definition = []) : Table |
||
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 | 4 | */ |
|
66 | public function addColumns(array $columns) : Table |
||
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 | 4 | */ |
|
82 | public function setPrimaryKey($column) : Table |
||
90 | /** |
||
91 | * Get the table schema |
||
92 | * @return string the table name |
||
93 | 156 | */ |
|
94 | public function getSchema() |
||
98 | /** |
||
99 | * Get the table name |
||
100 | * @return string the table name |
||
101 | */ |
||
102 | 156 | 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 | 28 | */ |
|
119 | public function getColumn($column) |
||
123 | /** |
||
124 | * Get all column names |
||
125 | * @return array array of strings, where each element is a column name |
||
126 | 156 | */ |
|
127 | public function getColumns() |
||
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 | public function getFullColumns() |
||
139 | /** |
||
140 | * Get the primary key columns |
||
141 | * @return array array of column names |
||
142 | */ |
||
143 | public function getPrimaryKey() |
||
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( |
||
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( |
||
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( |
||
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( |
||
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 | public function addRelation(TableRelation $relation, string $name = null) |
||
395 | /** |
||
396 | * Does the definition have related tables |
||
397 | * @return boolean |
||
398 | */ |
||
399 | 56 | public function hasRelations() : bool |
|
403 | /** |
||
404 | * Get all relation definitions |
||
405 | * @return TableRelation[] the relation definitions |
||
406 | */ |
||
407 | 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 | public function hasRelation(string $name) : bool |
||
420 | /** |
||
421 | * Get a relation by name |
||
422 | * @param string $name the name to search for |
||
423 | * @return TableRelation the relation definition |
||
424 | */ |
||
425 | public function getRelation(string $name) |
||
429 | /** |
||
430 | * Rename a relation |
||
431 | * @param string $name the name to search for |
||
432 | 4 | * @param string $new the new name for the relation |
|
433 | * @return mixed the relation definition |
||
434 | 4 | */ |
|
435 | 4 | public function renameRelation(string $name, string $new) |
|
479 | } |
||
480 |