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 \Darya\Storage\Readable Instance storage |
||
| 32 | */ |
||
| 33 | protected $storage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Darya\Storage\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) { |
||
| 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) { |
||
| 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) { |
||
| 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() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get and optionally set the model's storage instance. |
||
| 124 | * |
||
| 125 | * @return \Darya\Storage\Readable |
||
| 126 | */ |
||
| 127 | public function storage(Readable $storage = null) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the storage shared to all instances of this model. |
||
| 135 | * |
||
| 136 | * @return \Darya\Storage\Readable |
||
| 137 | */ |
||
| 138 | public static function getSharedStorage() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Share the given database connection to all instances of this model. |
||
| 144 | * |
||
| 145 | * @param \Darya\Storage\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() { |
||
| 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) { |
||
| 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) { |
||
| 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) { |
||
| 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()) { |
||
| 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()) { |
||
| 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) { |
||
| 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 | * Determine whether the given attribute is a relation. |
||
| 488 | * |
||
| 489 | * @param string $attribute |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | protected function hasRelation($attribute) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Retrieve the given relation. |
||
| 500 | * |
||
| 501 | * @param string $attribute |
||
| 502 | * @return \Darya\ORM\Relation |
||
| 503 | */ |
||
| 504 | public function relation($attribute) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Determine whether the given relation has any set model(s). |
||
| 526 | * |
||
| 527 | * @param string $attribute |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | protected function hasRelated($attribute) { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Retrieve the model(s) of the given relation. |
||
| 538 | * |
||
| 539 | * @param string $attribute |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | public function related($attribute) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Set the given related model(s). |
||
| 556 | * |
||
| 557 | * @param string $attribute |
||
| 558 | * @param mixed $value |
||
| 559 | */ |
||
| 560 | protected function setRelated($attribute, $value) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Retrieve the default search attributes for the model. |
||
| 576 | * |
||
| 577 | * @return array |
||
| 578 | */ |
||
| 579 | public function defaultSearchAttributes() { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Retrieve a relation. Shortcut for `relation()`. |
||
| 585 | * |
||
| 586 | * @param string $method |
||
| 587 | * @param array $arguments |
||
| 588 | * @return Relation |
||
| 589 | */ |
||
| 590 | public function __call($method, $arguments) { |
||
| 593 | |||
| 594 | } |
||
| 595 |
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: