Complex classes like Kohana_Jam_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 Kohana_Jam_Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 14 | abstract class Kohana_Jam_Model extends Jam_Validated { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var boolean Whether or not the model is loaded |
||
| 18 | */ |
||
| 19 | protected $_loaded = FALSE; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var boolean Whether or not the model is saved |
||
| 23 | */ |
||
| 24 | protected $_saved = FALSE; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var boolean Whether or not the model is saving |
||
| 28 | */ |
||
| 29 | protected $_is_saving = FALSE; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Boolean A flag that indicates a record has been deleted from the database |
||
| 33 | */ |
||
| 34 | protected $_deleted = FALSE; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * A key can be passed to automatically load a model by its |
||
| 40 | * unique key. |
||
| 41 | * |
||
| 42 | * @param mixed|null $key |
||
|
|
|||
| 43 | */ |
||
| 44 | 377 | public function __construct($meta_name = NULL) |
|
| 45 | { |
||
| 46 | 377 | parent::__construct($meta_name); |
|
| 47 | |||
| 48 | 377 | $this->meta()->events()->trigger('model.before_construct', $this); |
|
| 49 | |||
| 50 | // Copy over the defaults into the original data. |
||
| 51 | 377 | $this->_original = $this->meta()->defaults(); |
|
| 52 | |||
| 53 | 377 | $this->meta()->events()->trigger('model.after_construct', $this); |
|
| 54 | 377 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Gets the value for a field. |
||
| 58 | * |
||
| 59 | * @param string $name The field's name |
||
| 60 | * @return array|mixed |
||
| 61 | */ |
||
| 62 | 164 | public function get($name) |
|
| 63 | { |
||
| 64 | 164 | if ($association = $this->meta()->association($name)) |
|
| 65 | 164 | { |
|
| 66 | 20 | $name = $association->name; |
|
| 67 | |||
| 68 | 20 | return $association->get($this, Arr::get($this->_changed, $name), $this->changed($name)); |
|
| 69 | } |
||
| 70 | |||
| 71 | 163 | return parent::get($name); |
|
| 72 | } |
||
| 73 | |||
| 74 | public function __isset($name) |
||
| 75 | { |
||
| 76 | return ($this->meta()->association($name) OR parent::__isset($name)); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Sets the value of a field. |
||
| 81 | * |
||
| 82 | * You can pass an array of key => value pairs |
||
| 83 | * to set multiple fields at the same time: |
||
| 84 | * |
||
| 85 | * $model->set(array( |
||
| 86 | * 'field1' => 'value', |
||
| 87 | * 'field2' => 'value', |
||
| 88 | * .... |
||
| 89 | * )); |
||
| 90 | * |
||
| 91 | * @param array|string $values |
||
| 92 | * @param string $value |
||
| 93 | * @return Jam_Model |
||
| 94 | */ |
||
| 95 | 62 | public function set($values, $value = NULL) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Clears the object and loads an array of values into the object. |
||
| 129 | * |
||
| 130 | * This should only be used for setting from database results |
||
| 131 | * since the model declares itself as saved and loaded after. |
||
| 132 | * |
||
| 133 | * @param Jam_Model|array $values |
||
| 134 | * @return Jam_Model |
||
| 135 | */ |
||
| 136 | 150 | public function load_fields($values) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Validates the current model's data |
||
| 191 | * |
||
| 192 | * @throws Jam_Exception_Validation |
||
| 193 | * @param Validation|null $extra_validation |
||
| 194 | * @return Kohana_Jam_Model |
||
| 195 | */ |
||
| 196 | 11 | public function check($force = FALSE) |
|
| 202 | |||
| 203 | 637 | protected function _move_retrieved_to_changed() |
|
| 204 | { |
||
| 205 | 637 | foreach ($this->_retrieved as $column => $value) |
|
| 206 | { |
||
| 207 | 11 | if ($value instanceof Jam_Array_Association AND $value->changed()) |
|
| 208 | 11 | { |
|
| 209 | 5 | $this->_changed[$column] = $value; |
|
| 210 | 5 | } |
|
| 211 | 637 | } |
|
| 212 | 637 | } |
|
| 213 | |||
| 214 | 15 | public function update_fields($values, $value = NULL) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Creates or updates the current record. |
||
| 236 | * |
||
| 237 | * @param bool|null $validate |
||
| 238 | * @return Kohana_Jam_Model |
||
| 239 | */ |
||
| 240 | 53 | public function save($validate = NULL) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Deletes a single record. |
||
| 361 | * |
||
| 362 | * @return boolean |
||
| 363 | **/ |
||
| 364 | 8 | public function delete() |
|
| 365 | { |
||
| 366 | 8 | $result = FALSE; |
|
| 367 | |||
| 368 | // Are we loaded? Then we're just deleting this record |
||
| 369 | 8 | if ($this->_loaded) |
|
| 370 | 8 | { |
|
| 371 | 8 | $key = $this->_original[$this->meta()->primary_key()]; |
|
| 372 | |||
| 373 | 8 | if (($result = $this->meta()->events()->trigger('model.before_delete', $this)) !== FALSE) |
|
| 374 | 8 | { |
|
| 375 | 8 | $result = Jam::delete($this)->where_key($key)->execute(); |
|
| 376 | 8 | } |
|
| 377 | 8 | } |
|
| 378 | |||
| 379 | // Trigger the after-delete |
||
| 380 | 8 | $this->meta()->events()->trigger('model.after_delete', $this, array($result)); |
|
| 381 | |||
| 382 | // Clear the object so it appears deleted anyway |
||
| 383 | 8 | $this->clear(); |
|
| 384 | |||
| 385 | // Set the flag indicatig the model has been successfully deleted |
||
| 386 | 8 | $this->_deleted = $result; |
|
| 387 | |||
| 388 | 8 | return $this->_deleted; |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Removes any changes made to a model. |
||
| 393 | * |
||
| 394 | * This method only works on loaded models. |
||
| 395 | * |
||
| 396 | * @return Jam_Model |
||
| 397 | */ |
||
| 398 | public function revert() |
||
| 399 | { |
||
| 400 | if ($this->_loaded) |
||
| 401 | { |
||
| 402 | $this->_loaded = |
||
| 403 | $this->_saved = TRUE; |
||
| 404 | |||
| 405 | parent::revert(); |
||
| 406 | } |
||
| 407 | |||
| 408 | return $this; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Sets a model to its original state, as if freshly instantiated |
||
| 413 | * |
||
| 414 | * @return Jam_Model |
||
| 415 | */ |
||
| 416 | 150 | public function clear() |
|
| 417 | { |
||
| 418 | 150 | $this->_loaded = |
|
| 419 | 150 | $this->_saved = FALSE; |
|
| 420 | |||
| 421 | 150 | parent::clear(); |
|
| 422 | |||
| 423 | 150 | return $this; |
|
| 424 | } |
||
| 425 | |||
| 426 | 1 | public function get_insist($attribute_name) |
|
| 438 | |||
| 439 | 1 | public function build($association_name, array $attributes = array()) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns whether or not the model is loaded |
||
| 458 | * |
||
| 459 | * @return boolean |
||
| 460 | */ |
||
| 461 | 71 | public function loaded() |
|
| 462 | { |
||
| 463 | 71 | return $this->_loaded; |
|
| 464 | } |
||
| 465 | |||
| 466 | 1 | public function loaded_insist() |
|
| 467 | 1 | { |
|
| 468 | if ( ! $this->loaded()) |
||
| 469 | throw new Jam_Exception_NotLoaded("Model not loaded", $this); |
||
| 470 | |||
| 471 | return $this; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Whether or not the model is saved |
||
| 476 | * |
||
| 477 | * @return boolean |
||
| 478 | */ |
||
| 479 | 10 | public function saved() |
|
| 480 | { |
||
| 481 | 10 | return $this->_saved; |
|
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Whether or not the model is in the process of being saved |
||
| 486 | * |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | 4 | public function is_saving() |
|
| 490 | { |
||
| 491 | 4 | return $this->_is_saving; |
|
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Whether or not the model is deleted |
||
| 496 | * |
||
| 497 | * @return boolean |
||
| 498 | */ |
||
| 499 | public function deleted() |
||
| 500 | { |
||
| 501 | return $this->_deleted; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Build a new model object based on the current one, but without an ID, so it can be saved as a new object |
||
| 506 | * @return Jam_Model |
||
| 507 | */ |
||
| 508 | 1 | public function duplicate() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Returns a string representation of the model in the |
||
| 519 | * form of `Model_Name (id)` or `Model_Name (NULL)` if |
||
| 520 | * the model is not loaded. |
||
| 521 | * |
||
| 522 | * This is designed to be useful for debugging. |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | 2 | public function __toString() |
|
| 527 | { |
||
| 528 | 2 | return (string) get_class($this).'('.($this->loaded() ? $this->id() : 'NULL').')'; |
|
| 529 | } |
||
| 530 | |||
| 531 | 637 | public function serialize() |
|
| 544 | |||
| 545 | 637 | public function unserialize($data) |
|
| 546 | { |
||
| 547 | 637 | $data = unserialize($data); |
|
| 548 | |||
| 549 | 637 | $this->_meta = Jam::meta($this); |
|
| 550 | 637 | $this->_original = Arr::merge($this->meta()->defaults(), $data['original']); |
|
| 551 | 637 | $this->_changed = $data['changed']; |
|
| 552 | 637 | $this->_unmapped = $data['unmapped']; |
|
| 553 | 637 | $this->_saved = $data['saved']; |
|
| 554 | 637 | $this->_loaded = $data['loaded']; |
|
| 555 | 637 | $this->_deleted = $data['deleted']; |
|
| 556 | |||
| 557 | 637 | foreach ($this->_changed as $name => $attribute) |
|
| 566 | } |
||
| 567 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.