Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class Model implements ArrayAccess, IteratorAggregate, Serializable { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Attribute names as keys and types as values. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $attributes = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The model data. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $data = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Whether the model is currently in a valid state. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $valid = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array Errors that occured with validation |
||
| 43 | */ |
||
| 44 | protected $errors = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The attribute that uniquely identifies the model. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $key; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Attributes that have been modified. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $changed = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Instantiate a new model. |
||
| 62 | * |
||
| 63 | * @param array $data [optional] Attributes to set on the model |
||
| 64 | */ |
||
| 65 | public function __construct($data = array()) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Generate multiple instances of the model using arrays of attributes. |
||
| 71 | * |
||
| 72 | * @param array $rows |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public static function generate(array $rows = array()) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Generate multiple instances of the model with the assumption that they |
||
| 87 | * aren't new. |
||
| 88 | * |
||
| 89 | * Equivalent to generate() but with a reinstate() call on each new model. |
||
| 90 | * |
||
| 91 | * @param array $rows |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public static function hydrate(array $rows = array()) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Recursively convert a model, or set of models, to an array. |
||
| 106 | * |
||
| 107 | * @param Model[]|Model $model |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public static function convertToArray($model) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Recursively convert a model, or set of models, to JSON. |
||
| 130 | * |
||
| 131 | * @param Model[]|Model $model |
||
| 132 | * @param int $options [optional] json_encode() options |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public static function convertToJson($model, $options = null) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Prepare the given attribute name. |
||
| 141 | * |
||
| 142 | * @param string $attribute |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | protected function prepareAttribute($attribute) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Retrieve the attribute names of the model. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function attributes() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Retrieve the attribute types of the model. |
||
| 166 | * |
||
| 167 | * Attribute names are keys and types are values. |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function attributeTypes() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the transformer for accessing attributes. |
||
| 177 | * |
||
| 178 | * @return Transformer |
||
| 179 | */ |
||
| 180 | public function getAttributeAccessor() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the transformer for mutating attributes. |
||
| 186 | * |
||
| 187 | * @return Transformer |
||
| 188 | */ |
||
| 189 | public function getAttributeMutator() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieve the name of the attribute that uniquely identifies this model. |
||
| 195 | * |
||
| 196 | * Defaults to 'id' if the `key` property is unset. |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function key() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Retrieve the value of the attribute that uniquely identifies this model. |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function id() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Retrieve raw attributes that have changed. |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function changed() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Clear the record of changed attributes on the model, declaring the |
||
| 228 | * current attributes as unmodified. |
||
| 229 | */ |
||
| 230 | public function reinstate() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Retrieve the model's attributes. |
||
| 236 | * |
||
| 237 | * This method uses accessors to retrieve the data. |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function data() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Retrieve the model's raw attributes. |
||
| 253 | * |
||
| 254 | * This method returns the raw data without using any accessors. |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function rawData() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Determine whether the given attribute is set on the model. |
||
| 264 | * |
||
| 265 | * @param string $attribute |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function has($attribute) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Determine whether the given attribute has a defined type. |
||
| 274 | * |
||
| 275 | * @param string $attribute |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | protected function mutable($attribute) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Unmutate the given attribute to be retrieved. |
||
| 284 | * |
||
| 285 | * @param string $attribute |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | protected function access($attribute) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Mutate the given attribute to be set on the model. |
||
| 310 | * |
||
| 311 | * @param string $attribute |
||
| 312 | * @param mixed $value [optional] |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | protected function mutate($attribute, $value = null) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Retrieve the given attribute from the model. |
||
| 329 | * |
||
| 330 | * @param string $attribute |
||
| 331 | * @return mixed |
||
| 332 | */ |
||
| 333 | public function get($attribute) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set the value of an attribute on the model. |
||
| 343 | * |
||
| 344 | * If attribute is an array it will be forwarded to `setMany()`. |
||
| 345 | * |
||
| 346 | * @param array|string $attribute |
||
| 347 | * @param mixed $value [optional] |
||
| 348 | */ |
||
| 349 | public function set($attribute, $value = null) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set the values of the given attributes on the model. |
||
| 365 | * |
||
| 366 | * @param array $values |
||
| 367 | */ |
||
| 368 | public function setMany($values) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Remove the value of an attribute. |
||
| 376 | * |
||
| 377 | * @param string $attribute |
||
| 378 | */ |
||
| 379 | public function remove($attribute) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Retrieve the format to use for date attributes. |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function dateFormat() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set the `created` and `modified` attributes using the given timestamp. |
||
| 394 | * |
||
| 395 | * Defaults to the current system time if none is given. Only sets `created` |
||
| 396 | * attribute if `id` evaluates to false. |
||
| 397 | * |
||
| 398 | * @param int $time [optional] |
||
| 399 | */ |
||
| 400 | public function stamp($time = null) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Validate all of the model's attributes. |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function validate() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Retrieve an array of error strings generated by the last validation |
||
| 420 | * attempt. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function errors() { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Recursively convert the model to an array. |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function toArray() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Serialize the model as a JSON string. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function toJson() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Determine whether an attribute is set on the model. Shortcut for `set()`. |
||
| 448 | * |
||
| 449 | * @param string $property |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function __isset($property) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Retrieve an attribute from the model. Shortcut for `get()` and `id()`. |
||
| 458 | * |
||
| 459 | * @param string $property |
||
| 460 | * @return mixed |
||
| 461 | */ |
||
| 462 | public function __get($property) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set an attribute's value. Shortcut for `set()`. |
||
| 468 | * |
||
| 469 | * @param string $property |
||
| 470 | * @param mixed $value |
||
| 471 | */ |
||
| 472 | public function __set($property, $value) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Unset an attribute's value. Shortcut for `remove()`. |
||
| 478 | * |
||
| 479 | * @param string $property |
||
| 480 | */ |
||
| 481 | public function __unset($property) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Determine whether an attribute is set at the given offset. |
||
| 487 | * |
||
| 488 | * @param mixed $offset |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function offsetExists($offset) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get the attribute at the given offset. |
||
| 497 | * |
||
| 498 | * @param mixed $offset |
||
| 499 | * @return mixed |
||
| 500 | */ |
||
| 501 | public function offsetGet($offset) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Set the attribute at the given offset. |
||
| 507 | * |
||
| 508 | * @param mixed $offset |
||
| 509 | * @param mixed $value |
||
| 510 | */ |
||
| 511 | public function offsetSet($offset, $value) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Unset the attribute at the given offset. |
||
| 517 | * |
||
| 518 | * @param mixed $offset |
||
| 519 | */ |
||
| 520 | public function offsetUnset($offset) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Retrieve an iterator for the model's data. |
||
| 526 | * |
||
| 527 | * @return \Traversable |
||
| 528 | */ |
||
| 529 | public function getIterator() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Serialize the model's raw data. |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function serialize() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Unserialize the model's raw data. |
||
| 544 | * |
||
| 545 | * @param string $serialized |
||
| 546 | */ |
||
| 547 | public function unserialize($serialized) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Prepare the model's attributes for JSON serialization. |
||
| 553 | * |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | public function jsonSerialize() { |
||
| 559 | |||
| 560 | } |
||
| 561 |