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:
1 | <?php |
||
26 | class Category extends ActiveRecord |
||
27 | { |
||
28 | use MultilanguageTrait; |
||
29 | |||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | public static function tableName() |
||
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | public function rules() |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | public function attributes(): array |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | View Code Duplication | public function attributeLabels() |
|
131 | |||
132 | /** |
||
133 | * Reassigning child objects to their new parent after delete the main model record. |
||
134 | */ |
||
135 | public function afterDelete() |
||
141 | |||
142 | /** |
||
143 | * @return array|\yii\db\ActiveRecord[] |
||
144 | */ |
||
145 | public static function getMenu() |
||
151 | |||
152 | /** |
||
153 | * @return array|\yii\db\ActiveRecord[] |
||
154 | */ |
||
155 | View Code Duplication | public static function getActiveMenu() |
|
163 | |||
164 | /** |
||
165 | * @return \yii\db\ActiveQuery |
||
166 | */ |
||
167 | public function getCategoriesLanguages() |
||
173 | |||
174 | /** |
||
175 | * @return \yii\db\ActiveQuery |
||
176 | */ |
||
177 | public function getLanguages() |
||
185 | } |
||
186 |
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.