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 Property 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 Property, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Property extends ActiveRecord |
||
40 | { |
||
41 | use GetImages; |
||
42 | |||
43 | public static $identity_map = []; |
||
44 | public static $group_id_to_property_ids = []; |
||
45 | private $handlerAdditionalParams = []; |
||
46 | public $required; |
||
47 | public $interpret_as; |
||
48 | public $captcha; |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public function behaviors() |
||
54 | { |
||
55 | return [ |
||
56 | [ |
||
57 | 'class' => AttributeBehavior::className(), |
||
58 | 'attributes' => [ |
||
59 | ActiveRecord::EVENT_BEFORE_INSERT => 'sort_order', |
||
60 | ], |
||
61 | 'value' => 0, |
||
62 | ], |
||
63 | [ |
||
64 | 'class' => ActiveRecordHelper::className(), |
||
65 | ], |
||
66 | [ |
||
67 | 'class' => HasProperties::className(), |
||
68 | ], |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | public static function tableName() |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | public function rules() |
||
84 | { |
||
85 | return [ |
||
86 | [['property_group_id', 'name', 'property_handler_id', 'handler_additional_params'], 'required'], |
||
87 | [ |
||
88 | [ |
||
89 | 'property_group_id', |
||
90 | 'property_handler_id', |
||
91 | 'has_static_values', |
||
92 | 'has_slugs_in_values', |
||
93 | 'is_eav', |
||
94 | 'is_column_type_stored', |
||
95 | 'multiple', |
||
96 | 'sort_order', |
||
97 | 'alias', |
||
98 | ], |
||
99 | 'integer' |
||
100 | ], |
||
101 | [ |
||
102 | [ |
||
103 | 'display_only_on_depended_property_selected', |
||
104 | 'depends_on_property_id', |
||
105 | 'depends_on_category_group_id', |
||
106 | 'hide_other_values_if_selected' |
||
107 | ], |
||
108 | 'integer' |
||
109 | ], |
||
110 | [['interpret_as'], 'string'], |
||
111 | [['name', 'handler_additional_params', 'depended_property_values', 'value_type', 'mask'], 'string'], |
||
112 | [['key'], 'string', 'max' => 20], |
||
113 | [['key'], 'match', 'pattern' => '#^[\w]+$#'], |
||
114 | [['depends_on_property_id', 'depends_on_category_group_id'], 'default', 'value' => 0], |
||
115 | [['required', 'captcha'], 'integer', 'min' => 0, 'max' => 1], |
||
116 | [['dont_filter'], 'safe'], |
||
117 | [['key'], 'unique', 'targetAttribute' => ['key', 'property_group_id']], |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | public function attributeLabels() |
||
125 | { |
||
126 | return [ |
||
127 | 'id' => Yii::t('app', 'ID'), |
||
128 | 'property_group_id' => Yii::t('app', 'Property Group ID'), |
||
129 | 'name' => Yii::t('app', 'Name'), |
||
130 | 'key' => Yii::t('app', 'Key'), |
||
131 | 'value_type' => Yii::t('app', 'Value Type'), |
||
132 | 'property_handler_id' => Yii::t('app', 'Property Handler ID'), |
||
133 | 'has_static_values' => Yii::t('app', 'Has Static Values'), |
||
134 | 'has_slugs_in_values' => Yii::t('app', 'Has Slugs In Values'), |
||
135 | 'is_eav' => Yii::t('app', 'Is Eav'), |
||
136 | 'is_column_type_stored' => Yii::t('app', 'Is Column Type Stored'), |
||
137 | 'multiple' => Yii::t('app', 'Multiple'), |
||
138 | 'sort_order' => Yii::t('app', 'Sort Order'), |
||
139 | 'handler_additional_params' => Yii::t('app', 'Handler Additional Params'), |
||
140 | 'required' => Yii::t('app', 'Required'), |
||
141 | 'interpret_as' => Yii::t('app', 'Interpret Field As'), |
||
142 | 'captcha' => Yii::t('app', 'Captcha'), |
||
143 | 'dont_filter' => Yii::t('app', 'Don\'t use in filtration'), |
||
144 | 'hide_other_values_if_selected' => Yii::t('app', 'Hide Other Values If Selected'), |
||
145 | 'display_only_on_depended_property_selected' => Yii::t('app', 'Display Only On Depended Property Selected'), |
||
146 | 'depends_on_property_id' => Yii::t('app', 'Depends On Property Id'), |
||
147 | 'depended_property_values' => Yii::t('app', 'Depended Property Values'), |
||
148 | 'depends_on_category_group_id' => Yii::t('app', 'Depends On Category Group Id'), |
||
149 | 'mask' => Yii::t('app', 'Mask'), |
||
150 | 'alias' => Yii::t('app', 'Alias'), |
||
151 | ]; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Search tasks |
||
156 | * @param $params |
||
157 | * @return ActiveDataProvider |
||
158 | */ |
||
159 | public function search($params) |
||
160 | { |
||
161 | /* @var $query \yii\db\ActiveQuery */ |
||
162 | $query = static::find()->where(['property_group_id' => $this->property_group_id]); |
||
163 | $dataProvider = new ActiveDataProvider([ |
||
164 | 'query' => $query, |
||
165 | 'pagination' => [ |
||
166 | 'pageSize' => 10, |
||
167 | ], |
||
168 | ]); |
||
169 | if (!($this->load($params))) { |
||
170 | return $dataProvider; |
||
171 | } |
||
172 | $query->andFilterWhere(['id' => $this->id]); |
||
173 | $query->andFilterWhere(['like', 'name', $this->name]); |
||
174 | $query->andFilterWhere(['like', 'key', $this->key]); |
||
175 | $query->andFilterWhere(['property_handler_id' => $this->property_handler_id]); |
||
176 | $query->andFilterWhere(['has_static_values' => $this->has_static_values]); |
||
177 | $query->andFilterWhere(['has_slugs_in_values' => $this->has_slugs_in_values]); |
||
178 | $query->andFilterWhere(['is_eav' => $this->is_eav]); |
||
179 | $query->andFilterWhere(['is_column_type_stored' => $this->is_column_type_stored]); |
||
180 | $query->andFilterWhere(['multiple' => $this->multiple]); |
||
181 | return $dataProvider; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return \yii\db\ActiveQuery |
||
186 | */ |
||
187 | public function getGroup() |
||
191 | |||
192 | /** |
||
193 | * @return PropertyHandler |
||
194 | */ |
||
195 | public function getHandler() |
||
200 | |||
201 | /** |
||
202 | * Возвращает модель по ID с использованием IdentityMap |
||
203 | * @param int $id |
||
204 | * @return null|Property |
||
205 | */ |
||
206 | View Code Duplication | public static function findById($id) |
|
229 | |||
230 | /** |
||
231 | * @param $group_id |
||
232 | * @return null|array<Property> |
||
233 | */ |
||
234 | public static function getForGroupId($group_id) |
||
271 | |||
272 | /** |
||
273 | * @param $form |
||
274 | * @param $model |
||
275 | * @param $values |
||
276 | * @param string $renderType |
||
277 | * @return string |
||
278 | */ |
||
279 | public function handler($form, $model, $values, $renderType = 'frontend_render_view') |
||
291 | |||
292 | /** |
||
293 | * @inheritdoc |
||
294 | */ |
||
295 | public function afterFind() |
||
319 | |||
320 | /** |
||
321 | * @inheritdoc |
||
322 | * @param bool $insert |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function beforeSave($insert) |
||
351 | |||
352 | /** |
||
353 | * |
||
354 | */ |
||
355 | public function invalidateModelCache() |
||
368 | |||
369 | /** |
||
370 | * @param bool $insert |
||
371 | * @param array $changedAttributes |
||
372 | */ |
||
373 | public function afterSave($insert, $changedAttributes) |
||
379 | |||
380 | /** |
||
381 | * @return bool |
||
382 | */ |
||
383 | public function beforeDelete() |
||
388 | |||
389 | public function afterDelete() |
||
416 | |||
417 | /** |
||
418 | * @param $name |
||
419 | * @return null|mixed |
||
420 | */ |
||
421 | public function getAdditionalParam($name) |
||
428 | |||
429 | /** |
||
430 | * @return array |
||
431 | */ |
||
432 | public static function getAliases() |
||
442 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.