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 |
||
40 | class Table |
||
41 | { |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $name; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $options = []; |
||
51 | |||
52 | /** |
||
53 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
54 | */ |
||
55 | protected $adapter; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $columns = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $indexes = []; |
||
66 | |||
67 | /** |
||
68 | * @var \Phinx\Db\Table\ForeignKey[] |
||
69 | */ |
||
70 | protected $foreignKeys = []; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $data = []; |
||
76 | |||
77 | /** |
||
78 | * Class Constuctor. |
||
79 | * |
||
80 | * @param string $name Table Name |
||
81 | * @param array $options Options |
||
82 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
||
83 | */ |
||
84 | 239 | public function __construct($name, $options = [], AdapterInterface $adapter = null) |
|
93 | |||
94 | /** |
||
95 | * Sets the table name. |
||
96 | * |
||
97 | * @param string $name Table Name |
||
98 | * @return \Phinx\Db\Table |
||
99 | */ |
||
100 | 239 | public function setName($name) |
|
101 | { |
||
102 | 239 | $this->name = $name; |
|
103 | 239 | ||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Gets the table name. |
||
109 | * |
||
110 | * @return string |
||
111 | 215 | */ |
|
112 | public function getName() |
||
116 | |||
117 | /** |
||
118 | * Sets the table options. |
||
119 | * |
||
120 | * @param array $options |
||
121 | * @return \Phinx\Db\Table |
||
122 | 239 | */ |
|
123 | public function setOptions($options) |
||
129 | |||
130 | /** |
||
131 | * Gets the table options. |
||
132 | * |
||
133 | 189 | * @return array |
|
134 | */ |
||
135 | 189 | public function getOptions() |
|
139 | |||
140 | /** |
||
141 | * Sets the database adapter. |
||
142 | * |
||
143 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
||
144 | 231 | * @return \Phinx\Db\Table |
|
145 | */ |
||
146 | 231 | public function setAdapter(AdapterInterface $adapter) |
|
152 | |||
153 | /** |
||
154 | * Gets the database adapter. |
||
155 | 225 | * |
|
156 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
157 | 225 | */ |
|
158 | public function getAdapter() |
||
162 | |||
163 | /** |
||
164 | * Does the table exist? |
||
165 | 195 | * |
|
166 | * @return bool |
||
167 | 195 | */ |
|
168 | public function exists() |
||
172 | |||
173 | /** |
||
174 | * Drops the database table. |
||
175 | 1 | * |
|
176 | * @return void |
||
177 | 1 | */ |
|
178 | 1 | public function drop() |
|
182 | |||
183 | /** |
||
184 | * Renames the database table. |
||
185 | * |
||
186 | 3 | * @param string $newTableName New Table Name |
|
187 | * @return \Phinx\Db\Table |
||
188 | 3 | */ |
|
189 | 3 | public function rename($newTableName) |
|
196 | |||
197 | /** |
||
198 | * Sets an array of columns waiting to be committed. |
||
199 | * Use setPendingColumns |
||
200 | * |
||
201 | * @deprecated |
||
202 | * @param array $columns Columns |
||
203 | * @return \Phinx\Db\Table |
||
204 | */ |
||
205 | public function setColumns($columns) |
||
211 | 10 | ||
212 | /** |
||
213 | 10 | * Gets an array of the table columns. |
|
214 | * |
||
215 | * @return \Phinx\Db\Table\Column[] |
||
216 | */ |
||
217 | public function getColumns() |
||
221 | |||
222 | 196 | /** |
|
223 | * Sets an array of columns waiting to be committed. |
||
224 | 196 | * |
|
225 | 196 | * @param array $columns Columns |
|
226 | * @return \Phinx\Db\Table |
||
227 | */ |
||
228 | public function setPendingColumns($columns) |
||
234 | |||
235 | 204 | /** |
|
236 | * Gets an array of columns waiting to be committed. |
||
237 | * |
||
238 | * @return \Phinx\Db\Table\Column[] |
||
239 | */ |
||
240 | public function getPendingColumns() |
||
244 | 196 | ||
245 | /** |
||
246 | 196 | * Sets an array of columns waiting to be indexed. |
|
247 | 196 | * |
|
248 | * @param array $indexes Indexes |
||
249 | * @return \Phinx\Db\Table |
||
250 | */ |
||
251 | public function setIndexes($indexes) |
||
257 | 191 | ||
258 | /** |
||
259 | * Gets an array of indexes waiting to be committed. |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function getIndexes() |
||
267 | |||
268 | 196 | /** |
|
269 | 196 | * Sets an array of foreign keys waiting to be commited. |
|
270 | * |
||
271 | * @param \Phinx\Db\Table\ForeignKey[] $foreignKeys foreign keys |
||
272 | * @return \Phinx\Db\Table |
||
273 | */ |
||
274 | public function setForeignKeys($foreignKeys) |
||
280 | |||
281 | /** |
||
282 | * Gets an array of foreign keys waiting to be commited. |
||
283 | * |
||
284 | * @return array|\Phinx\Db\Table\ForeignKey[] |
||
285 | */ |
||
286 | public function getForeignKeys() |
||
290 | 196 | ||
291 | 196 | /** |
|
292 | * Sets an array of data to be inserted. |
||
293 | * |
||
294 | * @param array $data Data |
||
295 | * @return \Phinx\Db\Table |
||
296 | */ |
||
297 | public function setData($data) |
||
303 | |||
304 | /** |
||
305 | * Gets the data waiting to be inserted. |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 196 | public function getData() |
|
313 | 196 | ||
314 | 196 | /** |
|
315 | 196 | * Resets all of the pending table changes. |
|
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function reset() |
||
326 | |||
327 | /** |
||
328 | * Add a table column. |
||
329 | * |
||
330 | * Type can be: string, text, integer, float, decimal, datetime, timestamp, |
||
331 | * time, date, binary, boolean. |
||
332 | 210 | * |
|
333 | * Valid options can be: limit, default, null, precision or scale. |
||
334 | * |
||
335 | 210 | * @param string|\Phinx\Db\Table\Column $columnName Column Name |
|
336 | 1 | * @param string $type Column Type |
|
337 | * @param array $options Column Options |
||
338 | * @throws \RuntimeException |
||
339 | * @throws \InvalidArgumentException |
||
340 | 209 | * @return \Phinx\Db\Table |
|
341 | 207 | */ |
|
342 | 207 | public function addColumn($columnName, $type = null, $options = []) |
|
372 | |||
373 | /** |
||
374 | * Remove a table column. |
||
375 | * |
||
376 | * @param string $columnName Column Name |
||
377 | * @return \Phinx\Db\Table |
||
378 | */ |
||
379 | public function removeColumn($columnName) |
||
385 | |||
386 | /** |
||
387 | * Rename a table column. |
||
388 | * |
||
389 | * @param string $oldName Old Column Name |
||
390 | * @param string $newName New Column Name |
||
391 | * @return \Phinx\Db\Table |
||
392 | */ |
||
393 | public function renameColumn($oldName, $newName) |
||
399 | 4 | ||
400 | 4 | /** |
|
401 | 4 | * Change a table column type. |
|
402 | 4 | * |
|
403 | 13 | * @param string $columnName Column Name |
|
404 | * @param string|\Phinx\Db\Table\Column $newColumnType New Column Type |
||
405 | * @param array $options Options |
||
406 | * @return \Phinx\Db\Table |
||
407 | 17 | */ |
|
408 | 15 | public function changeColumn($columnName, $newColumnType, $options = []) |
|
428 | |||
429 | /** |
||
430 | * Checks to see if a column exists. |
||
431 | * |
||
432 | * @param string $columnName Column Name |
||
433 | * @return bool |
||
434 | */ |
||
435 | 29 | public function hasColumn($columnName) |
|
439 | 28 | ||
440 | 28 | /** |
|
441 | 22 | * Add an index to a database table. |
|
442 | 22 | * |
|
443 | 28 | * In $options you can specific unique = true/false or name (index name). |
|
444 | 28 | * |
|
445 | 28 | * @param string|array|\Phinx\Db\Table\Index $columns Table Column(s) |
|
446 | 1 | * @param array $options Index Options |
|
447 | * @return \Phinx\Db\Table |
||
448 | */ |
||
449 | 29 | public function addIndex($columns, $options = []) |
|
467 | |||
468 | /** |
||
469 | * Removes the given index from a table. |
||
470 | * |
||
471 | 1 | * @param array $columns Columns |
|
472 | * @return \Phinx\Db\Table |
||
473 | 1 | */ |
|
474 | 1 | public function removeIndex($columns) |
|
480 | |||
481 | /** |
||
482 | * Removes the given index identified by its name from a table. |
||
483 | * |
||
484 | 12 | * @param string $name Index name |
|
485 | * @return \Phinx\Db\Table |
||
486 | 12 | */ |
|
487 | public function removeIndexByName($name) |
||
493 | |||
494 | /** |
||
495 | * Checks to see if an index exists. |
||
496 | * |
||
497 | * @param string|array $columns Columns |
||
498 | * @return bool |
||
499 | */ |
||
500 | public function hasIndex($columns) |
||
504 | 4 | ||
505 | 4 | /** |
|
506 | 8 | * Checks to see if an index specified by name exists. |
|
507 | 8 | * |
|
508 | * @param string $indexName |
||
509 | * @return bool |
||
510 | 8 | */ |
|
511 | public function hasIndexByName($indexName) |
||
512 | 8 | { |
|
513 | 8 | return $this->getAdapter()->hasIndexByName($this->getName(), $indexName); |
|
514 | 8 | } |
|
515 | 8 | ||
516 | /** |
||
517 | 8 | * Add a foreign key to a database table. |
|
518 | * |
||
519 | * In $options you can specify on_delete|on_delete = cascade|no_action .., |
||
520 | * on_update, constraint = constraint name. |
||
521 | * |
||
522 | * @param string|array $columns Columns |
||
523 | * @param string|\Phinx\Db\Table $referencedTable Referenced Table |
||
524 | * @param string|array $referencedColumns Referenced Columns |
||
525 | * @param array $options Options |
||
526 | * @return \Phinx\Db\Table |
||
527 | 1 | */ |
|
528 | public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], $options = []) |
||
546 | |||
547 | /** |
||
548 | 1 | * Removes the given foreign key from the table. |
|
549 | * |
||
550 | 1 | * @param string|array $columns Column(s) |
|
551 | * @param null|string $constraint Constraint names |
||
552 | * @return \Phinx\Db\Table |
||
553 | */ |
||
554 | public function dropForeignKey($columns, $constraint = null) |
||
567 | |||
568 | 15 | /** |
|
569 | 15 | * Checks to see if a foreign key exists. |
|
570 | 15 | * |
|
571 | * @param string|array $columns Column(s) |
||
572 | 15 | * @param null|string $constraint Constraint names |
|
573 | * @return bool |
||
574 | 15 | */ |
|
575 | public function hasForeignKey($columns, $constraint = null) |
||
579 | |||
580 | /** |
||
581 | * Add timestamp columns created_at and updated_at to the table. |
||
582 | * |
||
583 | * @param string $createdAtColumnName |
||
584 | * @param string $updatedAtColumnName |
||
585 | * |
||
586 | * @return \Phinx\Db\Table |
||
587 | */ |
||
588 | public function addTimestamps($createdAtColumnName = 'created_at', $updatedAtColumnName = 'updated_at') |
||
603 | |||
604 | /** |
||
605 | * Insert data into the table. |
||
606 | * |
||
607 | 196 | * @param array $data array of data in the form: |
|
608 | * array( |
||
609 | 196 | * array("col1" => "value1", "col2" => "anotherValue1"), |
|
610 | 196 | * array("col2" => "value2", "col2" => "anotherValue2"), |
|
611 | 196 | * ) |
|
612 | 196 | * or array("col1" => "value1", "col2" => "anotherValue1") |
|
613 | * |
||
614 | * @return \Phinx\Db\Table |
||
615 | */ |
||
616 | public function insert($data) |
||
630 | |||
631 | 46 | /** |
|
632 | 6 | * Creates a table from the object instance. |
|
633 | 46 | * |
|
634 | * @return void |
||
635 | 46 | */ |
|
636 | 3 | public function create() |
|
642 | |||
643 | /** |
||
644 | * Updates a table from the object instance. |
||
645 | * |
||
646 | * @throws \RuntimeException |
||
647 | * @return void |
||
648 | 196 | */ |
|
649 | public function update() |
||
671 | 1 | ||
672 | /** |
||
673 | 12 | * Commit the pending data waiting for insertion. |
|
674 | * |
||
675 | * @return void |
||
676 | */ |
||
677 | public function saveData() |
||
703 | |||
704 | /** |
||
705 | * Truncates the table. |
||
706 | * |
||
707 | * @return void |
||
708 | */ |
||
709 | public function truncate() |
||
710 | { |
||
711 | $this->getAdapter()->truncateTable($this->getName()); |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Commits the table changes. |
||
716 | * |
||
717 | * If the table doesn't exist it is created otherwise it is updated. |
||
718 | * |
||
719 | * @return void |
||
720 | */ |
||
721 | public function save() |
||
731 | } |
||
732 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: