Complex classes like Record 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 Record, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Record extends Model |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Database table name. |
||
| 22 | * |
||
| 23 | * Overrides the name of the database table that persists the model. The |
||
| 24 | * model's lower-cased class name is used if this is not set. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $table; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Instance storage. |
||
| 32 | * |
||
| 33 | * @var Readable |
||
| 34 | */ |
||
| 35 | protected $storage; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Shared storage. |
||
| 39 | * |
||
| 40 | * @var Readable |
||
| 41 | */ |
||
| 42 | protected static $sharedStorage; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Definitions of related models. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $relations = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Default searchable attributes. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $search = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Instantiate a new record with the given data or load an instance from |
||
| 60 | * storage if the given data is a valid primary key. |
||
| 61 | * |
||
| 62 | * @param mixed $data An array of key-value attributes to set or a primary key to load by |
||
| 63 | */ |
||
| 64 | public function __construct($data = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Determine whether the given attribute or relation is set on the record. |
||
| 75 | * |
||
| 76 | * @param string $attribute |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function has($attribute) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Retrieve the given attribute or relation from the record. |
||
| 86 | * |
||
| 87 | * @param string $attribute |
||
| 88 | * @return mixed |
||
| 89 | */ |
||
| 90 | public function get($attribute) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set the value of an attribute or relation on the model. |
||
| 108 | * |
||
| 109 | * @param string $attribute |
||
| 110 | * @param mixed $value |
||
| 111 | */ |
||
| 112 | public function set($attribute, $value = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Unset the value for an attribute or relation on the model. |
||
| 125 | * |
||
| 126 | * @param string $attribute |
||
| 127 | */ |
||
| 128 | public function remove($attribute) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Retrieve the name of the table this model belongs to. |
||
| 139 | * |
||
| 140 | * If none is set, it defaults to creating it from the class name. |
||
| 141 | * |
||
| 142 | * For example: |
||
| 143 | * Page -> pages |
||
| 144 | * PageSection -> page_sections |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function table() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get and optionally set the model's storage instance. |
||
| 164 | * |
||
| 165 | * @param Readable $storage [optional] |
||
| 166 | * @return Readable |
||
| 167 | */ |
||
| 168 | public function storage(Readable $storage = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the storage shared to all instances of this model. |
||
| 177 | * |
||
| 178 | * @return Readable |
||
| 179 | */ |
||
| 180 | public static function getSharedStorage() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Share the given database connection to all instances of this model. |
||
| 187 | * |
||
| 188 | * @param Readable $storage |
||
| 189 | */ |
||
| 190 | public static function setSharedStorage(Readable $storage) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Prepare the record's data for storage. |
||
| 197 | * |
||
| 198 | * This is here until repositories are implemented. |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | protected function prepareData() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Prepare the given filter. |
||
| 238 | * |
||
| 239 | * Creates a filter for the record's key attribute if the given value is not |
||
| 240 | * an array. |
||
| 241 | * |
||
| 242 | * TODO: Filter by key if $filter has numeric keys |
||
| 243 | * |
||
| 244 | * @param mixed $filter |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | protected static function prepareFilter($filter) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Prepare the given list data. |
||
| 263 | * |
||
| 264 | * @param array $data |
||
| 265 | * @param string $attribute |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | protected static function prepareListing($data, $attribute) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Load record data from storage using the given criteria. |
||
| 286 | * |
||
| 287 | * @param array|string|int $filter [optional] |
||
| 288 | * @param array|string $order [optional] |
||
| 289 | * @param int $limit [optional] |
||
| 290 | * @param int $offset [optional] |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public static function load($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Load a record instance from storage using the given criteria. |
||
| 304 | * |
||
| 305 | * Returns false if the record cannot be found. |
||
| 306 | * |
||
| 307 | * @param array|string|int $filter [optional] |
||
| 308 | * @param array|string $order [optional] |
||
| 309 | * @return Record|bool |
||
| 310 | */ |
||
| 311 | public static function find($filter = array(), $order = array()) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Load a record instance from storage using the given criteria or create a |
||
| 328 | * new instance if nothing is found. |
||
| 329 | * |
||
| 330 | * @param array|string|int $filter [optional] |
||
| 331 | * @param array|string $order [optional] |
||
| 332 | * @return Record |
||
| 333 | */ |
||
| 334 | public static function findOrNew($filter = array(), $order = array()) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Load multiple record instances matching the given IDs. |
||
| 349 | * |
||
| 350 | * @param array|string|int $ids |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public static function in($ids = array()) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Load multiple record instances from storage using the given criteria. |
||
| 360 | * |
||
| 361 | * @param array|string|int $filter [optional] |
||
| 362 | * @param array|string $order [optional] |
||
| 363 | * @param int $limit [optional] |
||
| 364 | * @param int $offset [optional] |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | public static function all($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Eagerly load the given relations of multiple record instances. |
||
| 374 | * |
||
| 375 | * @param array|string $relations |
||
| 376 | * @param array|string|int $filter [optional] |
||
| 377 | * @param array|string $order [optional] |
||
| 378 | * @param int $limit [optional] |
||
| 379 | * @param int $offset [optional] |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public static function eager($relations, $filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Search for record instances in storage using the given criteria. |
||
| 398 | * |
||
| 399 | * @param string $query |
||
| 400 | * @param array $attributes [optional] |
||
| 401 | * @param array|string|int $filter [optional] |
||
| 402 | * @param array|string $order [optional] |
||
| 403 | * @param int $limit [optional] |
||
| 404 | * @param int $offset [optional] |
||
| 405 | * @return array |
||
| 406 | * @throws Exception |
||
| 407 | */ |
||
| 408 | public static function search($query, $attributes = array(), $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Retrieve key => value pairs using `id` for keys and the given attribute |
||
| 426 | * for values. |
||
| 427 | * |
||
| 428 | * @param string $attribute |
||
| 429 | * @param array $filter [optional] |
||
| 430 | * @param array $order [optional] |
||
| 431 | * @param int $limit [optional] |
||
| 432 | * @param int $offset [optional] |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public static function listing($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Retrieve the distinct values of the given attribute. |
||
| 447 | * |
||
| 448 | * @param string $attribute |
||
| 449 | * @param array $filter [optional] |
||
| 450 | * @param array $order [optional] |
||
| 451 | * @param int $limit [optional] |
||
| 452 | * @param int $offset [optional] |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public static function distinct($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Create a query builder for the model. |
||
| 469 | * |
||
| 470 | * @return Builder |
||
| 471 | * @throws Exception |
||
| 472 | */ |
||
| 473 | public static function query() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Create the model in storage. |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | protected function saveNew() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Update the model in storage. |
||
| 522 | * |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | protected function saveExisting() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Save the record to storage. |
||
| 549 | * |
||
| 550 | * @param array $options [optional] |
||
| 551 | * @return bool |
||
| 552 | * @throws Exception |
||
| 553 | */ |
||
| 554 | public function save(array $options = array()) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Save multiple record instances to storage. |
||
| 594 | * |
||
| 595 | * Returns the number of instances that saved successfully. |
||
| 596 | * |
||
| 597 | * @param array $instances |
||
| 598 | * @param array $options [optional] |
||
| 599 | * @return int |
||
| 600 | */ |
||
| 601 | public static function saveMany($instances, array $options = array()) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Delete the record from storage. |
||
| 616 | * |
||
| 617 | * @return bool |
||
| 618 | */ |
||
| 619 | public function delete() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Retrieve the list of relation attributes for this model. |
||
| 632 | * |
||
| 633 | * @return array |
||
| 634 | */ |
||
| 635 | public function relationAttributes() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Determine whether the given attribute is a relation. |
||
| 642 | * |
||
| 643 | * @param string $attribute |
||
| 644 | * @return bool |
||
| 645 | */ |
||
| 646 | protected function hasRelation($attribute) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Retrieve the given relation. |
||
| 655 | * |
||
| 656 | * @param string $attribute |
||
| 657 | * @return Relation |
||
| 658 | */ |
||
| 659 | public function relation($attribute) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Retrieve all relations. |
||
| 683 | * |
||
| 684 | * @return Relation[] |
||
| 685 | */ |
||
| 686 | public function relations() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Determine whether the given relation has any set models. |
||
| 699 | * |
||
| 700 | * @param string $attribute |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | protected function hasRelated($attribute) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Retrieve the models of the given relation. |
||
| 712 | * |
||
| 713 | * @param string $attribute |
||
| 714 | * @return array |
||
| 715 | */ |
||
| 716 | public function related($attribute) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Set the given related models. |
||
| 731 | * |
||
| 732 | * @param string $attribute |
||
| 733 | * @param mixed $value |
||
| 734 | */ |
||
| 735 | protected function setRelated($attribute, $value) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Unset the models of the given relation. |
||
| 754 | * |
||
| 755 | * @param string $attribute |
||
| 756 | */ |
||
| 757 | protected function unsetRelated($attribute) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Save all of the model's relations. |
||
| 768 | */ |
||
| 769 | public function saveRelations() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Retrieve the default search attributes for the model. |
||
| 778 | * |
||
| 779 | * @return array |
||
| 780 | */ |
||
| 781 | public function defaultSearchAttributes() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Retrieve a relation. Shortcut for `relation()`. |
||
| 788 | * |
||
| 789 | * @param string $method |
||
| 790 | * @param array $arguments |
||
| 791 | * @return Relation |
||
| 792 | * @throws Exception |
||
| 793 | */ |
||
| 794 | public function __call($method, $arguments) |
||
| 802 | } |
||
| 803 |