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 | 51 | 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 | 9 | 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 | 1 | 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 | 6 | public function model_before_delete(Jam_Model $model) |
|
| 135 | { |
||
| 136 | 6 | if (Jam_Association::DELETE === $this->dependent) |
|
| 137 | { |
||
| 138 | 2 | foreach ($model->{$this->name} as $item) |
|
| 139 | { |
||
| 140 | 2 | $item->delete(); |
|
| 141 | } |
||
| 142 | } |
||
| 143 | 6 | elseif (Jam_Association::ERASE === $this->dependent) |
|
| 144 | { |
||
| 145 | $query = $this->erase_query($model); |
||
| 146 | if ($query) |
||
| 147 | { |
||
| 148 | $query->execute(); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | 6 | elseif (Jam_Association::NULLIFY === $this->dependent) |
|
| 152 | { |
||
| 153 | $query = $this->nullify_query($model); |
||
| 154 | if ($query) |
||
| 155 | { |
||
| 156 | $query->execute(); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | 6 | } |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Remove items from this association (withought deleteing it) and persist the data in the database |
||
| 163 | * @param Jam_Validated $model |
||
| 164 | * @param Jam_Array_Association $collection |
||
| 165 | */ |
||
| 166 | 1 | public function clear(Jam_Validated $model, Jam_Array_Association $collection) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Generate a query to delete associated models in the database |
||
| 178 | * @param Jam_Model $model |
||
| 179 | * @return Database_Query |
||
| 180 | */ |
||
| 181 | 5 | public function erase_query(Jam_Model $model) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Generate a query to remove models from this association (without deleting them) |
||
| 201 | * @param Jam_Model $model |
||
| 202 | * @return Database_Query |
||
| 203 | */ |
||
| 204 | 5 | public function nullify_query(Jam_Model $model) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Generate a query to remove models from the association (without deleting them), for specific ids |
||
| 226 | * @param Jam_Model $model |
||
| 227 | * @param array $ids |
||
| 228 | * @return Database_Query |
||
| 229 | */ |
||
| 230 | 7 | public function remove_items_query(Jam_Model $model, array $ids) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Generate a query to add models from the association (without deleting them), for specific ids |
||
| 267 | * @param Jam_Model $model |
||
| 268 | * @param array $ids |
||
| 269 | * @return Database_Query |
||
| 270 | */ |
||
| 271 | 4 | public function add_items_query(Jam_Model $model, array $ids) |
|
| 283 | |||
| 284 | |||
| 285 | 7 | protected function assign_item(Jam_Model $item, $foreign_key, $polymorphic_key, $inverse_of) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Set the foreign and polymorphic keys on an item when its requested from the associated collection |
||
| 308 | * |
||
| 309 | * @param Jam_Model $model |
||
| 310 | * @param Jam_Model $item |
||
| 311 | */ |
||
| 312 | 6 | public function item_get(Jam_Model $model, Jam_Model $item) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Set the foreign and polymorphic keys on an item when its set to the associated collection |
||
| 319 | * |
||
| 320 | * @param Jam_Model $model |
||
| 321 | * @param Jam_Model $item |
||
| 322 | */ |
||
| 323 | 3 | public function item_set(Jam_Model $model, Jam_Model $item) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Unset the foreign and polymorphic keys on an item when its removed from the associated collection |
||
| 330 | * |
||
| 331 | * @param Jam_Model $model |
||
| 332 | * @param Jam_Model $item |
||
| 333 | */ |
||
| 334 | public function item_unset(Jam_Model $model, Jam_Model $item) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * See if the association is polymorphic |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | 46 | public function is_polymorphic() |
|
| 347 | } // End Kohana_Jam_Association_HasMany |
||
| 348 |
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.