Complex classes like Kohana_Jam_Association_Hasone 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_Association_Hasone, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 11 | abstract class Kohana_Jam_Association_Hasone extends Jam_Association { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Set this for polymorphic association, this has to be the name of the opposite belongsto relation, |
||
| 15 | * so if the oposite relation was item->parent, then this will have to be 'ites' => Jam::association('hasone, array('as' => 'parent') |
||
| 16 | * If this option is set then the foreign_key default becomes "{$as}_id", and polymorphic_key to "{$as}_model" |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $as = NULL; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The foreign key |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $foreign_key = NULL; |
||
| 26 | |||
| 27 | public $inverse_of = NULL; |
||
| 28 | |||
| 29 | public $polymorphic_key = NULL; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Automatically sets foreign to sensible defaults. |
||
| 33 | * |
||
| 34 | * @param string $model |
||
|
|
|||
| 35 | * @param string $name |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | 18 | public function initialize(Jam_Meta $meta, $name) |
|
| 39 | { |
||
| 40 | 18 | parent::initialize($meta, $name); |
|
| 41 | |||
| 42 | 18 | if ( ! $this->foreign_model) |
|
| 43 | 18 | { |
|
| 44 | 15 | $this->foreign_model = $name; |
|
| 45 | 15 | } |
|
| 46 | |||
| 47 | 18 | if ( ! $this->foreign_key) |
|
| 48 | 18 | { |
|
| 49 | 17 | $this->foreign_key = $this->model.'_id'; |
|
| 50 | 17 | } |
|
| 51 | |||
| 52 | // Polymorphic associations |
||
| 53 | 18 | if ($this->as) |
|
| 54 | 18 | { |
|
| 55 | 7 | $this->foreign_key = $this->as.'_id'; |
|
| 56 | 7 | if ( ! $this->polymorphic_key) |
|
| 57 | 7 | { |
|
| 58 | 7 | $this->polymorphic_key = $this->as.'_model'; |
|
| 59 | 7 | } |
|
| 60 | 7 | } |
|
| 61 | 18 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Load associated model (from database or after deserialization) |
||
| 65 | * @param Jam_Validated $model |
||
| 66 | * @param mixed $value |
||
| 67 | * @return Jam_Model |
||
| 68 | */ |
||
| 69 | 1 | public function load_fields(Jam_Validated $model, $value) |
|
| 70 | { |
||
| 71 | 1 | if (is_array($value)) |
|
| 72 | 1 | { |
|
| 73 | 1 | $value = Jam::build($this->foreign_model)->load_fields($value); |
|
| 74 | 1 | } |
|
| 75 | |||
| 76 | 1 | if ($value instanceof Jam_Model AND $this->inverse_of) |
|
| 77 | 1 | { |
|
| 78 | $value->retrieved($this->inverse_of, $model); |
||
| 79 | } |
||
| 80 | |||
| 81 | 1 | return $value; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return a Jam_Query_Builder_Join object to allow a query to join with this association |
||
| 86 | * |
||
| 87 | * @param string $alias table name alias |
||
| 88 | * @param string $type join type (LEFT, NATURAL) |
||
| 89 | * @return Jam_Query_Builder_Join |
||
| 90 | */ |
||
| 91 | 5 | public function join($alias, $type = NULL) |
|
| 92 | { |
||
| 93 | 5 | $join = Jam_Query_Builder_Join::factory($alias ? array($this->foreign_model, $alias) : $this->foreign_model, $type) |
|
| 94 | 5 | ->context_model($this->model) |
|
| 95 | 5 | ->on($this->foreign_key, '=', ':primary_key'); |
|
| 96 | |||
| 97 | 5 | if ($this->is_polymorphic()) |
|
| 98 | 5 | { |
|
| 99 | 2 | $join->on($this->polymorphic_key, '=', DB::expr(':model', array(':model' => $this->model))); |
|
| 100 | 2 | } |
|
| 101 | |||
| 102 | 5 | return $join; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the belonging model for this association using the foreign key, |
||
| 107 | * if the data was changed, use the key from the changed data. |
||
| 108 | * Assign inverse_of |
||
| 109 | * |
||
| 110 | * @param Jam_Validated $model |
||
| 111 | * @param mixed $value changed data |
||
| 112 | * @param boolean $is_changed |
||
| 113 | * @return Jam_Model |
||
| 114 | */ |
||
| 115 | 7 | public function get(Jam_Validated $model, $value, $is_changed) |
|
| 149 | |||
| 150 | 8 | public function set(Jam_Validated $model, $value, $is_changed) |
|
| 177 | |||
| 178 | 1 | public function build(Jam_Validated $model, array $attributes = NULL) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Perform validation on the belonging model, if it was changed. |
||
| 189 | * @param Jam_Model $model |
||
| 190 | * @param Jam_Event_Data $data |
||
| 191 | * @param array $changed |
||
| 192 | */ |
||
| 193 | 8 | public function model_after_check(Jam_Model $model, Jam_Event_Data $data, $changed) |
|
| 194 | { |
||
| 195 | 8 | if (($value = Arr::get($changed, $this->name)) AND Jam_Association::is_changed($value)) |
|
| 196 | 8 | { |
|
| 197 | 3 | if ( ! $model->{$this->name}->is_validating() AND ! $model->{$this->name}->check()) |
|
| 198 | 3 | { |
|
| 199 | $model->errors()->add($this->name, 'association', array(':errors' => $model->{$this->name}->errors())); |
||
| 200 | } |
||
| 201 | 3 | } |
|
| 202 | 8 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Save the related model after the main model, if it was changed |
||
| 206 | * Only save related model if it has been changed, and is not in a process of saving itself |
||
| 207 | * |
||
| 208 | * @param Jam_Model $model |
||
| 209 | * @param Jam_Event_Data $data |
||
| 210 | * @param boolean $changed |
||
| 211 | */ |
||
| 212 | 7 | public function model_after_save(Jam_Model $model, Jam_Event_Data $data, $changed) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Delete related model if it has been assigned as dependent |
||
| 254 | * If dependent is Jam_Association::DELETE - execute the delete method (and all events) |
||
| 255 | * IF dependent is Jam_Association::ERASE - simply remove from database without executing any events (faster) |
||
| 256 | * @param Jam_Model $model |
||
| 257 | */ |
||
| 258 | 4 | public function model_before_delete(Jam_Model $model) |
|
| 259 | { |
||
| 260 | 4 | switch ($this->dependent) |
|
| 261 | { |
||
| 262 | 4 | case Jam_Association::DELETE: |
|
| 263 | 1 | if ($model->{$this->name}) |
|
| 264 | 1 | { |
|
| 265 | $model->{$this->name}->delete(); |
||
| 266 | } |
||
| 267 | 1 | break; |
|
| 268 | |||
| 269 | 3 | case Jam_Association::ERASE: |
|
| 270 | 1 | $this->query_builder('delete', $model)->execute(); |
|
| 271 | 1 | break; |
|
| 272 | |||
| 273 | 2 | case Jam_Association::NULLIFY: |
|
| 274 | $this->update_query($model, NULL, NULL)->execute(); |
||
| 275 | break; |
||
| 276 | 4 | } |
|
| 277 | 4 | } |
|
| 278 | |||
| 279 | /** |
||
| 280 | * See if the association is polymorphic |
||
| 281 | * @return boolean |
||
| 282 | */ |
||
| 283 | 24 | public function is_polymorphic() |
|
| 284 | { |
||
| 285 | 24 | return (bool) $this->as; |
|
| 286 | } |
||
| 287 | |||
| 288 | 4 | protected function _find_item($foreign_model, $key) |
|
| 310 | |||
| 311 | 11 | public function query_builder($type, Jam_Model $model) |
|
| 312 | { |
||
| 313 | 11 | $query = call_user_func("Jam::{$type}", $this->foreign_model) |
|
| 314 | 11 | ->where($this->foreign_key, '=', $model->id()); |
|
| 315 | |||
| 316 | 11 | if ($this->is_polymorphic()) |
|
| 323 | |||
| 324 | 9 | public function update_query(Jam_Model $model, $new_id, $new_model) |
|
| 336 | } |
||
| 337 |
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.