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 | 23 | public function initialize(Jam_Meta $meta, $name) |
|
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 | 6 | public function get(Jam_Validated $model, $value, $is_changed) |
|
116 | { |
||
117 | 6 | if ($is_changed) |
|
118 | { |
||
119 | 4 | if ($value instanceof Jam_Validated OR ! $value) |
|
120 | 3 | return $value; |
|
121 | |||
122 | 2 | $key = Jam_Association::primary_key($this->foreign_model, $value); |
|
123 | |||
124 | 2 | if ($key) |
|
125 | { |
||
126 | 2 | $item = $this->_find_item($this->foreign_model, $key); |
|
127 | } |
||
128 | elseif (is_array($value)) |
||
129 | { |
||
130 | $item = Jam::build($this->foreign_model, $value); |
||
131 | } |
||
132 | else |
||
133 | { |
||
134 | $item = NULL; |
||
135 | } |
||
136 | |||
137 | 2 | if ($item AND is_array($value)) |
|
138 | { |
||
139 | 2 | $item->set($value); |
|
140 | } |
||
141 | } |
||
142 | else |
||
143 | { |
||
144 | 4 | $item = $this->_find_item($this->foreign_model, $model); |
|
145 | } |
||
146 | |||
147 | 4 | return $this->set($model, $item, $is_changed); |
|
148 | } |
||
149 | |||
150 | 7 | 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) |
|
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 | 3 | public function model_before_delete(Jam_Model $model) |
|
259 | { |
||
260 | 3 | if (Jam_Association::DELETE === $this->dependent) |
|
261 | { |
||
262 | 1 | if ($model->{$this->name}) |
|
263 | { |
||
264 | 1 | $model->{$this->name}->delete(); |
|
265 | } |
||
266 | } |
||
267 | 2 | elseif (Jam_Association::ERASE === $this->dependent) |
|
268 | { |
||
269 | $this->query_builder('delete', $model)->execute(); |
||
270 | } |
||
271 | 2 | elseif (Jam_Association::NULLIFY === $this->dependent) |
|
272 | { |
||
273 | $this->update_query($model, NULL, NULL)->execute(); |
||
274 | } |
||
275 | 3 | } |
|
276 | |||
277 | /** |
||
278 | * See if the association is polymorphic |
||
279 | * @return boolean |
||
280 | */ |
||
281 | 24 | public function is_polymorphic() |
|
285 | |||
286 | 3 | 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.