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 Post 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 Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Post extends ActiveRecord |
||
47 | { |
||
48 | public $username; |
||
49 | |||
50 | const COMMENT_STATUS_OPEN = 'open'; |
||
51 | const COMMENT_STATUS_CLOSE = 'close'; |
||
52 | const STATUS_PUBLISH = 'publish'; |
||
53 | const STATUS_PRIVATE = 'private'; |
||
54 | const STATUS_DRAFT = 'draft'; |
||
55 | const STATUS_TRASH = 'trash'; |
||
56 | const STATUS_REVIEW = 'review'; |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | public static function tableName() |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | View Code Duplication | public function behaviors() |
|
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | public function rules() |
||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | public function attributeLabels() |
||
133 | |||
134 | /** |
||
135 | * @return \yii\db\ActiveQuery |
||
136 | */ |
||
137 | public function getMedia() |
||
141 | |||
142 | /** |
||
143 | * @return \yii\db\ActiveQuery |
||
144 | */ |
||
145 | public function getPostType() |
||
149 | |||
150 | /** |
||
151 | * @return \yii\db\ActiveQuery |
||
152 | */ |
||
153 | public function getPostAuthor() |
||
157 | |||
158 | /** |
||
159 | * @return \yii\db\ActiveQuery |
||
160 | */ |
||
161 | public function getPostComments() |
||
165 | |||
166 | /** |
||
167 | * @return \yii\db\ActiveQuery |
||
168 | */ |
||
169 | public function getPostMeta() |
||
173 | |||
174 | /** |
||
175 | * @return \yii\db\ActiveQuery |
||
176 | */ |
||
177 | public function getTermRelationships() |
||
181 | |||
182 | /** |
||
183 | * @return \yii\db\ActiveQuery |
||
184 | */ |
||
185 | public function getTerms() |
||
191 | |||
192 | /** |
||
193 | * Get post status as array. |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function getPostStatuses() |
||
207 | |||
208 | /** |
||
209 | * Get comment status as array |
||
210 | */ |
||
211 | View Code Duplication | public function getCommentStatuses() |
|
218 | |||
219 | |||
220 | /** |
||
221 | * Get permalink of current post. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getUrl() |
||
229 | |||
230 | /** |
||
231 | * Get meta for current post. |
||
232 | * |
||
233 | * @param string $name |
||
234 | * @return mixed|null |
||
235 | */ |
||
236 | View Code Duplication | public function getMeta($name) |
|
251 | |||
252 | /** |
||
253 | * Add new meta data for current post. |
||
254 | * |
||
255 | * @param string $name |
||
256 | * @param string|array $value |
||
257 | * @return bool |
||
258 | */ |
||
259 | View Code Duplication | public function setMeta($name, $value) |
|
277 | |||
278 | /** |
||
279 | * Update meta data for current post. |
||
280 | * |
||
281 | * @param string $name |
||
282 | * @param string|array $value |
||
283 | * @return bool |
||
284 | */ |
||
285 | View Code Duplication | public function upMeta($name, $value) |
|
298 | |||
299 | /** |
||
300 | * @param bool $sameType |
||
301 | * @param bool $sameTerm |
||
302 | * @return array|null|Post |
||
303 | */ |
||
304 | View Code Duplication | public function getNextPost($sameType = true, $sameTerm = false) |
|
332 | |||
333 | /** |
||
334 | * @param bool $sameType |
||
335 | * @param bool $sameTerm |
||
336 | * @param string $title |
||
337 | * @param array $options |
||
338 | * @return string |
||
339 | */ |
||
340 | View Code Duplication | public function getNextPostLink($title = '{title}', $sameType = true, $sameTerm = false, $options = []) |
|
354 | |||
355 | /** |
||
356 | * @param bool $sameType |
||
357 | * @param bool $sameTerm |
||
358 | * @return array|null|Post |
||
359 | */ |
||
360 | View Code Duplication | public function getPrevPost($sameType = true, $sameTerm = false) |
|
388 | |||
389 | /** |
||
390 | * @param bool $sameType |
||
391 | * @param bool $sameTerm |
||
392 | * @param string $title |
||
393 | * @param array $options |
||
394 | * @return string |
||
395 | */ |
||
396 | View Code Duplication | public function getPrevPostLink($title = '{title}', $sameType = true, $sameTerm = false, $options = []) |
|
410 | |||
411 | /** |
||
412 | * Generate excerpt of post model. |
||
413 | * |
||
414 | * @param int $limit |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getExcerpt($limit = 55) |
||
431 | |||
432 | |||
433 | /** |
||
434 | * Get permission to access model by current user. |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function getPermission() |
||
449 | |||
450 | /** |
||
451 | * @inheritdoc |
||
452 | */ |
||
453 | public function beforeSave($insert) |
||
467 | } |
||
468 |
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.