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 |
||
12 | class AccessToken |
||
13 | { |
||
14 | /** |
||
15 | * ID. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $id; |
||
20 | |||
21 | /** |
||
22 | * Secret. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $secret; |
||
27 | |||
28 | /** |
||
29 | * Cache. |
||
30 | * |
||
31 | * @var Cache |
||
32 | */ |
||
33 | protected $cache; |
||
34 | |||
35 | /** |
||
36 | * Cache Key. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $cacheKey; |
||
41 | |||
42 | /** |
||
43 | * Http instance. |
||
44 | * |
||
45 | * @var Http |
||
46 | */ |
||
47 | protected $http; |
||
48 | |||
49 | /** |
||
50 | * Query name. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $queryName = 'access_token'; |
||
55 | |||
56 | /** |
||
57 | * Response Json key name. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $tokenJsonKey = 'access_token'; |
||
62 | |||
63 | /** |
||
64 | * Cache key prefix. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $prefix = 'entwechat.common.access_token.'; |
||
69 | |||
70 | // API |
||
71 | const API_TOKEN_GET = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'; |
||
72 | |||
73 | /** |
||
74 | * Constructor. |
||
75 | * |
||
76 | * @param string $id |
||
77 | * @param string $secret |
||
78 | * @param \Doctrine\Common\Cache\Cache $cache |
||
79 | */ |
||
80 | public function __construct($id, $secret, Cache $cache = null) |
||
86 | |||
87 | /** |
||
88 | * Get token from WeChat API. |
||
89 | * |
||
90 | * @param bool $forceRefresh |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getToken($forceRefresh = false) |
||
110 | |||
111 | /** |
||
112 | * Set custom token. |
||
113 | * |
||
114 | * @param string $token |
||
115 | * @param int $expires |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setToken($token, $expires = 7200) |
||
125 | |||
126 | /** |
||
127 | * Return the id. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getId() |
||
135 | |||
136 | /** |
||
137 | * Return the app id. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getAppId() |
||
145 | |||
146 | /** |
||
147 | * Return the corp id. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getCorpId() |
||
155 | |||
156 | /** |
||
157 | * Return the secret. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getSecret() |
||
165 | |||
166 | /** |
||
167 | * Return the fingerprint. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getFingerprint() |
||
175 | |||
176 | /** |
||
177 | * Set cache instance. |
||
178 | * |
||
179 | * @param \Doctrine\Common\Cache\Cache $cache |
||
180 | * |
||
181 | * @return AccessToken |
||
182 | */ |
||
183 | public function setCache(Cache $cache) |
||
189 | |||
190 | /** |
||
191 | * Return the cache manager. |
||
192 | * |
||
193 | * @return \Doctrine\Common\Cache\Cache |
||
194 | */ |
||
195 | public function getCache() |
||
199 | |||
200 | /** |
||
201 | * Set the query name. |
||
202 | * |
||
203 | * @param string $queryName |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setQueryName($queryName) |
||
213 | |||
214 | /** |
||
215 | * Return the query name. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getQueryName() |
||
223 | |||
224 | /** |
||
225 | * Return the API request queries. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getQueryFields() |
||
233 | |||
234 | /** |
||
235 | * Get the access token from WeChat server. |
||
236 | * |
||
237 | * @throws \EntWeChat\Core\Exceptions\HttpException |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | View Code Duplication | public function getTokenFromServer() |
|
258 | |||
259 | /** |
||
260 | * Return the http instance. |
||
261 | * |
||
262 | * @return \EntWeChat\Core\Http |
||
263 | */ |
||
264 | public function getHttp() |
||
268 | |||
269 | /** |
||
270 | * Set the http instance. |
||
271 | * |
||
272 | * @param \EntWeChat\Core\Http $http |
||
273 | * |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function setHttp(Http $http) |
||
282 | |||
283 | /** |
||
284 | * Set the access token prefix. |
||
285 | * |
||
286 | * @param string $prefix |
||
287 | * |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function setPrefix($prefix) |
||
296 | |||
297 | /** |
||
298 | * Set access token cache key. |
||
299 | * |
||
300 | * @param string $cacheKey |
||
301 | * |
||
302 | * @return $this |
||
303 | */ |
||
304 | public function setCacheKey($cacheKey) |
||
310 | |||
311 | /** |
||
312 | * Get access token cache key. |
||
313 | * |
||
314 | * @return string $this->cacheKey |
||
315 | */ |
||
316 | public function getCacheKey() |
||
324 | } |
||
325 |
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.