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 |
||
| 30 | class Collection extends Component |
||
| 31 | { |
||
| 32 | const EVENT_BEFORE_INSERT = 'beforeInsert'; |
||
| 33 | const EVENT_BEFORE_UPDATE = 'beforeUpdate'; |
||
| 34 | const EVENT_BEFORE_VALIDATE = 'beforeValidate'; |
||
| 35 | const EVENT_AFTER_VALIDATE = 'afterValidate'; |
||
| 36 | const EVENT_AFTER_SAVE = 'afterSave'; |
||
| 37 | const EVENT_BEFORE_LOAD = 'beforeLoad'; |
||
| 38 | const EVENT_AFTER_LOAD = 'afterLoad'; |
||
| 39 | const EVENT_BEFORE_DELETE = 'beforeDelete'; |
||
| 40 | const EVENT_AFTER_DELETE = 'afterDelete'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var boolean Whether to check, that all [[$models]] are instance of the same class |
||
| 44 | * @see isConsistent |
||
| 45 | */ |
||
| 46 | public $checkConsistency = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ActiveRecord[] array of models |
||
| 50 | */ |
||
| 51 | protected $models = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string the name of the form. Sets automatically on [[set()]] |
||
| 55 | * @see set() |
||
| 56 | */ |
||
| 57 | public $formName; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var callable the function to format loaded data. Gets three attributes: |
||
| 61 | * - model (instance of operating model) |
||
| 62 | * - key - the key of the loaded item |
||
| 63 | * - value - the value of the loaded item |
||
| 64 | * Should return array, where the first item is the new key, and the second - a new value. Example: |
||
| 65 | * ``` |
||
| 66 | * return [$key, $value]; |
||
| 67 | * ``` |
||
| 68 | */ |
||
| 69 | public $loadFormatter; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var ActiveRecord the template model instance. May be set manually by [[setModel()]] or |
||
| 73 | * automatically on [[set()]] call |
||
| 74 | * @see setModel() |
||
| 75 | * @see set() |
||
| 76 | */ |
||
| 77 | protected $model; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array options that will be passed to the new model when loading data in [[load]] |
||
| 81 | * @see load() |
||
| 82 | */ |
||
| 83 | public $modelOptions = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array Options that will be passed to [[ActiveRecord::query()]] method as third argument. |
||
| 87 | * @see ActiveRecord::query() |
||
| 88 | */ |
||
| 89 | public $queryOptions = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var ActiveRecord the first model of the set. Fills automatically by [[set()]] |
||
| 93 | * @see set() |
||
| 94 | */ |
||
| 95 | public $first; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array the model's attributes that will be saved |
||
| 99 | */ |
||
| 100 | public $attributes; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var Closure a closure that will used to collect data from [[models]] before saving. |
||
| 104 | * Signature: |
||
| 105 | * ```php |
||
| 106 | * function ($model, $collection) |
||
| 107 | * ``` |
||
| 108 | * |
||
| 109 | * Method must return array of two elements: |
||
| 110 | * - 0: key of the model in resulting array |
||
| 111 | * - 1: corresponding value |
||
| 112 | * |
||
| 113 | * @see collectData |
||
| 114 | */ |
||
| 115 | public $dataCollector; |
||
| 116 | |||
| 117 | |||
| 118 | public function init() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Sets the model of the collection. |
||
| 127 | * @param ActiveRecord|array $model if the model is an instance of [[Model]] - sets it, otherwise - creates the model |
||
| 128 | * using given options array |
||
| 129 | * @return object|ActiveRecord |
||
| 130 | */ |
||
| 131 | public function setModel($model) |
||
| 132 | { |
||
| 133 | if ($model instanceof Model) { |
||
| 134 | $this->model = $model; |
||
| 135 | } else { |
||
| 136 | $this->model = Yii::createObject($model); |
||
| 137 | } |
||
| 138 | |||
| 139 | $model = $this->model; |
||
| 140 | $this->updateFormName(); |
||
| 141 | |||
| 142 | if (empty($this->getScenario())) { |
||
| 143 | $this->setScenario($model->scenario); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->model; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the [[model]]. |
||
| 151 | * @return ActiveRecord |
||
| 152 | */ |
||
| 153 | public function getModel() |
||
| 157 | |||
| 158 | public function getIds() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return ActiveRecord[] models |
||
| 170 | */ |
||
| 171 | public function getModels() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sets the scenario of the default model. |
||
| 178 | * @param $value string scenario |
||
| 179 | */ |
||
| 180 | public function setScenario($value) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Gets the scenario the default model. |
||
| 187 | * @return string the scenario |
||
| 188 | */ |
||
| 189 | public function getScenario() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Updates [[formName]] from the current [[model]]. |
||
| 196 | * @return string the form name |
||
| 197 | */ |
||
| 198 | public function updateFormName() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * We can load data from 3 different structures:. |
||
| 209 | * 1) POST: [ |
||
| 210 | * 'ModelName' => [ |
||
| 211 | * 'attribute1' => 'value1', |
||
| 212 | * 'attribute2' => 'value2' |
||
| 213 | * ] |
||
| 214 | * ] |
||
| 215 | * 2) POST: [ |
||
| 216 | * 'ModelName' => [ |
||
| 217 | * 1 => [ |
||
| 218 | * 'attribute1' => 'value1', |
||
| 219 | * 'attribute2' => 'value2' |
||
| 220 | * ], |
||
| 221 | * 2 => [ |
||
| 222 | * ... |
||
| 223 | * ] |
||
| 224 | * ] |
||
| 225 | * } |
||
| 226 | * 3) foreach ($selection as $id) { |
||
| 227 | * $res[$id] = [reset($model->primaryKey()) => $id]; |
||
| 228 | * }. |
||
| 229 | * @param array|callable $data - the data to be proceeded. |
||
| 230 | * If is callable - gets arguments: |
||
| 231 | * - model |
||
| 232 | * - fromName |
||
| 233 | * @throws InvalidConfigException |
||
| 234 | * @return Collection |
||
| 235 | */ |
||
| 236 | public function load($data = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Sets the array of AR models to the collection. |
||
| 288 | * @param array|ActiveRecord $models - array of AR Models or a single model |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function set($models) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Saves the current collection. |
||
| 316 | * This method will call [[insert()]] or [[update()]]. |
||
| 317 | * @param bool $runValidation whether to perform validation before saving the collection |
||
| 318 | * @param array $attributes list of attribute names that need to be saved. Defaults to null, |
||
| 319 | * meaning all attributes that are loaded will be saved. If the scenario is specified, will use only |
||
| 320 | * fields from the scenario |
||
| 321 | * @param array $options the array of options that will be passed to [[insert]] or [[update]] methods to override |
||
| 322 | * model parameters |
||
| 323 | * @return bool whether the saving succeeds |
||
| 324 | */ |
||
| 325 | public function save($runValidation = true, $attributes = null, $options = []) |
||
| 338 | |||
| 339 | public function insert($runValidation = true, $attributes = null, array $queryOptions = []) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Try to find the model data if the response from the API came without an index by ID |
||
| 377 | * |
||
| 378 | * @param $data |
||
| 379 | * @param $model |
||
| 380 | * @param $pk |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | private function findAssociatedModelData($data, $model, $pk) |
||
| 392 | |||
| 393 | public function update($runValidation = true, $attributes = null, array $queryOptions = []) |
||
| 422 | |||
| 423 | public function delete() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Collects data from the stored models. |
||
| 439 | * @param string|array $attributes list of attributes names |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function collectData($attributes = null) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Whether one of models has an error. |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | public function hasErrors() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns the first error of the collection. |
||
| 480 | * @return bool|mixed |
||
| 481 | */ |
||
| 482 | public function getFirstError() |
||
| 494 | |||
| 495 | public function count() |
||
| 499 | |||
| 500 | public function validate($attributes = null) |
||
| 514 | |||
| 515 | public function beforeValidate() |
||
| 522 | |||
| 523 | public function afterValidate() |
||
| 531 | |||
| 532 | public function beforeSave($insert = false) |
||
| 542 | |||
| 543 | public function afterSave() |
||
| 547 | |||
| 548 | public function beforeLoad() |
||
| 555 | |||
| 556 | public function afterLoad() |
||
| 560 | |||
| 561 | public function beforeDelete() |
||
| 568 | |||
| 569 | public function afterDelete() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Iterates over all of the models and triggers some event. |
||
| 576 | * @param string $name the event name |
||
| 577 | * @param ModelEvent $event |
||
| 578 | * @return bool whether is valid |
||
| 579 | */ |
||
| 580 | public function triggerModels($name, ModelEvent $event = null) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Calls [[triggerModels()]], then calls [[trigger()]]. |
||
| 594 | * @param string $name the event name |
||
| 595 | * @param ModelEvent $event |
||
| 596 | * @return bool whether is valid |
||
| 597 | */ |
||
| 598 | public function triggerAll($name, ModelEvent $event = null) |
||
| 609 | |||
| 610 | public function isConsistent() |
||
| 622 | |||
| 623 | public function isEmpty() |
||
| 627 | } |
||
| 628 |