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 Staff extends AbstractAPI |
||
29 | { |
||
30 | const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist'; |
||
31 | const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist'; |
||
32 | const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del'; |
||
33 | const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update'; |
||
34 | const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add'; |
||
35 | const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send'; |
||
36 | const API_AVATAR_UPLOAD = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg'; |
||
37 | |||
38 | /** |
||
39 | * List all staffs. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | 1 | public function lists() |
|
47 | |||
48 | /** |
||
49 | * List all online staffs. |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | 1 | public function onlines() |
|
57 | |||
58 | /** |
||
59 | * Create a staff. |
||
60 | * |
||
61 | * @param string $email |
||
62 | * @param string $nickname |
||
63 | * @param string $password |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | 1 | View Code Duplication | public function create($email, $nickname, $password) |
77 | |||
78 | /** |
||
79 | * Update a staff. |
||
80 | * |
||
81 | * @param string $email |
||
82 | * @param string $nickname |
||
83 | * @param string $password |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | 1 | View Code Duplication | public function update($email, $nickname, $password) |
97 | |||
98 | /** |
||
99 | * Delete a staff. |
||
100 | * |
||
101 | * @param string $email |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 1 | public function delete($email) |
|
109 | |||
110 | /** |
||
111 | * Set staff avatar. |
||
112 | * |
||
113 | * @param string $email |
||
114 | * @param string $path |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | 1 | public function avatar($email, $path) |
|
122 | |||
123 | /** |
||
124 | * Get message builder. |
||
125 | * |
||
126 | * @param \EasyWeChat\Message\AbstractMessage|string $message |
||
127 | * |
||
128 | * @return \EasyWeChat\Staff\MessageBuilder |
||
129 | * |
||
130 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
131 | */ |
||
132 | public function message($message) |
||
138 | |||
139 | /** |
||
140 | * Send a message. |
||
141 | * |
||
142 | * @param string|array $message |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function send($message) |
||
150 | } |
||
151 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.