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 | * @var Readable Instance storage |
||
| 32 | */ |
||
| 33 | protected $storage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Readable Shared storage |
||
| 37 | */ |
||
| 38 | protected static $sharedStorage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array Definitions of related models |
||
| 42 | */ |
||
| 43 | protected $relations = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array Default searchable attributes |
||
| 47 | */ |
||
| 48 | protected $search = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Instantiate a new record with the given data or load an instance from |
||
| 52 | * storage if the given data is a valid primary key. |
||
| 53 | * |
||
| 54 | * @param mixed $data An array of key-value attributes to set or a primary key to load by |
||
| 55 | */ |
||
| 56 | public function __construct($data = null) { |
||
| 57 | if (is_numeric($data) || is_string($data)) { |
||
| 58 | $this->data = static::load($data); |
||
| 59 | } |
||
| 60 | |||
| 61 | parent::__construct($data); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Determine whether the given attribute or relation is set on the record. |
||
| 66 | * |
||
| 67 | * @param string $attribute |
||
| 68 | */ |
||
| 69 | public function has($attribute) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Retrieve the given attribute or relation from the record. |
||
| 75 | * |
||
| 76 | * @param string $attribute |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | public function get($attribute) { |
||
| 80 | if ($this->hasRelation($attribute)) { |
||
| 81 | return $this->related($attribute); |
||
| 82 | } |
||
| 83 | |||
| 84 | return parent::get($attribute); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set the value of an attribute or relation on the model. |
||
| 89 | * |
||
| 90 | * @param string $attribute |
||
| 91 | * @param mixed $value |
||
| 92 | */ |
||
| 93 | public function set($attribute, $value = null) { |
||
| 94 | if (is_string($attribute) && $this->hasRelation($attribute)) { |
||
| 95 | return $this->setRelated($attribute, $value); |
||
| 96 | } |
||
| 97 | |||
| 98 | parent::set($attribute, $value); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Retrieve the name of the table this model belongs to. |
||
| 103 | * |
||
| 104 | * If none is set, it defaults to creating it from the class name. |
||
| 105 | * |
||
| 106 | * For example: |
||
| 107 | * Page -> pages |
||
| 108 | * PageSection -> page_sections |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function table() { |
||
| 113 | if ($this->table) { |
||
| 114 | return $this->table; |
||
| 115 | } |
||
| 116 | |||
| 117 | return preg_replace_callback('/([A-Z])/', function($matches) { |
||
| 118 | return '_' . strtolower($matches[1]); |
||
| 119 | }, lcfirst(basename(get_class($this)))) . 's'; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get and optionally set the model's storage instance. |
||
| 124 | * |
||
| 125 | * @return Readable |
||
| 126 | */ |
||
| 127 | public function storage(Readable $storage = null) { |
||
| 128 | $this->storage = $storage ?: $this->storage; |
||
| 129 | |||
| 130 | return $this->storage ?: static::getSharedStorage(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the storage shared to all instances of this model. |
||
| 135 | * |
||
| 136 | * @return Readable |
||
| 137 | */ |
||
| 138 | public static function getSharedStorage() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Share the given database connection to all instances of this model. |
||
| 144 | * |
||
| 145 | * @param Readable $storage |
||
| 146 | */ |
||
| 147 | public static function setSharedStorage(Readable $storage) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Prepare the record's data for storage. This is here until repositories |
||
| 153 | * are implemented. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | protected function prepareData() { |
||
| 158 | $types = $this->attributes; |
||
| 159 | |||
| 160 | $changed = array_intersect_key($this->data, array_flip($this->changed)); |
||
| 161 | |||
| 162 | $data = $this->id() && $changed ? $changed : $this->data; |
||
| 163 | |||
| 164 | foreach ($data as $attribute => $value) { |
||
| 165 | if (isset($types[$attribute])) { |
||
| 166 | $type = $types[$attribute]; |
||
| 167 | |||
| 168 | switch ($type) { |
||
| 169 | case 'int': |
||
| 170 | $value = (int) $value; |
||
| 171 | break; |
||
| 172 | case 'date': |
||
| 173 | $value = date('Y-m-d', $value); |
||
| 174 | break; |
||
| 175 | case 'datetime': |
||
| 176 | $value = date('Y-m-d H:i:s', $value); |
||
| 177 | break; |
||
| 178 | case 'time': |
||
| 179 | $value = date('H:i:s', $value); |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | |||
| 183 | $data[$attribute] = $value; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $data; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Prepare the given filter. |
||
| 192 | * |
||
| 193 | * Creates a filter for the record's key attribute if the given value is not |
||
| 194 | * an array. |
||
| 195 | * |
||
| 196 | * @param mixed $filter |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | protected static function prepareFilter($filter) { |
||
| 200 | if ($filter === null) { |
||
| 201 | return array(); |
||
| 202 | } |
||
| 203 | |||
| 204 | if (!is_array($filter)) { |
||
| 205 | $instance = new static; |
||
| 206 | $filter = array($instance->key() => $filter); |
||
| 207 | } |
||
| 208 | |||
| 209 | return $filter; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Prepare the given list data. |
||
| 214 | * |
||
| 215 | * @param array $data |
||
| 216 | * @param string $attribute |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected static function prepareListing($data, $attribute) { |
||
| 220 | $instance = new static; |
||
| 221 | $key = $instance->key(); |
||
| 222 | |||
| 223 | $list = array(); |
||
| 224 | |||
| 225 | foreach ($data as $row) { |
||
| 226 | if (isset($row[$attribute])) { |
||
| 227 | $list[$row[$key]] = $row[$attribute]; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | return $list; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Load record data from storage using the given criteria. |
||
| 236 | * |
||
| 237 | * @param array|string|int $filter [optional] |
||
| 238 | * @param array|string $order [optional] |
||
| 239 | * @param int $limit [optional] |
||
| 240 | * @param int $offset [optional] |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public static function load($filter = array(), $order = array(), $limit = null, $offset = 0) { |
||
| 244 | $instance = new static; |
||
| 245 | $storage = $instance->storage(); |
||
| 246 | $filter = static::prepareFilter($filter); |
||
| 247 | |||
| 248 | return $storage->read($instance->table(), $filter, $order, $limit, $offset); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Load a record instance from storage using the given criteria. |
||
| 253 | * |
||
| 254 | * Returns false if the record cannot be found. |
||
| 255 | * |
||
| 256 | * @param array|string|int $filter [optional] |
||
| 257 | * @param array|string $order [optional] |
||
| 258 | * @return Record|bool |
||
| 259 | */ |
||
| 260 | public static function find($filter = array(), $order = array()) { |
||
| 261 | $data = static::load($filter, $order, 1); |
||
| 262 | |||
| 263 | return !empty($data[0]) ? new static($data[0]) : false; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Load a record instance from storage using the given criteria or create a |
||
| 268 | * new instance if nothing is found. |
||
| 269 | * |
||
| 270 | * @param array|string|int $filter [optional] |
||
| 271 | * @param array|string $order [optional] |
||
| 272 | * @return Record |
||
| 273 | */ |
||
| 274 | public static function findOrNew($filter = array(), $order = array()) { |
||
| 275 | $instance = static::find($filter, $order); |
||
| 276 | |||
| 277 | return $instance === false ? new static : $instance; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Load multiple record instances from storage using the given criteria. |
||
| 282 | * |
||
| 283 | * @param array|string|int $filter [optional] |
||
| 284 | * @param array|string $order [optional] |
||
| 285 | * @param int $limit [optional] |
||
| 286 | * @param int $offset [optional] |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public static function all($filter = array(), $order = array(), $limit = null, $offset = 0) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Eagerly load the given relations of multiple record instances. |
||
| 295 | * |
||
| 296 | * @param array|string $relations |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public static function eager($relations) { |
||
| 300 | $instance = new static; |
||
| 301 | $instances = static::all(); |
||
| 302 | |||
| 303 | foreach ((array) $relations as $relation) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Search for record instances in storage using the given criteria. |
||
| 314 | * |
||
| 315 | * @param string $query |
||
| 316 | * @param array $attributes [optional] |
||
| 317 | * @param array|string|int $filter [optional] |
||
| 318 | * @param array|string $order [optional] |
||
| 319 | * @param int $limit [optional] |
||
| 320 | * @param int $offset [optional] |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public static function search($query, $attributes = array(), $filter = array(), $order = array(), $limit = null, $offset = 0) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Retrieve key => value pairs using `id` for keys and the given attribute |
||
| 340 | * for values. |
||
| 341 | * |
||
| 342 | * @param string $attribute |
||
| 343 | * @param array $filter [optional] |
||
| 344 | * @param array $order [optional] |
||
| 345 | * @param int $limit [optional] |
||
| 346 | * @param int $offset [optional] |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public static function listing($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Retrieve the distinct values of the given attribute. |
||
| 360 | * |
||
| 361 | * @param string $attribute |
||
| 362 | * @param array $filter [optional] |
||
| 363 | * @param array $order [optional] |
||
| 364 | * @param int $limit [optional] |
||
| 365 | * @param int $offset [optional] |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public static function distinct($attribute, $filter = array(), $order = array(), $limit = null, $offset = 0) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Create a query builder for the model. |
||
| 381 | * |
||
| 382 | * @return Builder |
||
| 383 | */ |
||
| 384 | public static function query() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Save the record to storage. |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function save() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Save multiple record instances to storage. |
||
| 451 | * |
||
| 452 | * Returns the number of instances that saved successfully. |
||
| 453 | * |
||
| 454 | * @param array $instances |
||
| 455 | * @return int |
||
| 456 | */ |
||
| 457 | public static function saveMany($instances) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Delete the record from storage. |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function delete() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Retrieve the list of relation attributes for this model. |
||
| 488 | * |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | public function relationAttributes() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Determine whether the given attribute is a relation. |
||
| 497 | * |
||
| 498 | * @param string $attribute |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | protected function hasRelation($attribute) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Retrieve the given relation. |
||
| 509 | * |
||
| 510 | * @param string $attribute |
||
| 511 | * @return Relation |
||
| 512 | */ |
||
| 513 | public function relation($attribute) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Retrieve all relations. |
||
| 535 | * |
||
| 536 | * @return Relation[] |
||
| 537 | */ |
||
| 538 | public function relations() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Determine whether the given relation has any set model(s). |
||
| 550 | * |
||
| 551 | * @param string $attribute |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | protected function hasRelated($attribute) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Retrieve the model(s) of the given relation. |
||
| 562 | * |
||
| 563 | * @param string $attribute |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | public function related($attribute) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Set the given related model(s). |
||
| 580 | * |
||
| 581 | * @param string $attribute |
||
| 582 | * @param mixed $value |
||
| 583 | */ |
||
| 584 | protected function setRelated($attribute, $value) { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Retrieve the default search attributes for the model. |
||
| 600 | * |
||
| 601 | * @return array |
||
| 602 | */ |
||
| 603 | public function defaultSearchAttributes() { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Retrieve a relation. Shortcut for `relation()`. |
||
| 609 | * |
||
| 610 | * @param string $method |
||
| 611 | * @param array $arguments |
||
| 612 | * @return Relation |
||
| 613 | */ |
||
| 614 | public function __call($method, $arguments) { |
||
| 617 | |||
| 618 | } |
||
| 619 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: