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 = []) |
||
361 | |||
362 | public function update($runValidation = true, $attributes = null, array $options = []) |
||
391 | |||
392 | public function delete() |
||
405 | |||
406 | /** |
||
407 | * Collects data from the stored models. |
||
408 | * @param string|array $attributes list of attributes names |
||
409 | * @return array |
||
410 | */ |
||
411 | public function collectData($attributes = null) |
||
431 | |||
432 | /** |
||
433 | * Whether one of models has an error. |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function hasErrors() |
||
446 | |||
447 | /** |
||
448 | * Returns the first error of the collection. |
||
449 | * @return bool|mixed |
||
450 | */ |
||
451 | public function getFirstError() |
||
463 | |||
464 | public function count() |
||
468 | |||
469 | public function validate($attributes = null) |
||
483 | |||
484 | public function beforeValidate() |
||
491 | |||
492 | public function afterValidate() |
||
500 | |||
501 | public function beforeSave($insert = false) |
||
511 | |||
512 | public function afterSave() |
||
516 | |||
517 | public function beforeLoad() |
||
524 | |||
525 | public function afterLoad() |
||
529 | |||
530 | public function beforeDelete() |
||
537 | |||
538 | public function afterDelete() |
||
542 | |||
543 | /** |
||
544 | * Iterates over all of the models and triggers some event. |
||
545 | * @param string $name the event name |
||
546 | * @param ModelEvent $event |
||
547 | * @return bool whether is valid |
||
548 | */ |
||
549 | public function triggerModels($name, ModelEvent $event = null) |
||
560 | |||
561 | /** |
||
562 | * Calls [[triggerModels()]], then calls [[trigger()]]. |
||
563 | * @param string $name the event name |
||
564 | * @param ModelEvent $event |
||
565 | * @return bool whether is valid |
||
566 | */ |
||
567 | public function triggerAll($name, ModelEvent $event = null) |
||
578 | |||
579 | public function isConsistent() |
||
591 | |||
592 | public function isEmpty() |
||
596 | |||
597 | /** |
||
598 | * @param array $options |
||
599 | * @return bool |
||
600 | */ |
||
601 | public function isBatch($options = []) |
||
609 | } |
||
610 |