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 | * @var array Options that will be passed to [[ActiveRecord::query()]] method as third argument. |
||
| 85 | * @see ActiveRecord::query() |
||
| 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 | public function init() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the model of the collection. |
||
| 125 | * @param ActiveRecord|array $model if the model is an instance of [[Model]] - sets it, otherwise - creates the model |
||
| 126 | * using given options array |
||
| 127 | * @return object|ActiveRecord |
||
| 128 | */ |
||
| 129 | public function setModel($model) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the [[model]]. |
||
| 149 | * @return ActiveRecord |
||
| 150 | */ |
||
| 151 | public function getModel() |
||
| 155 | |||
| 156 | public function getIds() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return ActiveRecord[] models |
||
| 168 | */ |
||
| 169 | public function getModels() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the scenario of the default model. |
||
| 176 | * @param $value string scenario |
||
| 177 | */ |
||
| 178 | public function setScenario($value) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets the scenario the default model. |
||
| 185 | * @return string the scenario |
||
| 186 | */ |
||
| 187 | public function getScenario() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Updates [[formName]] from the current [[model]]. |
||
| 194 | * @return string the form name |
||
| 195 | */ |
||
| 196 | public function updateFormName() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * We can load data from 3 different structures:. |
||
| 207 | * 1) POST: [ |
||
| 208 | * 'ModelName' => [ |
||
| 209 | * 'attribute1' => 'value1', |
||
| 210 | * 'attribute2' => 'value2' |
||
| 211 | * ] |
||
| 212 | * ] |
||
| 213 | * 2) POST: [ |
||
| 214 | * 'ModelName' => [ |
||
| 215 | * 1 => [ |
||
| 216 | * 'attribute1' => 'value1', |
||
| 217 | * 'attribute2' => 'value2' |
||
| 218 | * ], |
||
| 219 | * 2 => [ |
||
| 220 | * ... |
||
| 221 | * ] |
||
| 222 | * ] |
||
| 223 | * } |
||
| 224 | * 3) foreach ($selection as $id) { |
||
| 225 | * $res[$id] = [reset($model->primaryKey()) => $id]; |
||
| 226 | * }. |
||
| 227 | * @param array|callable $data - the data to be proceeded. |
||
| 228 | * If is callable - gets arguments: |
||
| 229 | * - model |
||
| 230 | * - fromName |
||
| 231 | * @throws InvalidConfigException |
||
| 232 | * @return Collection |
||
| 233 | */ |
||
| 234 | public function load($data = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Sets the array of AR models to the collection. |
||
| 286 | * @param array|ActiveRecord $models - array of AR Models or a single model |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function set($models) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Saves the current collection. |
||
| 314 | * This method will call [[insert()]] or [[update()]]. |
||
| 315 | * @param bool $runValidation whether to perform validation before saving the collection |
||
| 316 | * @param array $attributes list of attribute names that need to be saved. Defaults to null, |
||
| 317 | * meaning all attributes that are loaded will be saved. If the scenario is specified, will use only |
||
| 318 | * fields from the scenario |
||
| 319 | * @param array $options the array of options that will be passed to [[insert]] or [[update]] methods to override |
||
| 320 | * model parameters |
||
| 321 | * @return bool whether the saving succeeds |
||
| 322 | */ |
||
| 323 | public function save($runValidation = true, $attributes = null, $options = []) |
||
| 336 | |||
| 337 | public function insert($runValidation = true, $attributes = null, array $queryOptions = []) |
||
| 369 | |||
| 370 | public function update($runValidation = true, $attributes = null, array $queryOptions = []) |
||
| 399 | |||
| 400 | public function delete() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Collects data from the stored models. |
||
| 416 | * @param string|array $attributes list of attributes names |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public function collectData($attributes = null) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Whether one of models has an error. |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function hasErrors() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Returns the first error of the collection. |
||
| 457 | * @return bool|mixed |
||
| 458 | */ |
||
| 459 | public function getFirstError() |
||
| 471 | |||
| 472 | public function count() |
||
| 476 | |||
| 477 | public function validate($attributes = null) |
||
| 491 | |||
| 492 | public function beforeValidate() |
||
| 499 | |||
| 500 | public function afterValidate() |
||
| 508 | |||
| 509 | public function beforeSave($insert = false) |
||
| 519 | |||
| 520 | public function afterSave() |
||
| 524 | |||
| 525 | public function beforeLoad() |
||
| 532 | |||
| 533 | public function afterLoad() |
||
| 537 | |||
| 538 | public function beforeDelete() |
||
| 545 | |||
| 546 | public function afterDelete() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Iterates over all of the models and triggers some event. |
||
| 553 | * @param string $name the event name |
||
| 554 | * @param ModelEvent $event |
||
| 555 | * @return bool whether is valid |
||
| 556 | */ |
||
| 557 | public function triggerModels($name, ModelEvent $event = null) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Calls [[triggerModels()]], then calls [[trigger()]]. |
||
| 571 | * @param string $name the event name |
||
| 572 | * @param ModelEvent $event |
||
| 573 | * @return bool whether is valid |
||
| 574 | */ |
||
| 575 | public function triggerAll($name, ModelEvent $event = null) |
||
| 586 | |||
| 587 | public function isConsistent() |
||
| 599 | |||
| 600 | public function isEmpty() |
||
| 604 | } |
||
| 605 |