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 |
||
28 | class Contact extends ActiveRecord |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public static function tableName() |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function rules() |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | View Code Duplication | public function attributeLabels() |
|
113 | |||
114 | /** |
||
115 | * Returns the default contacts record. |
||
116 | * |
||
117 | * @return null|\yii\db\ActiveRecord |
||
118 | */ |
||
119 | public static function getDefaultContacts() |
||
127 | |||
128 | /** |
||
129 | * Reset the default contacts record. |
||
130 | * |
||
131 | * @param boolean $insert |
||
132 | * |
||
133 | * @return mixed |
||
134 | */ |
||
135 | View Code Duplication | public function beforeSave($insert) |
|
151 | |||
152 | /** |
||
153 | * @return \yii\db\ActiveQuery |
||
154 | */ |
||
155 | public function getContactSocial() |
||
161 | |||
162 | /** |
||
163 | * @return \yii\db\ActiveQuery |
||
164 | */ |
||
165 | public function getSocial() |
||
173 | } |
||
174 |
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.