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) |
|
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) |
|
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) |
|
116 | { |
||
117 | if ($is_changed) |
||
118 | 7 | { |
|
119 | 5 | if ($value instanceof Jam_Validated OR ! $value) |
|
120 | 5 | return $value; |
|
121 | |||
122 | 3 | $key = Jam_Association::primary_key($this->foreign_model, $value); |
|
123 | |||
124 | if ($key) |
||
125 | 3 | { |
|
126 | 2 | $item = $this->_find_item($this->foreign_model, $key); |
|
127 | 2 | } |
|
128 | 1 | elseif (is_array($value)) |
|
129 | { |
||
130 | 1 | $item = Jam::build($this->foreign_model, $value); |
|
131 | 1 | } |
|
132 | else |
||
133 | { |
||
134 | $item = NULL; |
||
135 | } |
||
136 | |||
137 | 3 | if ($item AND is_array($value)) |
|
138 | 3 | { |
|
139 | 3 | $item->set($value); |
|
140 | 3 | } |
|
141 | 3 | } |
|
142 | else |
||
143 | { |
||
144 | 5 | $item = $this->_find_item($this->foreign_model, $model); |
|
145 | } |
||
146 | |||
147 | 5 | return $this->set($model, $item, $is_changed); |
|
148 | } |
||
149 | |||
150 | 8 | public function set(Jam_Validated $model, $value, $is_changed) |
|
151 | { |
||
152 | 8 | if ($value instanceof Jam_Model) |
|
153 | 8 | { |
|
154 | 5 | if ($this->is_polymorphic()) |
|
155 | 5 | { |
|
156 | 2 | $value->{$this->polymorphic_key} = $model->meta()->model(); |
|
157 | 2 | } |
|
158 | |||
159 | 5 | if ($model->loaded()) |
|
160 | 5 | { |
|
161 | 4 | $value->{$this->foreign_key} = $model->id(); |
|
162 | 4 | } |
|
163 | |||
164 | 5 | if ($this->as) |
|
165 | 5 | { |
|
166 | 2 | $value->retrieved($this->as, $model); |
|
167 | 2 | } |
|
168 | |||
169 | 5 | if ($this->inverse_of) |
|
170 | 5 | { |
|
171 | 3 | $value->retrieved($this->inverse_of, $model); |
|
172 | 3 | } |
|
173 | 5 | } |
|
174 | |||
175 | 8 | return $value; |
|
176 | } |
||
177 | |||
178 | 1 | public function build(Jam_Validated $model, array $attributes = NULL) |
|
179 | { |
||
180 | 1 | $item = Jam::build($this->foreign_model, $attributes); |
|
181 | |||
182 | 1 | $this->set($model, $item, TRUE); |
|
183 | |||
184 | 1 | return $item; |
|
185 | } |
||
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) |
|
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) |
|
276 | |||
277 | /** |
||
278 | * See if the association is polymorphic |
||
279 | * @return boolean |
||
280 | */ |
||
281 | 24 | public function is_polymorphic() |
|
285 | |||
286 | 4 | protected function _find_item($foreign_model, $key) |
|
308 | |||
309 | 11 | public function query_builder($type, Jam_Model $model) |
|
321 | |||
322 | 9 | public function update_query(Jam_Model $model, $new_id, $new_model) |
|
334 | } |
||
335 |
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.