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 |
||
16 | View Code Duplication | class MpNews extends AbstractMessage |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * Message type. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $type = 'mpnews'; |
||
24 | |||
25 | /** |
||
26 | * Properties. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $properties = [ |
||
31 | 'title', |
||
32 | 'thumb_media_id', |
||
33 | 'author', |
||
34 | 'source_url', |
||
35 | 'content', |
||
36 | 'digest', |
||
37 | 'show_cover', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * Aliases of attribute. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $aliases = [ |
||
46 | 'source_url' => 'content_source_url', |
||
47 | 'show_cover' => 'show_cover_pic', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * 设置音乐封面. |
||
52 | * |
||
53 | * @param string $mediaId |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function thumb($mediaId) |
||
63 | } |
||
64 |
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.