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) |
|
45 | { |
||
46 | 384 | parent::__construct($meta_name); |
|
47 | |||
48 | 384 | $this->meta()->events()->trigger('model.before_construct', $this); |
|
49 | |||
50 | // Copy over the defaults into the original data. |
||
51 | 384 | $this->_original = $this->meta()->defaults(); |
|
52 | |||
53 | 384 | $this->meta()->events()->trigger('model.after_construct', $this); |
|
54 | 384 | } |
|
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) |
|
63 | { |
||
64 | 170 | if ($association = $this->meta()->association($name)) |
|
65 | 170 | { |
|
66 | 20 | $name = $association->name; |
|
67 | |||
68 | 20 | return $association->get($this, Arr::get($this->_changed, $name), $this->changed($name)); |
|
69 | } |
||
70 | |||
71 | 169 | 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 | 63 | 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 | 157 | 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 | 644 | protected function _move_retrieved_to_changed() |
|
204 | { |
||
205 | 644 | 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 | 644 | } |
|
212 | 644 | } |
|
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 | 54 | public function save($validate = NULL) |
|
358 | |||
359 | /** |
||
360 | * Deletes a single record. |
||
361 | * |
||
362 | * @return boolean |
||
363 | **/ |
||
364 | 10 | public function delete() |
|
365 | { |
||
366 | 10 | $result = FALSE; |
|
367 | |||
368 | // Are we loaded? Then we're just deleting this record |
||
369 | 10 | if ($this->_loaded) |
|
370 | 10 | { |
|
371 | 10 | $key = $this->_original[$this->meta()->primary_key()]; |
|
372 | |||
373 | 10 | if (($result = $this->meta()->events()->trigger('model.before_delete', $this)) !== FALSE) |
|
374 | 10 | { |
|
375 | 10 | $result = Jam::delete($this)->where_key($key)->execute(); |
|
376 | 10 | } |
|
377 | 10 | } |
|
378 | |||
379 | // Trigger the after-delete |
||
380 | 10 | $this->meta()->events()->trigger('model.after_delete', $this, array($result)); |
|
381 | |||
382 | // Clear the object so it appears deleted anyway |
||
383 | 10 | $this->clear(); |
|
384 | |||
385 | // Set the flag indicatig the model has been successfully deleted |
||
386 | 10 | $this->_deleted = $result; |
|
387 | |||
388 | 10 | 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 | 157 | public function clear() |
|
417 | { |
||
418 | 157 | $this->_loaded = |
|
419 | 157 | $this->_saved = FALSE; |
|
420 | |||
421 | 157 | parent::clear(); |
|
422 | |||
423 | 157 | 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 | 73 | public function loaded() |
|
462 | { |
||
463 | 73 | 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 | 644 | public function serialize() |
|
544 | |||
545 | 644 | public function unserialize($data) |
|
546 | { |
||
547 | 644 | $data = unserialize($data); |
|
548 | |||
549 | 644 | $this->_meta = Jam::meta($this); |
|
550 | 644 | $this->_original = Arr::merge($this->meta()->defaults(), $data['original']); |
|
551 | 644 | $this->_changed = $data['changed']; |
|
552 | 644 | $this->_unmapped = $data['unmapped']; |
|
553 | 644 | $this->_saved = $data['saved']; |
|
554 | 644 | $this->_loaded = $data['loaded']; |
|
555 | 644 | $this->_deleted = $data['deleted']; |
|
556 | |||
557 | 644 | 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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.