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 | * Overrides the name of the database table that persists the model. The |
||
| 24 | * model's lowercased class name is used if this is not set. |
||
| 25 | * |
||
| 26 | * @var string Database table name |
||
| 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 | */ |
||
| 78 | public function has($attribute) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Retrieve the given attribute or relation from the record. |
||
| 85 | * |
||
| 86 | * @param string $attribute |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | 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) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Retrieve the name of the table this model belongs to. |
||
| 123 | * |
||
| 124 | * If none is set, it defaults to creating it from the class name. |
||
| 125 | * |
||
| 126 | * For example: |
||
| 127 | * Page -> pages |
||
| 128 | * PageSection -> page_sections |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function table() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get and optionally set the model's storage instance. |
||
| 148 | * |
||
| 149 | * @return Readable |
||
| 150 | */ |
||
| 151 | public function storage(Readable $storage = null) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the storage shared to all instances of this model. |
||
| 160 | * |
||
| 161 | * @return Readable |
||
| 162 | */ |
||
| 163 | public static function getSharedStorage() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Share the given database connection to all instances of this model. |
||
| 170 | * |
||
| 171 | * @param Readable $storage |
||
| 172 | */ |
||
| 173 | public static function setSharedStorage(Readable $storage) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Prepare the record's data for storage. This is here until repositories |
||
| 180 | * are implemented. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | protected function prepareData() |
||
| 185 | { |
||
| 186 | $types = $this->attributes; |
||
| 187 | |||
| 188 | $changed = array_intersect_key($this->data, array_flip($this->changed)); |
||
| 189 | |||
| 190 | $data = $this->id() ? $changed : $this->data; |
||
| 191 | |||
| 192 | foreach ($data as $attribute => $value) { |
||
| 193 | if (isset($types[$attribute])) { |
||
| 194 | $type = $types[$attribute]; |
||
| 195 | |||
| 196 | switch ($type) { |
||
| 197 | case 'int': |
||
| 198 | $value = (int) $value; |
||
| 199 | break; |
||
| 200 | case 'date': |
||
| 201 | $value = date('Y-m-d', $value); |
||
| 202 | break; |
||
| 203 | case 'datetime': |
||
| 204 | $value = date('Y-m-d H:i:s', $value); |
||
| 205 | break; |
||
| 206 | case 'time': |
||
| 207 | $value = date('H:i:s', $value); |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | |||
| 211 | $data[$attribute] = $value; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | return $data; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Prepare the given filter. |
||
| 220 | * |
||
| 221 | * Creates a filter for the record's key attribute if the given value is not |
||
| 222 | * an array. |
||
| 223 | * |
||
| 224 | * @param mixed $filter |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | protected static function prepareFilter($filter) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Prepare the given list data. |
||
| 243 | * |
||
| 244 | * @param array $data |
||
| 245 | * @param string $attribute |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | protected static function prepareListing($data, $attribute) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Load record data from storage using the given criteria. |
||
| 266 | * |
||
| 267 | * @param array|string|int $filter [optional] |
||
| 268 | * @param array|string $order [optional] |
||
| 269 | * @param int $limit [optional] |
||
| 270 | * @param int $offset [optional] |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public static function load($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Load a record instance from storage using the given criteria. |
||
| 284 | * |
||
| 285 | * Returns false if the record cannot be found. |
||
| 286 | * |
||
| 287 | * @param array|string|int $filter [optional] |
||
| 288 | * @param array|string $order [optional] |
||
| 289 | * @return Record|bool |
||
| 290 | */ |
||
| 291 | public static function find($filter = array(), $order = array()) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Load a record instance from storage using the given criteria or create a |
||
| 308 | * new instance if nothing is found. |
||
| 309 | * |
||
| 310 | * @param array|string|int $filter [optional] |
||
| 311 | * @param array|string $order [optional] |
||
| 312 | * @return Record |
||
| 313 | */ |
||
| 314 | public static function findOrNew($filter = array(), $order = array()) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Load multiple record instances from storage using the given criteria. |
||
| 329 | * |
||
| 330 | * @param array|string|int $filter [optional] |
||
| 331 | * @param array|string $order [optional] |
||
| 332 | * @param int $limit [optional] |
||
| 333 | * @param int $offset [optional] |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public static function all($filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Eagerly load the given relations of multiple record instances. |
||
| 343 | * |
||
| 344 | * @param array|string $relations |
||
| 345 | * @param array|string|int $filter [optional] |
||
| 346 | * @param array|string $order [optional] |
||
| 347 | * @param int $limit [optional] |
||
| 348 | * @param int $offset [optional] |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public static function eager($relations, $filter = array(), $order = array(), $limit = 0, $offset = 0) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Search for record instances in storage using the given criteria. |
||
| 367 | * |
||
| 368 | * @param string $query |
||
| 369 | * @param array $attributes [optional] |
||
| 370 | * @param array|string|int $filter [optional] |
||
| 371 | * @param array|string $order [optional] |
||
| 372 | * @param int $limit [optional] |
||
| 373 | * @param int $offset [optional] |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public static function search($query, $attributes = array(), $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Retrieve key => value pairs using `id` for keys and the given attribute |
||
| 394 | * for values. |
||
| 395 | * |
||
| 396 | * @param string $attribute |
||
| 397 | * @param array $filter [optional] |
||
| 398 | * @param array $order [optional] |
||
| 399 | * @param int $limit [optional] |
||
| 400 | * @param int $offset [optional] |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public static function listing($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Retrieve the distinct values of the given attribute. |
||
| 415 | * |
||
| 416 | * @param string $attribute |
||
| 417 | * @param array $filter [optional] |
||
| 418 | * @param array $order [optional] |
||
| 419 | * @param int $limit [optional] |
||
| 420 | * @param int $offset [optional] |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public static function distinct($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Create a query builder for the model. |
||
| 437 | * |
||
| 438 | * @return Builder |
||
| 439 | */ |
||
| 440 | public static function query() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Save the record to storage. |
||
| 461 | * |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | public function save() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Save multiple record instances to storage. |
||
| 524 | * |
||
| 525 | * Returns the number of instances that saved successfully. |
||
| 526 | * |
||
| 527 | * @param array $instances |
||
| 528 | * @return int |
||
| 529 | */ |
||
| 530 | public static function saveMany($instances) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Delete the record from storage. |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function delete() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Retrieve the list of relation attributes for this model. |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | public function relationAttributes() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Determine whether the given attribute is a relation. |
||
| 573 | * |
||
| 574 | * @param string $attribute |
||
| 575 | * @return bool |
||
| 576 | */ |
||
| 577 | protected function hasRelation($attribute) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Retrieve the given relation. |
||
| 586 | * |
||
| 587 | * @param string $attribute |
||
| 588 | * @return Relation |
||
| 589 | */ |
||
| 590 | public function relation($attribute) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Retrieve all relations. |
||
| 614 | * |
||
| 615 | * @return Relation[] |
||
| 616 | */ |
||
| 617 | public function relations() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Determine whether the given relation has any set model(s). |
||
| 630 | * |
||
| 631 | * @param string $attribute |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | protected function hasRelated($attribute) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Retrieve the model(s) of the given relation. |
||
| 643 | * |
||
| 644 | * @param string $attribute |
||
| 645 | * @return array |
||
| 646 | */ |
||
| 647 | public function related($attribute) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Set the given related model(s). |
||
| 662 | * |
||
| 663 | * @param string $attribute |
||
| 664 | * @param mixed $value |
||
| 665 | */ |
||
| 666 | protected function setRelated($attribute, $value) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Retrieve the default search attributes for the model. |
||
| 683 | * |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | public function defaultSearchAttributes() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Retrieve a relation. Shortcut for `relation()`. |
||
| 693 | * |
||
| 694 | * @param string $method |
||
| 695 | * @param array $arguments |
||
| 696 | * @return Relation |
||
| 697 | */ |
||
| 698 | public function __call($method, $arguments) |
||
| 702 | } |
||
| 703 |