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 | 13 | 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 | 13 | 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 | 13 | 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 | 13 | 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( |
||
184 | /** |
||
185 | * Create a relation where each record has zero, one or more related rows in another table |
||
186 | * @param Table $toTable the related table definition |
||
187 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
188 | * @param string|array|null $toTableColumn the remote columns pointing to the PK in the current table |
||
189 | * @param string|null $sql additional where clauses to use, default to null |
||
190 | * @param array|null $par parameters for the above statement, defaults to null |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function hasMany( |
||
238 | /** |
||
239 | * Create a relation where each record belongs to another row in another table |
||
240 | * @param Table $toTable the related table definition |
||
241 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
242 | * @param string|array|null $localColumn the local columns pointing to the PK of the related table |
||
243 | * @param string|null $sql additional where clauses to use, default to null |
||
244 | * @param array|null $par parameters for the above statement, defaults to null |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function belongsTo( |
||
292 | /** |
||
293 | * Create a relation where each record has many linked records in another table but using a liking table |
||
294 | * @param Table $toTable the related table definition |
||
295 | * @param Table $pivot the pivot table definition |
||
296 | * @param string|null $name the name of the relation (defaults to the related table name) |
||
297 | * @param string|array|null $toTableColumn the local columns pointing to the pivot table |
||
298 | * @param string|array|null $localColumn the pivot columns pointing to the related table PK |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function manyToMany( |
||
365 | /** |
||
366 | * Create an advanced relation using the internal array format |
||
367 | * @param TableRelation $relation the relation definition |
||
368 | * @param string|null $name optional name of the relation (defaults to the related table name) |
||
369 | * @return $this |
||
370 | */ |
||
371 | 1 | public function addRelation(TableRelation $relation, string $name = null) |
|
378 | /** |
||
379 | * Does the definition have related tables |
||
380 | * @return boolean |
||
381 | */ |
||
382 | public function hasRelations() : bool |
||
386 | /** |
||
387 | * Get all relation definitions |
||
388 | * @return TableRelation[] the relation definitions |
||
389 | */ |
||
390 | 13 | public function getRelations() : array |
|
394 | /** |
||
395 | * Check if a named relation exists |
||
396 | * @param string $name the name to search for |
||
397 | * @return boolean does the relation exist |
||
398 | */ |
||
399 | 3 | public function hasRelation(string $name) : bool |
|
403 | /** |
||
404 | * Get a relation by name |
||
405 | * @param string $name the name to search for |
||
406 | * @return TableRelation|null the relation definition |
||
407 | */ |
||
408 | 5 | public function getRelation(string $name) |
|
412 | /** |
||
413 | * Rename a relation |
||
414 | * @param string $name the name to search for |
||
415 | * @param string $new the new name for the relation |
||
416 | * @return TableRelation the relation definition |
||
417 | */ |
||
418 | public function renameRelation(string $name, string $new) : array |
||
460 | } |
||
461 |