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 |
||
45 | class Media extends ActiveRecord |
||
46 | { |
||
47 | const COMMENT_STATUS_OPEN = 'open'; |
||
48 | const COMMENT_STATUS_CLOSE = 'close'; |
||
49 | |||
50 | public $username; |
||
51 | public $post_title; |
||
52 | /** |
||
53 | * @var \yii\web\UploadedFile |
||
54 | */ |
||
55 | public $file; |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | public static function tableName() |
||
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | */ |
||
68 | View Code Duplication | public function behaviors() |
|
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function scenarios() |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public function rules() |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function attributeLabels() |
||
139 | |||
140 | /** |
||
141 | * @return \yii\db\ActiveQuery |
||
142 | */ |
||
143 | public function getMediaPost() |
||
147 | |||
148 | /** |
||
149 | * @return \yii\db\ActiveQuery |
||
150 | */ |
||
151 | public function getMediaAuthor() |
||
155 | |||
156 | /** |
||
157 | * @return \yii\db\ActiveQuery |
||
158 | */ |
||
159 | public function getMediaComments() |
||
163 | |||
164 | /** |
||
165 | * @return \yii\db\ActiveQuery |
||
166 | */ |
||
167 | public function getMediaMeta() |
||
171 | |||
172 | /** |
||
173 | * Get comment status as array |
||
174 | */ |
||
175 | View Code Duplication | public function getCommentStatuses() |
|
182 | |||
183 | /** |
||
184 | * Get meta for current media. |
||
185 | * |
||
186 | * @param string $name |
||
187 | * @return mixed|null|string |
||
188 | */ |
||
189 | View Code Duplication | public function getMeta($name) |
|
204 | |||
205 | /** |
||
206 | * Add new meta data for current media. |
||
207 | * |
||
208 | * @param string $name |
||
209 | * @param string|array $value |
||
210 | * @return bool |
||
211 | */ |
||
212 | View Code Duplication | public function setMeta($name, $value) |
|
230 | |||
231 | /** |
||
232 | * Update meta data for current media. |
||
233 | * |
||
234 | * @param string $name |
||
235 | * @param string|array $value |
||
236 | * @return bool |
||
237 | */ |
||
238 | View Code Duplication | public function upMeta($name, $value) |
|
251 | |||
252 | /** |
||
253 | * Get permalink of current media |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getUrl() |
||
261 | |||
262 | /** |
||
263 | * Get upload URL. |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public static function getUploadUrl() |
||
271 | |||
272 | /** |
||
273 | * Get media image thumbnail. If the version is not found, full size will be returned. |
||
274 | * |
||
275 | * @param string $version Version of image thumbnail. |
||
276 | * @param array $options Html::image options. |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getThumbnail($version = 'thumbnail', $options = []) |
||
304 | |||
305 | /** |
||
306 | * Get permission to access model by current user. |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function getPermission() |
||
318 | |||
319 | /** |
||
320 | * @inheritdoc |
||
321 | */ |
||
322 | public function beforeSave($insert) |
||
338 | } |
||
339 |
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.