Complex classes like Kohana_Jam_Association_Hasmany 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_Hasmany, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 12 | abstract class Kohana_Jam_Association_Hasmany extends Jam_Association_Collection { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Set this for polymorphic associations, this has to be the name of the opposite belongsto relation, |
||
| 16 | * so if the oposite relation was item->parent, then this will have to be 'items' => Jam::association('hasmany', array('as' => 'parent') |
||
| 17 | * If this option is set then the foreign_key default becomes "{$as}_id", and polymorphic_key to "{$as}_model" |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $as; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The field in the opposite model that is used to linking to this one. |
||
| 24 | * It defaults to "{$foreign_model}_id", but you can custumize it |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $foreign_key = NULL; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The field in the opposite model that is used by the polymorphic association to determine the model |
||
| 31 | * It defaults to "{$as}_model" |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $polymorphic_key = NULL; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * You can set this to the name of the opposite belongsto relation for optimization purposes |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $inverse_of = NULL; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Optionally delete the item when it is removed from the association |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $delete_on_remove = NULL; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Initialize foreign_key, as, and polymorphic_key with default values |
||
| 50 | * |
||
| 51 | * @param string $model |
||
|
|
|||
| 52 | * @param string $name |
||
| 53 | */ |
||
| 54 | 39 | public function initialize(Jam_Meta $meta, $name) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Return a Jam_Query_Builder_Join object to allow a query to join with this association |
||
| 76 | * @param string $alias table name alias |
||
| 77 | * @param string $type join type (LEFT, NATURAL) |
||
| 78 | * @return Jam_Query_Builder_Join |
||
| 79 | */ |
||
| 80 | 9 | public function join($alias, $type = NULL) |
|
| 93 | |||
| 94 | 10 | public function collection(Jam_Model $model) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Assign inverse associations to elements of arrays |
||
| 110 | * @param Jam_Validated $model |
||
| 111 | * @param mixed $value |
||
| 112 | * @param boolean $is_changed |
||
| 113 | */ |
||
| 114 | 2 | public function set(Jam_Validated $model, $value, $is_changed) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Before the model is deleted, and the depenedent option is set, remove the dependent models |
||
| 132 | * @param Jam_Model $model |
||
| 133 | */ |
||
| 134 | 5 | public function model_before_delete(Jam_Model $model) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Remove items from this association (withought deleteing it) and persist the data in the database |
||
| 161 | * @param Jam_Validated $model |
||
| 162 | * @param Jam_Array_Association $collection |
||
| 163 | */ |
||
| 164 | 1 | public function clear(Jam_Validated $model, Jam_Array_Association $collection) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Generate a query to delete associated models in the database |
||
| 176 | * @param Jam_Model $model |
||
| 177 | * @return Database_Query |
||
| 178 | */ |
||
| 179 | 6 | public function erase_query(Jam_Model $model) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Generate a query to remove models from this association (without deleting them) |
||
| 199 | * @param Jam_Model $model |
||
| 200 | * @return Database_Query |
||
| 201 | */ |
||
| 202 | 5 | public function nullify_query(Jam_Model $model) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Generate a query to remove models from the association (without deleting them), for specific ids |
||
| 224 | * @param Jam_Model $model |
||
| 225 | * @param array $ids |
||
| 226 | * @return Database_Query |
||
| 227 | */ |
||
| 228 | public function remove_items_query(Jam_Model $model, array $ids) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Generate a query to add models from the association (without deleting them), for specific ids |
||
| 265 | * @param Jam_Model $model |
||
| 266 | * @param array $ids |
||
| 267 | * @return Database_Query |
||
| 268 | */ |
||
| 269 | 5 | public function add_items_query(Jam_Model $model, array $ids) |
|
| 281 | |||
| 282 | |||
| 283 | 8 | protected function assign_item(Jam_Model $item, $foreign_key, $polymorphic_key, $inverse_of) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Set the foreign and polymorphic keys on an item when its requested from the associated collection |
||
| 306 | * |
||
| 307 | * @param Jam_Model $model |
||
| 308 | * @param Jam_Model $item |
||
| 309 | */ |
||
| 310 | 7 | public function item_get(Jam_Model $model, Jam_Model $item) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Set the foreign and polymorphic keys on an item when its set to the associated collection |
||
| 317 | * |
||
| 318 | * @param Jam_Model $model |
||
| 319 | * @param Jam_Model $item |
||
| 320 | */ |
||
| 321 | 3 | public function item_set(Jam_Model $model, Jam_Model $item) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Unset the foreign and polymorphic keys on an item when its removed from the associated collection |
||
| 328 | * |
||
| 329 | * @param Jam_Model $model |
||
| 330 | * @param Jam_Model $item |
||
| 331 | */ |
||
| 332 | public function item_unset(Jam_Model $model, Jam_Model $item) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * See if the association is polymorphic |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | 43 | public function is_polymorphic() |
|
| 345 | } // End Kohana_Jam_Association_HasMany |
||
| 346 |
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.