Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Collection extends Component |
||
| 29 | { |
||
| 30 | const EVENT_BEFORE_INSERT = 'beforeInsert'; |
||
| 31 | const EVENT_BEFORE_UPDATE = 'beforeUpdate'; |
||
| 32 | const EVENT_BEFORE_VALIDATE = 'beforeValidate'; |
||
| 33 | const EVENT_AFTER_VALIDATE = 'afterValidate'; |
||
| 34 | const EVENT_AFTER_SAVE = 'afterSave'; |
||
| 35 | const EVENT_BEFORE_LOAD = 'beforeLoad'; |
||
| 36 | const EVENT_AFTER_LOAD = 'afterLoad'; |
||
| 37 | const EVENT_BEFORE_DELETE = 'beforeDelete'; |
||
| 38 | const EVENT_AFTER_DELETE = 'afterDelete'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var boolean Whether to check, that all [[$models]] are instance of the same class |
||
| 42 | * @see isConsistent |
||
| 43 | */ |
||
| 44 | public $checkConsistency = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ActiveRecord[] array of models |
||
| 48 | */ |
||
| 49 | protected $models = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string the name of the form. Sets automatically on [[set()]] |
||
| 53 | * @see set() |
||
| 54 | */ |
||
| 55 | public $formName; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var callable the function to format loaded data. Gets three attributes: |
||
| 59 | * - model (instance of operating model) |
||
| 60 | * - key - the key of the loaded item |
||
| 61 | * - value - the value of the loaded item |
||
| 62 | * Should return array, where the first item is the new key, and the second - a new value. Example: |
||
| 63 | * ``` |
||
| 64 | * return [$key, $value]; |
||
| 65 | * ``` |
||
| 66 | */ |
||
| 67 | public $loadFormatter; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var ActiveRecord the template model instance. May be set manually by [[setModel()]] or |
||
| 71 | * automatically on [[set()]] call |
||
| 72 | * @see setModel() |
||
| 73 | * @see set() |
||
| 74 | */ |
||
| 75 | protected $model; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array options that will be passed to the new model when loading data in [[load]] |
||
| 79 | * @see load() |
||
| 80 | */ |
||
| 81 | public $modelOptions = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Perform query options |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | public $queryOptions = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var ActiveRecord the first model of the set. Fills automatically by [[set()]] |
||
| 91 | * @see set() |
||
| 92 | */ |
||
| 93 | public $first; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array the model's attributes that will be saved |
||
| 97 | */ |
||
| 98 | public $attributes; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Closure a closure that will used to collect data from [[models]] before saving. |
||
| 102 | * Signature: |
||
| 103 | * ```php |
||
| 104 | * function ($model, $collection) |
||
| 105 | * ``` |
||
| 106 | * |
||
| 107 | * Method must return array of two elements: |
||
| 108 | * - 0: key of the model in resulting array |
||
| 109 | * - 1: corresponding value |
||
| 110 | * |
||
| 111 | * @see collectData |
||
| 112 | */ |
||
| 113 | public $dataCollector; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets the model of the collection. |
||
| 117 | * @param ActiveRecord|array $model if the model is an instance of [[Model]] - sets it, otherwise - creates the model |
||
| 118 | * using given options array |
||
| 119 | * @return object|ActiveRecord |
||
| 120 | */ |
||
| 121 | public function setModel($model) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the [[model]]. |
||
| 141 | * @return ActiveRecord |
||
| 142 | */ |
||
| 143 | public function getModel() |
||
| 147 | |||
| 148 | public function getIds() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return ActiveRecord[] models |
||
| 160 | */ |
||
| 161 | public function getModels() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the scenario of the default model. |
||
| 168 | * @param $value string scenario |
||
| 169 | */ |
||
| 170 | public function setScenario($value) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Gets the scenario the default model. |
||
| 177 | * @return string the scenario |
||
| 178 | */ |
||
| 179 | public function getScenario() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Updates [[formName]] from the current [[model]]. |
||
| 186 | * @return string the form name |
||
| 187 | */ |
||
| 188 | public function updateFormName() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * We can load data from 3 different structures:. |
||
| 199 | * 1) POST: [ |
||
| 200 | * 'ModelName' => [ |
||
| 201 | * 'attribute1' => 'value1', |
||
| 202 | * 'attribute2' => 'value2' |
||
| 203 | * ] |
||
| 204 | * ] |
||
| 205 | * 2) POST: [ |
||
| 206 | * 'ModelName' => [ |
||
| 207 | * 1 => [ |
||
| 208 | * 'attribute1' => 'value1', |
||
| 209 | * 'attribute2' => 'value2' |
||
| 210 | * ], |
||
| 211 | * 2 => [ |
||
| 212 | * ... |
||
| 213 | * ] |
||
| 214 | * ] |
||
| 215 | * } |
||
| 216 | * 3) foreach ($selection as $id) { |
||
| 217 | * $res[$id] = [reset($model->primaryKey()) => $id]; |
||
| 218 | * }. |
||
| 219 | * @param array|callable $data - the data to be proceeded. |
||
| 220 | * If is callable - gets arguments: |
||
| 221 | * - model |
||
| 222 | * - fromName |
||
| 223 | * @throws InvalidConfigException |
||
| 224 | * @return Collection |
||
| 225 | */ |
||
| 226 | public function load($data = null) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets the array of AR models to the collection. |
||
| 278 | * @param array|ActiveRecord $models - array of AR Models or a single model |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function set($models) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Saves the current collection. |
||
| 306 | * This method will call [[insert()]] or [[update()]]. |
||
| 307 | * @param bool $runValidation whether to perform validation before saving the collection |
||
| 308 | * @param array $attributes list of attribute names that need to be saved. Defaults to null, |
||
| 309 | * meaning all attributes that are loaded will be saved. If the scenario is specified, will use only |
||
| 310 | * fields from the scenario |
||
| 311 | * @param array $options the array of options that will be passed to [[insert]] or [[update]] methods to override |
||
| 312 | * model parameters |
||
| 313 | * @return bool whether the saving succeeds |
||
| 314 | */ |
||
| 315 | public function save($runValidation = true, $attributes = null, $options = []) |
||
| 328 | |||
| 329 | public function insert($runValidation = true, $attributes = null, array $options = []) |
||
| 330 | { |
||
| 331 | if (!$attributes) { |
||
| 332 | $attributes = $this->attributes ?: $this->first->activeAttributes(); |
||
| 333 | } |
||
| 334 | if ($runValidation && !$this->validate($attributes)) { |
||
| 335 | return false; |
||
| 336 | } |
||
| 337 | if (!$this->beforeSave(true)) { |
||
| 338 | return false; |
||
| 339 | } |
||
| 340 | |||
| 341 | $data = $this->collectData($attributes, $options); |
||
| 342 | $results = $this->first->query('create', $data, $options); |
||
| 343 | $pk = $this->first->primaryKey()[0]; |
||
| 344 | foreach ($this->models as $key => $model) { |
||
| 345 | $values = &$data[$key]; |
||
| 346 | $result = &$results[$key]; |
||
| 347 | |||
| 348 | $model->{$pk} = $result['id']; |
||
| 349 | if ($pk !== 'id') { |
||
| 350 | $values[$pk] = $result['id']; |
||
| 351 | } |
||
| 352 | $changedAttributes = array_fill_keys(array_keys($values), null); |
||
| 353 | $model->setOldAttributes($values); |
||
| 354 | $model->afterSave(true, $changedAttributes); |
||
| 355 | } |
||
| 356 | |||
| 357 | $this->afterSave(); |
||
| 358 | |||
| 359 | return true; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function update($runValidation = true, $attributes = null, array $options = []) |
||
| 363 | { |
||
| 364 | if (!$attributes) { |
||
| 365 | $attributes = $this->attributes ?: $this->first->activeAttributes(); |
||
| 366 | } |
||
| 367 | if ($runValidation && !$this->validate($attributes)) { |
||
| 368 | return false; |
||
| 369 | } |
||
| 370 | if (!$this->beforeSave()) { |
||
| 371 | return false; |
||
| 372 | } |
||
| 373 | |||
| 374 | $data = $this->collectData($attributes, $options); |
||
| 375 | $results = $this->first->query('update', $data, $options); |
||
| 376 | |||
| 377 | foreach ($this->models as $key => $model) { |
||
| 378 | $changedAttributes = []; |
||
| 379 | $values = array_key_exists($key, $data) ? $data[$key] : $data[$model->id]; /// XXX not good |
||
| 380 | foreach ($values as $name => $value) { |
||
| 381 | $changedAttributes[$name] = $model->getOldAttribute($name); |
||
| 382 | $model->setOldAttribute($name, $value); |
||
| 383 | } |
||
| 384 | $model->afterSave(false, $changedAttributes); |
||
| 385 | } |
||
| 386 | |||
| 387 | $this->afterSave(); |
||
| 388 | |||
| 389 | return true; |
||
| 390 | } |
||
| 391 | |||
| 392 | public function delete() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Collects data from the stored models. |
||
| 408 | * @param string|array $attributes list of attributes names |
||
| 409 | * @param array $options |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | public function collectData($attributes = null, $options = []) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Whether one of models has an error. |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public function hasErrors() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the first error of the collection. |
||
| 450 | * @return bool|mixed |
||
| 451 | */ |
||
| 452 | public function getFirstError() |
||
| 464 | |||
| 465 | public function count() |
||
| 469 | |||
| 470 | public function validate($attributes = null) |
||
| 484 | |||
| 485 | public function beforeValidate() |
||
| 492 | |||
| 493 | public function afterValidate() |
||
| 501 | |||
| 502 | public function beforeSave($insert = false) |
||
| 512 | |||
| 513 | public function afterSave() |
||
| 517 | |||
| 518 | public function beforeLoad() |
||
| 525 | |||
| 526 | public function afterLoad() |
||
| 530 | |||
| 531 | public function beforeDelete() |
||
| 538 | |||
| 539 | public function afterDelete() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Iterates over all of the models and triggers some event. |
||
| 546 | * @param string $name the event name |
||
| 547 | * @param ModelEvent $event |
||
| 548 | * @return bool whether is valid |
||
| 549 | */ |
||
| 550 | public function triggerModels($name, ModelEvent $event = null) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Calls [[triggerModels()]], then calls [[trigger()]]. |
||
| 564 | * @param string $name the event name |
||
| 565 | * @param ModelEvent $event |
||
| 566 | * @return bool whether is valid |
||
| 567 | */ |
||
| 568 | public function triggerAll($name, ModelEvent $event = null) |
||
| 579 | |||
| 580 | public function isConsistent() |
||
| 592 | |||
| 593 | public function isEmpty() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param array $options |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function isBatch($options = []) |
||
| 610 | } |
||
| 611 |