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 |
||
30 | class MessageBuilder |
||
31 | { |
||
32 | /** |
||
33 | * Message target user or group. |
||
34 | * |
||
35 | * @var mixed |
||
36 | */ |
||
37 | protected $to; |
||
38 | |||
39 | /** |
||
40 | * Message type. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $msgType; |
||
45 | |||
46 | /** |
||
47 | * Message. |
||
48 | * |
||
49 | * @var mixed |
||
50 | */ |
||
51 | protected $message; |
||
52 | |||
53 | /** |
||
54 | * Message types. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $msgTypes = [ |
||
59 | Broadcast::MSG_TYPE_TEXT, |
||
60 | Broadcast::MSG_TYPE_NEWS, |
||
61 | Broadcast::MSG_TYPE_IMAGE, |
||
62 | Broadcast::MSG_TYPE_VIDEO, |
||
63 | Broadcast::MSG_TYPE_VOICE, |
||
64 | Broadcast::MSG_TYPE_CARD, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Preview bys. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $previewBys = [ |
||
73 | Broadcast::PREVIEW_BY_OPENID, |
||
74 | Broadcast::PREVIEW_BY_NAME, |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Set message type. |
||
79 | * |
||
80 | * @param string $msgType |
||
81 | * |
||
82 | * @return MessageBuilder |
||
83 | * |
||
84 | * @throws InvalidArgumentException |
||
85 | */ |
||
86 | 5 | public function msgType($msgType) |
|
96 | |||
97 | /** |
||
98 | * Set message. |
||
99 | * |
||
100 | * @param string|array $message |
||
101 | * |
||
102 | * @return MessageBuilder |
||
103 | */ |
||
104 | 5 | public function message($message) |
|
110 | |||
111 | /** |
||
112 | * Set target user or group. |
||
113 | * |
||
114 | * @param mixed $to |
||
115 | * |
||
116 | * @return MessageBuilder |
||
117 | */ |
||
118 | 5 | public function to($to) |
|
124 | |||
125 | /** |
||
126 | * Build message. |
||
127 | * |
||
128 | * @return bool |
||
129 | * |
||
130 | * @throws RuntimeException |
||
131 | */ |
||
132 | 3 | public function build() |
|
158 | |||
159 | /** |
||
160 | * Build preview message. |
||
161 | * |
||
162 | * @param string $by |
||
163 | * |
||
164 | * @return array |
||
165 | * |
||
166 | * @throws RuntimeException |
||
167 | * @throws InvalidArgumentException |
||
168 | */ |
||
169 | 2 | public function buildPreview($by) |
|
198 | |||
199 | /** |
||
200 | * Build group. |
||
201 | * |
||
202 | * @param mixed $group |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | 2 | private function buildGroup($group) |
|
229 | |||
230 | /** |
||
231 | * Build to. |
||
232 | * |
||
233 | * @param string $to |
||
234 | * @param string $by |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | 2 | private function buildTo($to, $by) |
|
244 | |||
245 | /** |
||
246 | * Return property. |
||
247 | * |
||
248 | * @param string $property |
||
249 | * |
||
250 | * @return mixed |
||
251 | */ |
||
252 | 3 | public function __get($property) |
|
258 | } |
||
259 |
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.