Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BActiveRecord 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 BActiveRecord, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class BActiveRecord extends CActiveRecord implements IHasFrontendModel |
||
10 | { |
||
11 | protected $_hints; |
||
12 | |||
13 | protected $_popupHints; |
||
14 | |||
15 | /** |
||
16 | * @param string $className |
||
17 | * |
||
18 | * @return BActiveRecord|CActiveRecord |
||
19 | */ |
||
20 | 63 | public static function model($className = __CLASS__) |
|
24 | |||
25 | /** |
||
26 | * Получение CHtml::listData по стандартным ключам $key => $value |
||
27 | * |
||
28 | * @param string $key |
||
29 | * @param string|callable $value |
||
30 | * @param CDbCriteria $criteria |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | 3 | public static function listData($key = 'id', $value = 'name', CDbCriteria $criteria = null) |
|
54 | |||
55 | /** |
||
56 | * Получение имени таблицы по имени текущей модели в виде {{class_name}} |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 55 | public function tableName() |
|
64 | |||
65 | /** |
||
66 | * @param bool $runValidation |
||
67 | * @param null $attributes |
||
68 | * @return bool |
||
69 | */ |
||
70 | 29 | public function save($runValidation = true, $attributes = null) |
|
71 | { |
||
72 | 29 | if( $this->isNestedSetModel() ) |
|
73 | 29 | { |
|
74 | if( $this->isNewRecord ) |
||
75 | { |
||
76 | $modelName = get_class($this); |
||
77 | $parent = $modelName::model()->findByPk($this->parent); |
||
78 | 1 | $result = $this->appendTo($parent); |
|
79 | 1 | } |
|
80 | 1 | else |
|
81 | 1 | $result = $this->saveNode($runValidation, $attributes); |
|
82 | |||
83 | return $result; |
||
84 | } |
||
85 | |||
86 | 29 | return parent::save($runValidation, $attributes); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Сохраняем данные в моделях, связанных через отношение |
||
91 | * |
||
92 | * @param $relationName |
||
93 | * @param array $relatedData |
||
94 | * @param bool $ignoreEmptyItems |
||
95 | * |
||
96 | * @return bool |
||
97 | * @throws CHttpException |
||
98 | */ |
||
99 | public function saveRelatedModels($relationName, array $relatedData, $ignoreEmptyItems = true) |
||
100 | { |
||
101 | $models = $this->prepareModels($relationName, $relatedData, $ignoreEmptyItems); |
||
102 | |||
103 | if( !$this->validateRelatedModels($models) ) |
||
104 | return false; |
||
105 | |||
106 | foreach($models as $id => $model) |
||
107 | { |
||
108 | if( !$model->save(false) ) |
||
109 | throw new CHttpException(500, "Не удается сохранить зависимую модель ".get_class($model)); |
||
110 | } |
||
111 | |||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param BActiveRecord[] $models |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function validateRelatedModels(array $models) |
||
121 | { |
||
122 | $validationError = false; |
||
123 | |||
124 | foreach($models as $id => $model) |
||
125 | { |
||
126 | if( !$model->validate() ) |
||
127 | { |
||
128 | foreach($model->errors as $errors) |
||
129 | foreach($errors as $value) |
||
130 | $this->addError('relatedModelErrors', $value); |
||
131 | |||
132 | $validationError = true; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return !$validationError; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param $relationName |
||
141 | * @param array $relatedData |
||
142 | * @param bool|true $ignoreEmptyItems |
||
143 | * |
||
144 | * @return BActiveRecord[] |
||
145 | */ |
||
146 | public function prepareModels($relationName, array $relatedData, $ignoreEmptyItems = true) |
||
147 | { |
||
148 | $models = array(); |
||
149 | $relation = $this->getActiveRelation($relationName); |
||
150 | $className = $relation ? $relation->className : null; |
||
151 | |||
152 | if( is_null($className) ) |
||
153 | return $models; |
||
154 | |||
155 | if( is_array(reset($relatedData)) ) |
||
156 | { |
||
157 | foreach($relatedData as $id => $attributes) |
||
158 | { |
||
159 | $value = trim(implode("", $attributes)); |
||
160 | |||
161 | if( empty($value) && $ignoreEmptyItems ) |
||
162 | continue; |
||
163 | |||
164 | $models[$id] = $this->prepareModel($className, $relation, $id, $attributes); |
||
165 | } |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | if( $relatedModel = $this->{$relationName} ) |
||
170 | { |
||
171 | $models[$relatedModel->primaryKey] = $this->prepareModel($className, $relation, $relatedModel->primaryKey, $relatedData); |
||
172 | } |
||
173 | else |
||
174 | { |
||
175 | $models[] = $this->prepareModel($className, $relation, null, $relatedData); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | return $models; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param string $className |
||
184 | * @param CActiveRelation $relation |
||
185 | * @param integer $id |
||
186 | * @param array $attributes |
||
187 | * |
||
188 | * @return BActiveRecord |
||
189 | */ |
||
190 | 5 | private function prepareModel($className, $relation, $id, $attributes) |
|
191 | { |
||
192 | /** |
||
193 | * @var BActiveRecord $model |
||
194 | */ |
||
195 | $model = $className::model()->findByPk($id); |
||
196 | |||
197 | if( !$model ) |
||
198 | { |
||
199 | $model = new $className(); |
||
200 | $model->{$relation->foreignKey} = $this->getPrimaryKey(); |
||
201 | } |
||
202 | |||
203 | 5 | $model->setAttributes(Arr::trim($attributes)); |
|
204 | |||
205 | 5 | return $model; |
|
206 | } |
||
207 | |||
208 | 8 | public function getFormId() |
|
212 | |||
213 | 5 | public function yesNoList() |
|
220 | |||
221 | public function getImageTypes() |
||
225 | |||
226 | 14 | public function relations() |
|
232 | |||
233 | /** |
||
234 | * @param CDbCriteria $criteria |
||
235 | * |
||
236 | * @return BActiveDataProvider |
||
237 | */ |
||
238 | 7 | public function search(CDbCriteria $criteria = null) |
|
252 | |||
253 | /** |
||
254 | * @param $event |
||
255 | */ |
||
256 | 7 | public function onBeforeSearch($event) |
|
260 | |||
261 | 7 | public function attributeLabels() |
|
316 | |||
317 | /** |
||
318 | * Задает popup подсказки |
||
319 | * @return array |
||
320 | */ |
||
321 | 1 | View Code Duplication | public function getPopupHints() |
336 | |||
337 | /** |
||
338 | * Получает popup подсказку для атрибута |
||
339 | * @param $attribute |
||
340 | * @return null |
||
341 | */ |
||
342 | 1 | public function getPopupHint($attribute) |
|
348 | |||
349 | /** |
||
350 | * Задает подсказки |
||
351 | * @return array |
||
352 | */ |
||
353 | 2 | View Code Duplication | public function getHints() |
368 | |||
369 | /** |
||
370 | * Получает подсказку для атрибута |
||
371 | * @param $attribute |
||
372 | * @return null |
||
373 | */ |
||
374 | 2 | public function getHint($attribute) |
|
380 | |||
381 | /** |
||
382 | * @return string |
||
383 | */ |
||
384 | 5 | public function getFrontendModelName() |
|
388 | |||
389 | /** |
||
390 | * @param CDbCriteria $criteria |
||
391 | * |
||
392 | * @return CDbCriteria |
||
393 | */ |
||
394 | protected function getSearchCriteria(CDbCriteria $criteria) |
||
398 | |||
399 | /** |
||
400 | * @return array of search params |
||
401 | */ |
||
402 | 2 | protected function getSearchParams() |
|
406 | |||
407 | /** |
||
408 | * Проверка на поддержку моделью nested set |
||
409 | * @return bool |
||
410 | */ |
||
411 | 29 | private function isNestedSetModel() |
|
415 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.