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) |
|
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() |
|
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.