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 | 384 | public function __construct($meta_name = NULL) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Gets the value for a field. |
||
| 58 | * |
||
| 59 | * @param string $name The field's name |
||
| 60 | * @return array|mixed |
||
| 61 | */ |
||
| 62 | 170 | public function get($name) |
|
| 73 | |||
| 74 | public function __isset($name) |
||
| 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 $this |
||
| 94 | */ |
||
| 95 | 63 | public function set($values, $value = NULL) |
|
| 96 | { |
||
| 97 | // Accept set('name', 'value'); |
||
| 98 | 63 | if ( ! is_array($values)) |
|
| 99 | 63 | { |
|
| 100 | 51 | $values = array($values => $value); |
|
| 101 | 51 | } |
|
| 102 | |||
| 103 | 63 | foreach ($values as $key => & $value) |
|
| 104 | { |
||
| 105 | 63 | if ($association = $this->meta()->association($key)) |
|
| 106 | 63 | { |
|
| 107 | 20 | if ($association->readonly) |
|
| 108 | 20 | throw new Kohana_Exception('Cannot change the value of :name, its readonly', array(':name' => $association->name)); |
|
| 109 | |||
| 110 | 20 | $this->_changed[$association->name] = $association->set($this, $value, TRUE); |
|
| 111 | |||
| 112 | 20 | unset($this->_retrieved[$association->name]); |
|
| 113 | 20 | unset($values[$key]); |
|
| 114 | 20 | } |
|
| 115 | 63 | } |
|
| 116 | |||
| 117 | 63 | parent::set($values); |
|
| 118 | |||
| 119 | 63 | if ($this->_saved AND $this->changed()) |
|
| 120 | 63 | { |
|
| 121 | 18 | $this->_saved = FALSE; |
|
| 122 | 18 | } |
|
| 123 | |||
| 124 | 63 | return $this; |
|
| 125 | } |
||
| 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 $this |
||
| 135 | */ |
||
| 136 | 157 | public function load_fields($values) |
|
| 137 | { |
||
| 138 | // Clear the object |
||
| 139 | 157 | $this->clear(); |
|
| 140 | |||
| 141 | 157 | $this->meta()->events()->trigger('model.before_load', $this); |
|
| 142 | |||
| 143 | 157 | $this->_loaded = TRUE; |
|
| 144 | |||
| 145 | 157 | $columns = array(); |
|
| 146 | |||
| 147 | 157 | foreach ($this->meta()->fields() as $key => $field) |
|
| 148 | { |
||
| 149 | 157 | $columns[$field->column] = $field; |
|
| 150 | 157 | } |
|
| 151 | |||
| 152 | 157 | foreach ($values as $key => $value) |
|
| 153 | { |
||
| 154 | 154 | if ($field = $this->meta()->field($key)) |
|
| 155 | 154 | { |
|
| 156 | 154 | $this->_original[$field->name] = $field->set($this, $value, FALSE); |
|
| 157 | 154 | } |
|
| 158 | 23 | elseif (isset($columns[$key])) |
|
| 159 | { |
||
| 160 | $field = $columns[$key]; |
||
| 161 | 1 | $this->_original[$field->name] = $field->set($this, $value, FALSE); |
|
| 162 | } |
||
| 163 | 23 | elseif ($association = $this->meta()->association($key)) |
|
| 164 | { |
||
| 165 | 8 | $association_value = $association->load_fields($this, $value, FALSE); |
|
| 166 | 8 | if (is_object($association_value)) |
|
| 167 | 8 | { |
|
| 168 | 8 | $this->_retrieved[$association->name] = $association->load_fields($this, $value, FALSE); |
|
| 169 | 8 | } |
|
| 170 | else |
||
| 171 | { |
||
| 172 | 1 | $this->_changed[$association->name] = $association_value; |
|
| 173 | } |
||
| 174 | 8 | } |
|
| 175 | else |
||
| 176 | { |
||
| 177 | 15 | $this->_unmapped[$key] = $value; |
|
| 178 | } |
||
| 179 | 157 | } |
|
| 180 | |||
| 181 | // Model is now saved and loaded |
||
| 182 | 157 | $this->_saved = TRUE; |
|
| 183 | |||
| 184 | 157 | $this->meta()->events()->trigger('model.after_load', $this); |
|
| 185 | |||
| 186 | 157 | return $this; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Validates the current model's data |
||
| 191 | * |
||
| 192 | * @throws Jam_Exception_Validation |
||
| 193 | * @param Validation|null $extra_validation |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 11 | public function check($force = FALSE) |
|
| 197 | { |
||
| 198 | 11 | $this->_move_retrieved_to_changed(); |
|
| 199 | |||
| 200 | 11 | return parent::check( ! $this->loaded() OR $force); |
|
| 201 | } |
||
| 202 | |||
| 203 | 644 | protected function _move_retrieved_to_changed() |
|
| 213 | |||
| 214 | 15 | public function update_fields($values, $value = NULL) |
|
| 215 | { |
||
| 216 | 15 | if ( ! $this->loaded()) |
|
| 217 | throw new Kohana_Exception('Model must be loaded to use update_fields method'); |
||
| 218 | |||
| 219 | if ( ! is_array($values)) |
||
| 220 | { |
||
| 221 | $values = array($values => $value); |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->set($values); |
||
| 225 | |||
| 226 | Jam::update($this) |
||
| 227 | ->where_key($this->id()) |
||
| 228 | ->set($values) |
||
| 229 | ->execute(); |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Creates or updates the current record. |
||
| 236 | * |
||
| 237 | * @param bool|null $validate |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | 54 | public function save($validate = NULL) |
|
| 241 | { |
||
| 242 | 10 | if ($this->_is_saving) |
|
| 243 | 10 | throw new Kohana_Exception("Cannot save a model that is already in the process of saving"); |
|
| 244 | |||
| 245 | |||
| 246 | 10 | $key = $this->_original[$this->meta()->primary_key()]; |
|
| 247 | |||
| 248 | // Run validation |
||
| 249 | 10 | if ($validate !== FALSE) |
|
| 250 | 10 | { |
|
| 251 | 10 | $this->check_insist(); |
|
| 252 | 10 | } |
|
| 253 | |||
| 254 | 10 | $this->_is_saving = TRUE; |
|
| 255 | |||
| 256 | // These will be processed later |
||
| 257 | 10 | $values = $defaults = array(); |
|
| 258 | |||
| 259 | 10 | if ($this->meta()->events()->trigger('model.before_save', $this, array($this->_changed)) === FALSE) |
|
| 260 | 10 | { |
|
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | // Trigger callbacks and ensure we should proceed |
||
| 265 | 10 | $event_type = $key ? 'update' : 'create'; |
|
| 266 | |||
| 267 | 10 | if ($this->meta()->events()->trigger('model.before_'.$event_type, $this, array($this->_changed)) === FALSE) |
|
| 268 | 10 | { |
|
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | 10 | $this->_move_retrieved_to_changed(); |
|
| 273 | |||
| 274 | // Iterate through all fields in original in case any unchanged fields |
||
| 275 | // have convert() behavior like timestamp updating... |
||
| 276 | // |
||
| 277 | 10 | foreach (array_merge($this->_original, $this->_changed) as $column => $value) |
|
| 278 | { |
||
| 279 | 10 | if ($field = $this->meta()->field($column)) |
|
| 280 | 10 | { |
|
| 281 | // Only save in_db values |
||
| 282 | 10 | if ($field->in_db) |
|
| 283 | 10 | { |
|
| 284 | // See if field wants to alter the value on save() |
||
| 285 | 10 | $value = $field->convert($this, $value, $key); |
|
| 286 | |||
| 287 | // Only set the value to be saved if it's changed from the original |
||
| 288 | 10 | if ($value !== $this->_original[$column]) |
|
| 289 | 10 | { |
|
| 290 | 7 | $values[$field->column] = $value; |
|
| 291 | 54 | } |
|
| 292 | // Or if we're INSERTing and we need to set the defaults for the first time |
||
| 293 | 10 | elseif ( ! $key AND ( ! $this->changed($field->name) OR $field->default === $value) AND ! $field->primary) |
|
| 294 | { |
||
| 295 | 8 | $defaults[$field->column] = $field->default; |
|
| 296 | 8 | } |
|
| 297 | 10 | } |
|
| 298 | 10 | } |
|
| 299 | 10 | } |
|
| 300 | |||
| 301 | // If we have a key, we're updating |
||
| 302 | if ($key) |
||
| 303 | 10 | { |
|
| 304 | // Do we even have to update anything in the row? |
||
| 305 | if ($values) |
||
| 306 | 4 | { |
|
| 307 | 3 | Jam::update($this) |
|
| 308 | 3 | ->where_key($key) |
|
| 309 | 3 | ->set($values) |
|
| 310 | 3 | ->execute(); |
|
| 311 | 3 | } |
|
| 312 | 4 | } |
|
| 313 | else |
||
| 314 | { |
||
| 315 | 8 | $insert_values = array_merge($defaults, $values); |
|
| 316 | 8 | list($id) = Jam::insert($this) |
|
| 317 | 8 | ->columns(array_keys($insert_values)) |
|
| 318 | 8 | ->values(array_values($insert_values)) |
|
| 319 | 8 | ->execute(); |
|
| 320 | |||
| 321 | // Gotta make sure to set this |
||
| 322 | 8 | $key = $values[$this->meta()->primary_key()] = $id; |
|
| 323 | } |
||
| 324 | |||
| 325 | // Re-set any saved values; they may have changed |
||
| 326 | 10 | $this->set($values); |
|
| 327 | |||
| 328 | 10 | $this->_loaded = $this->_saved = TRUE; |
|
| 329 | |||
| 330 | 10 | $this->meta()->events()->trigger('model.after_save', $this, array($this->_changed, $event_type)); |
|
| 331 | |||
| 332 | 10 | $this->meta()->events()->trigger('model.after_'.$event_type, $this, array($this->_changed)); |
|
| 333 | |||
| 334 | // Set the changed data back as original |
||
| 335 | 10 | $this->_original = array_merge($this->_original, $this->_changed); |
|
| 336 | |||
| 337 | 10 | $this->_changed = array(); |
|
| 338 | |||
| 339 | 10 | foreach ($this->_retrieved as $name => $retrieved) |
|
| 340 | { |
||
| 341 | 7 | if (($association = $this->meta()->association($name))) |
|
| 342 | 7 | { |
|
| 343 | 2 | if ($association instanceof Jam_Association_Collection) |
|
| 344 | 2 | { |
|
| 345 | 1 | $retrieved->clear_changed(); |
|
| 346 | 1 | } |
|
| 347 | 2 | } |
|
| 348 | 7 | elseif (($field = $this->meta()->field($name)) AND $field->in_db) |
|
| 349 | { |
||
| 350 | 7 | unset($this->_retrieved[$name]); |
|
| 351 | 7 | } |
|
| 352 | 10 | } |
|
| 353 | |||
| 354 | 10 | $this->_is_saving = FALSE; |
|
| 355 | |||
| 356 | 10 | return $this; |
|
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Deletes a single record. |
||
| 361 | * |
||
| 362 | * @return boolean |
||
| 363 | **/ |
||
| 364 | 10 | public function delete() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Removes any changes made to a model. |
||
| 393 | * |
||
| 394 | * This method only works on loaded models. |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function revert() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Sets a model to its original state, as if freshly instantiated |
||
| 413 | * |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | 157 | public function clear() |
|
| 425 | |||
| 426 | 1 | public function get_insist($attribute_name) |
|
| 427 | { |
||
| 428 | 1 | $attribute = $this->__get($attribute_name); |
|
| 429 | |||
| 430 | 1 | if ($attribute === NULL) |
|
| 431 | 1 | throw new Jam_Exception_Notfound('The association :name was empty on :model_name', NULL, array( |
|
| 432 | 1 | ':name' => $attribute_name, |
|
| 433 | 1 | ':model_name' => (string) $this, |
|
| 434 | 1 | )); |
|
| 435 | |||
| 436 | 1 | return $attribute; |
|
| 437 | } |
||
| 438 | |||
| 439 | 1 | public function build($association_name, array $attributes = array()) |
|
| 440 | { |
||
| 441 | 1 | $association = $this->meta()->association($association_name); |
|
| 442 | |||
| 443 | 1 | if ( ! $association) |
|
| 444 | 1 | throw new Kohana_Exception('There is no association named :association_name on model :model', array(':association_name' => $association_name, ':model' => $this->meta()->model())); |
|
| 445 | |||
| 446 | 1 | if ($association instanceof Jam_Association_Collection) |
|
| 447 | 1 | throw new Kohana_Exception(':association_name association must not be a collection on model :model', array(':association_name' => $association_name, ':model' => $this->meta()->model())); |
|
| 448 | |||
| 449 | 1 | $item = $association->build($this, $attributes); |
|
| 450 | |||
| 451 | 1 | $this->set($association_name, $item); |
|
| 452 | |||
| 453 | 1 | return $item; |
|
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns whether or not the model is loaded |
||
| 458 | * |
||
| 459 | * @return boolean |
||
| 460 | */ |
||
| 461 | 73 | public function loaded() |
|
| 465 | |||
| 466 | public function loaded_insist() |
||
| 467 | { |
||
| 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() |
|
| 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() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Whether or not the model is deleted |
||
| 496 | * |
||
| 497 | * @return boolean |
||
| 498 | */ |
||
| 499 | public function deleted() |
||
| 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() |
|
| 509 | { |
||
| 510 | 1 | $fields = $this->as_array(); |
|
| 511 | |||
| 512 | 1 | unset($fields[$this->meta()->primary_key()]); |
|
| 513 | |||
| 514 | 1 | return Jam::build($this->meta()->model(), $fields); |
|
| 515 | } |
||
| 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() |
|
| 530 | |||
| 531 | 644 | public function serialize() |
|
| 532 | { |
||
| 533 | 644 | $this->_move_retrieved_to_changed(); |
|
| 534 | |||
| 535 | 644 | return serialize(array( |
|
| 536 | 644 | 'original' => $this->_original, |
|
| 537 | 644 | 'changed' => $this->_changed, |
|
| 538 | 644 | 'unmapped' => $this->_unmapped, |
|
| 539 | 644 | 'saved' => $this->_saved, |
|
| 540 | 644 | 'loaded' => $this->_loaded, |
|
| 541 | 644 | 'deleted' => $this->_deleted, |
|
| 542 | 644 | )); |
|
| 543 | } |
||
| 544 | |||
| 545 | 644 | public function unserialize($data) |
|
| 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.