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 |
||
| 15 | class Table |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Table name |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $table; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Prefixed table name or table name if no prefix |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $prefixed_table; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var Table\Relation |
||
| 32 | */ |
||
| 33 | protected $relation; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Primary key of the table |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | protected $primary_key; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Primary keys of the table in case there's a multiple column pk |
||
| 43 | * @var array|null |
||
| 44 | */ |
||
| 45 | protected $primary_keys; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Table alias useful when using join |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $table_alias; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param TableManager |
||
| 55 | */ |
||
| 56 | protected $tableManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $tablePrefix; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @var Sql |
||
| 67 | */ |
||
| 68 | protected $sql; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $column_information; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param string $table table name |
||
| 79 | * @param TableManager $tableManager |
||
| 80 | * |
||
| 81 | * @throws Exception\InvalidArgumentException |
||
| 82 | */ |
||
| 83 | 131 | public function __construct($table, TableManager $tableManager) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Get a TableSearch object |
||
| 97 | * |
||
| 98 | * @param string $table_alias whenever you want to alias the table (useful in joins) |
||
| 99 | * @return TableSearch |
||
| 100 | */ |
||
| 101 | 26 | public function search($table_alias = null) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Return all records in the table |
||
| 108 | * |
||
| 109 | * @return ResultSet |
||
| 110 | */ |
||
| 111 | 1 | public function all() |
|
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Find a record |
||
| 120 | * |
||
| 121 | * @param integer|string|array $id |
||
| 122 | * |
||
| 123 | * @throws Exception\InvalidArgumentException when id is invalid |
||
| 124 | * @throws Exception\PrimaryKeyNotFoundException |
||
| 125 | * |
||
| 126 | * @return Record|false |
||
| 127 | */ |
||
| 128 | 32 | public function find($id) |
|
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Find a record by primary key, throw a NotFoundException if record does not exists |
||
| 137 | * |
||
| 138 | * @param integer|string|array $id |
||
| 139 | * |
||
| 140 | * @throws Exception\NotFoundException |
||
| 141 | * @throws Exception\InvalidArgumentException when the id is not valid |
||
| 142 | * @throws Exception\PrimaryKeyNotFoundException |
||
| 143 | * |
||
| 144 | * @return Record |
||
| 145 | */ |
||
| 146 | 14 | public function findOrFail($id) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Find a record by unique key |
||
| 157 | * |
||
| 158 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 159 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 160 | * |
||
| 161 | * @throws Exception\ColumnNotFoundException when a column in the predicate does not exists |
||
| 162 | * @throws Exception\MultipleMatchesException when more than one record match the predicate |
||
| 163 | * @throws Exception\InvalidArgumentException when the predicate is not correct / invalid column |
||
| 164 | * @throws \Zend\Db\Sql\Exception\InvalidArgumentException |
||
| 165 | * |
||
| 166 | * @return Record|false |
||
| 167 | */ |
||
| 168 | 51 | public function findOneBy($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Find a record by unique key and trhow an exception id record cannot be found |
||
| 218 | * |
||
| 219 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 220 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 221 | * |
||
| 222 | * @throws Exception\NotFoundException when the record is not found |
||
| 223 | * @throws Exception\ColumnNotFoundException when a column in the predicate does not exists |
||
| 224 | * @throws Exception\MultipleMatchesException when more than one record match the predicate |
||
| 225 | * @throws Exception\InvalidArgumentException when the predicate is not correct / invalid column |
||
| 226 | * |
||
| 227 | * @return Record |
||
| 228 | */ |
||
| 229 | 5 | public function findOneByOrFail($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Count the number of record in table |
||
| 240 | * |
||
| 241 | * @return int number of record in table |
||
| 242 | */ |
||
| 243 | 4 | public function count() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Find a record by unique key |
||
| 250 | * |
||
| 251 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 252 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 253 | * @throws Exception\InvalidArgumentException |
||
| 254 | * |
||
| 255 | * @return int number of record matching predicates |
||
| 256 | */ |
||
| 257 | 52 | public function countBy($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Test if a record exists |
||
| 276 | * |
||
| 277 | * @param integer|string|array $id |
||
| 278 | * |
||
| 279 | * @throws Exception\InvalidArgumentException when the id is invalid |
||
| 280 | * @throws Exception\PrimaryKeyNotFoundException |
||
| 281 | * |
||
| 282 | * @return boolean |
||
| 283 | */ |
||
| 284 | 7 | public function exists($id) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Test if a record exists by a predicate |
||
| 296 | * |
||
| 297 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 298 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 299 | * |
||
| 300 | * @throws Exception\InvalidArgumentException when the predicate is not correct |
||
| 301 | * |
||
| 302 | * @return boolean |
||
| 303 | */ |
||
| 304 | 2 | public function existsBy($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get a select object (Soluble\Db\Select) |
||
| 320 | * |
||
| 321 | * @param string $table_alias useful when you want to join columns |
||
| 322 | * @return Select |
||
| 323 | */ |
||
| 324 | 88 | public function select($table_alias = null) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Delete by primary/unique key value |
||
| 340 | * |
||
| 341 | * @param integer|string|array $id primary key(s) or a Record object |
||
| 342 | * |
||
| 343 | * @throws Exception\InvalidArgumentException if $id is not valid |
||
| 344 | * @return integer the number of affected rows (maybe be greater than 1 with triggers or cascade) |
||
| 345 | */ |
||
| 346 | 9 | public function delete($id) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Delete a record by predicate |
||
| 353 | * |
||
| 354 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 355 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 356 | * |
||
| 357 | * @return integer the number of affected rows (can be be influenced by triggers or cascade) |
||
| 358 | */ |
||
| 359 | 17 | public function deleteBy($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Delete a record or throw an Exception |
||
| 371 | * |
||
| 372 | * @param integer|string|array $id primary key value |
||
| 373 | * |
||
| 374 | * @throws Exception\InvalidArgumentException if $id is not valid |
||
| 375 | * @throws Exception\NotFoundException if record does not exists |
||
| 376 | * |
||
| 377 | * @return Table |
||
| 378 | */ |
||
| 379 | 2 | public function deleteOrFail($id) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Update data into table |
||
| 390 | * |
||
| 391 | * @param array|ArrayObject $data |
||
| 392 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
| 393 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
| 394 | * @param boolean $validate_datatypes ensure all datatype are compatible with column definition |
||
| 395 | * |
||
| 396 | * @throws Exception\InvalidArgumentException |
||
| 397 | * @throws Exception\ColumnNotFoundException when $data contains columns that does not exists in table |
||
| 398 | * @throws Exception\ForeignKeyException when insertion failed because of an invalid foreign key |
||
| 399 | * @throws Exception\DuplicateEntryException when insertion failed because of an invalid foreign key |
||
| 400 | * @throws Exception\NotNullException when insertion failed because a column cannot be null |
||
| 401 | * @throws Exception\RuntimeException when insertion failed for another reason |
||
| 402 | * |
||
| 403 | * @return int number of affected rows |
||
| 404 | */ |
||
| 405 | |||
| 406 | 5 | public function update($data, $predicate, $combination = Predicate\PredicateSet::OP_AND, $validate_datatypes = false) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Insert data into table |
||
| 435 | * |
||
| 436 | * @param array|ArrayObject $data |
||
| 437 | * @param boolean $validate_datatypes ensure data are compatible with database columns datatypes |
||
| 438 | * |
||
| 439 | * @throws Exception\InvalidArgumentException when data is not an array or an ArrayObject |
||
| 440 | * @throws Exception\ColumnNotFoundException when $data contains columns that does not exists in table |
||
| 441 | * @throws Exception\ForeignKeyException when insertion failed because of an invalid foreign key |
||
| 442 | * @throws Exception\DuplicateEntryException when insertion failed because of an invalid foreign key |
||
| 443 | * @throws Exception\NotNullException when insertion failed because a column cannot be null |
||
| 444 | * @throws Exception\RuntimeException when insertion failed for another reason |
||
| 445 | * |
||
| 446 | * @return Record |
||
| 447 | */ |
||
| 448 | 19 | public function insert($data, $validate_datatypes = false) |
|
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Insert on duplicate key |
||
| 503 | * |
||
| 504 | * @param array|ArrayObject $data |
||
| 505 | * @param array|null $duplicate_exclude |
||
| 506 | * @param boolean $validate_datatypes ensure data are compatible with database columns datatypes |
||
| 507 | * |
||
| 508 | * @throws Exception\ColumnNotFoundException |
||
| 509 | * @throws Exception\RecordNotFoundException |
||
| 510 | * @throws Exception\ForeignKeyException when insertion failed because of an invalid foreign key |
||
| 511 | * @throws Exception\DuplicateEntryException when insertion failed because of an invalid foreign key |
||
| 512 | * @throws Exception\NotNullException when insertion failed because a column cannot be null |
||
| 513 | * |
||
| 514 | * @return Record|false |
||
| 515 | */ |
||
| 516 | 27 | public function insertOnDuplicateKey($data, array $duplicate_exclude = [], $validate_datatypes = false) |
|
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * Return table relations (foreign keys infos) |
||
| 665 | * |
||
| 666 | * @return array |
||
| 667 | */ |
||
| 668 | 1 | public function getRelations() |
|
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * Return a record object for this table |
||
| 676 | * If $data is specified, the record will be filled with the |
||
| 677 | * data present in the associative array |
||
| 678 | * |
||
| 679 | * |
||
| 680 | * If $throwException is true, if any non existing column is found |
||
| 681 | * an error will be thrown |
||
| 682 | * |
||
| 683 | * @throws Exception\ColumnNotFoundException if $ignore_invalid_columns is false and some columns does not exists in table |
||
| 684 | * |
||
| 685 | * @param array|ArrayObject $data associative array containing initial data |
||
| 686 | * @param boolean $ignore_invalid_columns if true will throw an exception if a column does not exists |
||
| 687 | * @return Record |
||
| 688 | */ |
||
| 689 | 50 | public function record($data = [], $ignore_invalid_columns = true) |
|
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * Return table relation reader |
||
| 703 | * |
||
| 704 | * @return Table\Relation |
||
| 705 | */ |
||
| 706 | 5 | public function relation() |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Return table primary keys |
||
| 716 | * |
||
| 717 | * @throws Exception\PrimaryKeyNotFoundException when no pk |
||
| 718 | * @throws Exception\RuntimeException when it cannot determine primary key on table |
||
| 719 | * |
||
| 720 | * |
||
| 721 | * @return array |
||
| 722 | */ |
||
| 723 | 54 | public function getPrimaryKeys() |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Return primary key, if multiple primary keys found will |
||
| 741 | * throw an exception |
||
| 742 | * |
||
| 743 | * @throws Exception\PrimaryKeyNotFoundException when no pk found |
||
| 744 | * @throws Exception\MultiplePrimaryKeysFoundException when multiple primary keys found |
||
| 745 | * @throws Exception\RuntimeException when it cannot determine primary key on table |
||
| 746 | * |
||
| 747 | * @return int|string |
||
| 748 | */ |
||
| 749 | 29 | public function getPrimaryKey() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Return list of table columns |
||
| 763 | * |
||
| 764 | * @throws Soluble\Db\Metadata\Exception\InvalidArgumentException |
||
| 765 | * @throws Soluble\Db\Metadata\Exception\ErrorException |
||
| 766 | * @throws Soluble\Db\Metadata\Exception\ExceptionInterface |
||
| 767 | * @throws Soluble\Db\Metadata\Exception\TableNotFoundException |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | 50 | public function getColumnsInformation() |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Return the original table name |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | 3 | public function getTableName() |
|
| 790 | |||
| 791 | /** |
||
| 792 | * Return the prefixed table |
||
| 793 | * |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | 1 | public function getPrefixedTableName() |
|
| 800 | |||
| 801 | |||
| 802 | /** |
||
| 803 | * Return underlying table manager |
||
| 804 | * |
||
| 805 | * @return TableManager |
||
| 806 | */ |
||
| 807 | 21 | public function getTableManager() |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Execute a statement |
||
| 814 | * |
||
| 815 | * @todo move to driver if not will only support MySQL |
||
| 816 | * |
||
| 817 | * @throws Exception\ForeignKeyException when insertion failed because of an invalid foreign key |
||
| 818 | * @throws Exception\DuplicateEntryException when insertion failed because of an invalid foreign key |
||
| 819 | * @throws Exception\NotNullException when insertion failed because a column cannot be null |
||
| 820 | * @throws Exception\RuntimeException when insertion failed for another reason |
||
| 821 | * |
||
| 822 | * @param string|PreparableSqlInterface $sqlObject |
||
| 823 | * @return \Zend\Db\Adapter\Driver\ResultInterface |
||
| 824 | */ |
||
| 825 | 39 | protected function executeStatement($sqlObject) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Return primary key predicate |
||
| 878 | * |
||
| 879 | * @param integer|string|array $id |
||
| 880 | * |
||
| 881 | * @throws Exception\InvalidArgumentException |
||
| 882 | * @throws Exception\PrimaryKeyNotFoundException |
||
| 883 | * @return array predicate |
||
| 884 | */ |
||
| 885 | 37 | protected function getPrimaryKeyPredicate($id) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Check if all columns exists in table |
||
| 919 | * |
||
| 920 | * @param array $data |
||
| 921 | * @throws Exception\ColumnNotFoundException |
||
| 922 | */ |
||
| 923 | 42 | protected function checkDataColumns(array $data) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Validate data with database column datatype |
||
| 934 | * |
||
| 935 | * @param array $data |
||
| 936 | * @return void |
||
| 937 | */ |
||
| 938 | 3 | protected function validateDatatypes(array $data) |
|
| 948 | } |
||
| 949 |