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 |
||
| 20 | class Record extends Model |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Database table name. |
||
| 24 | * |
||
| 25 | * Overrides the name of the database table that persists the model. The |
||
| 26 | * model's lower-cased class name is used if this is not set. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $table; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Instance storage. |
||
| 34 | * |
||
| 35 | * @var Readable |
||
| 36 | */ |
||
| 37 | protected $storage; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Shared storage. |
||
| 41 | * |
||
| 42 | * @var Readable |
||
| 43 | */ |
||
| 44 | protected static $sharedStorage; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Definitions of related models. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $relations = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Default searchable attributes. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $search = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Instantiate a new record with the given data or load an instance from |
||
| 62 | * storage if the given data is a valid primary key. |
||
| 63 | * |
||
| 64 | * @param mixed $data An array of key-value attributes to set or a primary key to load by |
||
| 65 | */ |
||
| 66 | public function __construct($data = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Determine whether the given attribute or relation is set on the record. |
||
| 77 | * |
||
| 78 | * @param string $attribute |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function has($attribute) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Retrieve the given attribute or relation from the record. |
||
| 88 | * |
||
| 89 | * @param string $attribute |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | public function get($attribute) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set the value of an attribute or relation on the model. |
||
| 111 | * |
||
| 112 | * @param string $attribute |
||
| 113 | * @param mixed $value |
||
| 114 | */ |
||
| 115 | public function set($attribute, $value = null) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Unset the value for an attribute or relation on the model. |
||
| 128 | * |
||
| 129 | * @param string $attribute |
||
| 130 | */ |
||
| 131 | public function remove($attribute) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Retrieve the name of the table this model belongs to. |
||
| 142 | * |
||
| 143 | * If none is set, it defaults to creating it from the class name. |
||
| 144 | * |
||
| 145 | * For example: |
||
| 146 | * Page -> pages |
||
| 147 | * PageSection -> page_sections |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function table() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get and optionally set the model's storage instance. |
||
| 167 | * |
||
| 168 | * @param Readable $storage [optional] |
||
| 169 | * @return Readable |
||
| 170 | */ |
||
| 171 | public function storage(Readable $storage = null) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the storage shared to all instances of this model. |
||
| 180 | * |
||
| 181 | * @return Readable |
||
| 182 | */ |
||
| 183 | public static function getSharedStorage() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Share the given database connection to all instances of this model. |
||
| 190 | * |
||
| 191 | * @param Readable $storage |
||
| 192 | */ |
||
| 193 | public static function setSharedStorage(Readable $storage) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Prepare the record's data for storage. This is here until repositories |
||
| 200 | * are implemented. |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | protected function prepareData() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Prepare the given filter. |
||
| 240 | * |
||
| 241 | * Creates a filter for the record's key attribute if the given value is not |
||
| 242 | * an array. |
||
| 243 | * |
||
| 244 | * TODO: Filter by key if $filter has numeric keys |
||
| 245 | * |
||
| 246 | * @param mixed $filter |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | protected static function prepareFilter($filter) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Prepare the given list data. |
||
| 265 | * |
||
| 266 | * @param array $data |
||
| 267 | * @param string $attribute |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected static function prepareListing($data, $attribute) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Load record data from storage using the given criteria. |
||
| 288 | * |
||
| 289 | * @param array|string|int $filter [optional] |
||
| 290 | * @param array|string $order [optional] |
||
| 291 | * @param int $limit [optional] |
||
| 292 | * @param int $offset [optional] |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public static function load($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Load a record instance from storage using the given criteria. |
||
| 306 | * |
||
| 307 | * Returns false if the record cannot be found. |
||
| 308 | * |
||
| 309 | * @param array|string|int $filter [optional] |
||
| 310 | * @param array|string $order [optional] |
||
| 311 | * @return Record|bool |
||
| 312 | */ |
||
| 313 | public static function find($filter = array(), $order = array()) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Load a record instance from storage using the given criteria or create a |
||
| 330 | * new instance if nothing is found. |
||
| 331 | * |
||
| 332 | * @param array|string|int $filter [optional] |
||
| 333 | * @param array|string $order [optional] |
||
| 334 | * @return Record |
||
| 335 | */ |
||
| 336 | public static function findOrNew($filter = array(), $order = array()) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Load multiple record instances matching the given IDs. |
||
| 351 | * |
||
| 352 | * @param array|string|int $ids |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public static function in($ids = array()) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Load multiple record instances from storage using the given criteria. |
||
| 362 | * |
||
| 363 | * @param array|string|int $filter [optional] |
||
| 364 | * @param array|string $order [optional] |
||
| 365 | * @param int $limit [optional] |
||
| 366 | * @param int $offset [optional] |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public static function all($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Eagerly load the given relations of multiple record instances. |
||
| 376 | * |
||
| 377 | * @param array|string $relations |
||
| 378 | * @param array|string|int $filter [optional] |
||
| 379 | * @param array|string $order [optional] |
||
| 380 | * @param int $limit [optional] |
||
| 381 | * @param int $offset [optional] |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public static function eager($relations, $filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Search for record instances in storage using the given criteria. |
||
| 400 | * |
||
| 401 | * @param string $query |
||
| 402 | * @param array $attributes [optional] |
||
| 403 | * @param array|string|int $filter [optional] |
||
| 404 | * @param array|string $order [optional] |
||
| 405 | * @param int $limit [optional] |
||
| 406 | * @param int $offset [optional] |
||
| 407 | * @return array |
||
| 408 | * @throws Exception |
||
| 409 | */ |
||
| 410 | public static function search($query, $attributes = array(), $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Retrieve key => value pairs using `id` for keys and the given attribute |
||
| 428 | * for values. |
||
| 429 | * |
||
| 430 | * @param string $attribute |
||
| 431 | * @param array $filter [optional] |
||
| 432 | * @param array $order [optional] |
||
| 433 | * @param int $limit [optional] |
||
| 434 | * @param int $offset [optional] |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | public static function listing($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Retrieve the distinct values of the given attribute. |
||
| 449 | * |
||
| 450 | * @param string $attribute |
||
| 451 | * @param array $filter [optional] |
||
| 452 | * @param array $order [optional] |
||
| 453 | * @param int $limit [optional] |
||
| 454 | * @param int $offset [optional] |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | public static function distinct($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Create a query builder for the model. |
||
| 471 | * |
||
| 472 | * @return Builder |
||
| 473 | * @throws Exception |
||
| 474 | */ |
||
| 475 | public static function query() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Create the model in storage. |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | protected function saveNew() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Update the model in storage. |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | protected function saveExisting() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Save the record to storage. |
||
| 551 | * |
||
| 552 | * @param array $options [optional] |
||
| 553 | * @return bool |
||
| 554 | * @throws Exception |
||
| 555 | */ |
||
| 556 | public function save(array $options = array()) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Save multiple record instances to storage. |
||
| 596 | * |
||
| 597 | * Returns the number of instances that saved successfully. |
||
| 598 | * |
||
| 599 | * @param array $instances |
||
| 600 | * @param array $options [optional] |
||
| 601 | * @return int |
||
| 602 | */ |
||
| 603 | public static function saveMany($instances, array $options = array()) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Delete the record from storage. |
||
| 618 | * |
||
| 619 | * @return bool |
||
| 620 | */ |
||
| 621 | public function delete() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Retrieve the list of relation attributes for this model. |
||
| 634 | * |
||
| 635 | * @return array |
||
| 636 | */ |
||
| 637 | public function relationAttributes() |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Determine whether the given attribute is a relation. |
||
| 644 | * |
||
| 645 | * @param string $attribute |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | protected function hasRelation($attribute) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Retrieve the given relation. |
||
| 657 | * |
||
| 658 | * @param string $attribute |
||
| 659 | * @return Relation |
||
| 660 | */ |
||
| 661 | public function relation($attribute) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Retrieve all relations. |
||
| 685 | * |
||
| 686 | * @return Relation[] |
||
| 687 | */ |
||
| 688 | public function relations() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Determine whether the given relation has any set models. |
||
| 701 | * |
||
| 702 | * @param string $attribute |
||
| 703 | * @return bool |
||
| 704 | */ |
||
| 705 | protected function hasRelated($attribute) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Retrieve the models of the given relation. |
||
| 714 | * |
||
| 715 | * @param string $attribute |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | public function related($attribute) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Set the given related models. |
||
| 733 | * |
||
| 734 | * @param string $attribute |
||
| 735 | * @param mixed $value |
||
| 736 | */ |
||
| 737 | protected function setRelated($attribute, $value) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Unset the models of the given relation. |
||
| 756 | * |
||
| 757 | * @param string $attribute |
||
| 758 | */ |
||
| 759 | protected function unsetRelated($attribute) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Save all of the model's relations. |
||
| 770 | */ |
||
| 771 | public function saveRelations() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Retrieve the default search attributes for the model. |
||
| 780 | * |
||
| 781 | * @return array |
||
| 782 | */ |
||
| 783 | public function defaultSearchAttributes() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Retrieve a relation. Shortcut for `relation()`. |
||
| 790 | * |
||
| 791 | * @param string $method |
||
| 792 | * @param array $arguments |
||
| 793 | * @return Relation |
||
| 794 | */ |
||
| 795 | public function __call($method, $arguments) |
||
| 799 | } |
||
| 800 |