Complex classes like BaseList 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 BaseList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class BaseList extends CComponent |
||
11 | { |
||
12 | /** |
||
13 | * @var string $sorting |
||
14 | */ |
||
15 | public $sorting; |
||
16 | |||
17 | /** |
||
18 | * @var bool $pagination |
||
19 | */ |
||
20 | public $pagination; |
||
21 | |||
22 | /** |
||
23 | * @var Filter[]|null $filters |
||
24 | */ |
||
25 | public $filters = null; |
||
26 | |||
27 | /** |
||
28 | * Параметр, отвечающий за выборку параметров и картинок товаров, |
||
29 | * можно отключить, если данные эти не используются, чтобы не тратить время на обработку |
||
30 | * @var bool |
||
31 | */ |
||
32 | public $fetchContent = true; |
||
33 | |||
34 | public static $sortingRange = array(); |
||
35 | |||
36 | /** |
||
37 | * @var $products FActiveDataProvider |
||
38 | */ |
||
39 | protected $dataProvider; |
||
40 | |||
41 | /** |
||
42 | * @var CDbCriteria $criteria |
||
43 | */ |
||
44 | protected $criteria; |
||
45 | |||
46 | /** |
||
47 | * @var CDbCriteria $criteria |
||
48 | */ |
||
49 | protected $filteredCriteria; |
||
50 | |||
51 | /** |
||
52 | * @param CDbCriteria $criteria |
||
53 | * @param null $sorting |
||
54 | * @param bool $pagination |
||
55 | * @param null $filters |
||
56 | */ |
||
57 | 2 | public function __construct(CDbCriteria $criteria, $sorting = null, $pagination = true, $filters = null) |
|
68 | |||
69 | public function init() |
||
73 | |||
74 | /** |
||
75 | * @param boolean $refresh |
||
76 | * |
||
77 | * @return FActiveDataProvider |
||
78 | */ |
||
79 | 2 | public function getDataProvider($refresh = false) |
|
86 | |||
87 | /** |
||
88 | * @return FActiveDataProvider |
||
89 | */ |
||
90 | public function getRandomDataProvider() |
||
107 | |||
108 | /** |
||
109 | * @param bool $resetLimit |
||
110 | * |
||
111 | * @return array |
||
112 | * @throws CDbException |
||
113 | */ |
||
114 | 2 | public function getModelIds($resetLimit = true) |
|
115 | { |
||
116 | $criteria = clone $this->getFilterCriteria(); |
||
117 | |||
118 | if( $resetLimit ) |
||
119 | { |
||
120 | $criteria->limit = -1; |
||
121 | $criteria->offset = -1; |
||
122 | } |
||
123 | |||
124 | ProductAssignment::model()->addAssignmentCondition($criteria); |
||
125 | |||
126 | $builder = new CDbCommandBuilder(Yii::app()->db->getSchema()); |
||
127 | 2 | $command = $builder->createFindCommand($this->getModel()->tableName(), $criteria); |
|
128 | |||
129 | return CHtml::listData($command->queryAll(), 'id', 'id'); |
||
130 | } |
||
131 | |||
132 | public function getCacheKey() |
||
145 | |||
146 | 2 | public function getFilterCriteria() |
|
172 | |||
173 | /** |
||
174 | * Возвращает раздельный массив join'ов |
||
175 | * @param $sql |
||
176 | * |
||
177 | * @return array |
||
178 | * @throws CHttpException |
||
179 | */ |
||
180 | protected function getJoins($sql) |
||
198 | |||
199 | /** |
||
200 | * Возвращает join нужный для сортировки |
||
201 | * @param $order |
||
202 | * @param $baseJoin |
||
203 | * |
||
204 | * @return string |
||
205 | * @throws CHttpException |
||
206 | */ |
||
207 | protected function getOrderJoin($order, $baseJoin) |
||
232 | |||
233 | protected function setOrder() |
||
238 | |||
239 | 2 | protected function buildDataProvider(CDbCriteria $criteria) |
|
252 | |||
253 | protected function afterFetchData($event) |
||
257 | |||
258 | 2 | protected function setImages() |
|
290 | |||
291 | protected function setRelations($relationName) |
||
311 | |||
312 | /** |
||
313 | * @param string $fk |
||
314 | * @param string $className |
||
315 | * @param array $keys |
||
316 | * @param CDbCriteria $criteria |
||
317 | * |
||
318 | * @return FActiveRecord[] |
||
319 | */ |
||
320 | 2 | protected function findRecords($fk, $className, array $keys = null, CDbCriteria $criteria = null) |
|
331 | |||
332 | /** |
||
333 | * @param string $modelName |
||
334 | * @param FActiveRecord[] $models |
||
335 | */ |
||
336 | 2 | protected function setRecords($modelName, array $models) |
|
357 | |||
358 | /** |
||
359 | * @return CActiveRecord |
||
360 | */ |
||
361 | 2 | protected function getModelName() |
|
365 | |||
366 | 2 | protected function getTablePrefix() |
|
370 | |||
371 | /** |
||
372 | * @return CActiveRecord |
||
373 | */ |
||
374 | 2 | protected function getModel() |
|
379 | |||
380 | /** |
||
381 | * @param CDbCriteria $criteria |
||
382 | * @param $attribute |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | protected function getConditionParams(CDbCriteria $criteria, $attribute) |
||
400 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: