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 Support 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 Support, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Support |
||
35 | { |
||
36 | use HasHttpRequest; |
||
37 | |||
38 | /** |
||
39 | * Wechat gateway. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $baseUri; |
||
44 | |||
45 | /** |
||
46 | * Config. |
||
47 | * |
||
48 | * @var Config |
||
49 | */ |
||
50 | protected $config; |
||
51 | |||
52 | /** |
||
53 | * Instance. |
||
54 | * |
||
55 | * @var Support |
||
56 | */ |
||
57 | private static $instance; |
||
58 | |||
59 | /** |
||
60 | * Bootstrap. |
||
61 | * |
||
62 | * @author yansongda <[email protected]> |
||
63 | * |
||
64 | * @param Config $config |
||
65 | */ |
||
66 | View Code Duplication | private function __construct(Config $config) |
|
73 | |||
74 | /** |
||
75 | * __get. |
||
76 | * |
||
77 | * @author yansongda <[email protected]> |
||
78 | * |
||
79 | * @param $key |
||
80 | * |
||
81 | * @return mixed|null|Config |
||
82 | */ |
||
83 | public function __get($key) |
||
87 | |||
88 | /** |
||
89 | * create. |
||
90 | * |
||
91 | * @author yansongda <[email protected]> |
||
92 | * |
||
93 | * @param Config $config |
||
94 | * |
||
95 | * @return Support |
||
96 | */ |
||
97 | View Code Duplication | public static function create(Config $config) |
|
105 | |||
106 | /** |
||
107 | * getInstance. |
||
108 | * |
||
109 | * @author yansongda <[email protected]> |
||
110 | * |
||
111 | * @throws InvalidArgumentException |
||
112 | * |
||
113 | * @return Support |
||
114 | */ |
||
115 | public static function getInstance() |
||
123 | |||
124 | /** |
||
125 | * clear. |
||
126 | * |
||
127 | * @author yansongda <[email protected]> |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | public function clear() |
||
135 | |||
136 | /** |
||
137 | * Request wechat api. |
||
138 | * |
||
139 | * @author yansongda <[email protected]> |
||
140 | * |
||
141 | * @param string $endpoint |
||
142 | * @param array $data |
||
143 | * @param bool $cert |
||
144 | * |
||
145 | * @throws GatewayException |
||
146 | * @throws InvalidArgumentException |
||
147 | * @throws InvalidSignException |
||
148 | * |
||
149 | * @return Collection |
||
150 | */ |
||
151 | public static function requestApi($endpoint, $data, $cert = false): Collection |
||
183 | |||
184 | /** |
||
185 | * Filter payload. |
||
186 | * |
||
187 | * @author yansongda <[email protected]> |
||
188 | * |
||
189 | * @param array $payload |
||
190 | * @param array|string $params |
||
191 | * @param bool $preserve_notify_url |
||
192 | * |
||
193 | * @throws InvalidArgumentException |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public static function filterPayload($payload, $params, $preserve_notify_url = false): array |
||
220 | |||
221 | /** |
||
222 | * Generate wechat sign. |
||
223 | * |
||
224 | * @author yansongda <[email protected]> |
||
225 | * |
||
226 | * @param array $data |
||
227 | * |
||
228 | * @throws InvalidArgumentException |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public static function generateSign($data): string |
||
248 | |||
249 | /** |
||
250 | * Generate sign content. |
||
251 | * |
||
252 | * @author yansongda <[email protected]> |
||
253 | * |
||
254 | * @param array $data |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public static function getSignContent($data): string |
||
270 | |||
271 | /** |
||
272 | * Decrypt refund contents. |
||
273 | * |
||
274 | * @author yansongda <[email protected]> |
||
275 | * |
||
276 | * @param string $contents |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public static function decryptRefundContents($contents): string |
||
289 | |||
290 | /** |
||
291 | * Convert array to xml. |
||
292 | * |
||
293 | * @author yansongda <[email protected]> |
||
294 | * |
||
295 | * @param array $data |
||
296 | * |
||
297 | * @throws InvalidArgumentException |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | public static function toXml($data): string |
||
316 | |||
317 | /** |
||
318 | * Convert xml to array. |
||
319 | * |
||
320 | * @author yansongda <[email protected]> |
||
321 | * |
||
322 | * @param string $xml |
||
323 | * |
||
324 | * @throws InvalidArgumentException |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | public static function fromXml($xml): array |
||
338 | |||
339 | /** |
||
340 | * Get service config. |
||
341 | * |
||
342 | * @author yansongda <[email protected]> |
||
343 | * |
||
344 | * @param null|string $key |
||
345 | * @param null|mixed $default |
||
346 | * |
||
347 | * @return mixed|null |
||
348 | */ |
||
349 | View Code Duplication | public function getConfig($key = null, $default = null) |
|
361 | |||
362 | /** |
||
363 | * Get app id according to param type. |
||
364 | * |
||
365 | * @author yansongda <[email protected]> |
||
366 | * |
||
367 | * @param string $type |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | public static function getTypeName($type = ''): string |
||
386 | |||
387 | /** |
||
388 | * Get Base Uri. |
||
389 | * |
||
390 | * @author yansongda <[email protected]> |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | public function getBaseUri() |
||
398 | |||
399 | /** |
||
400 | * Set Http options. |
||
401 | * |
||
402 | * @author yansongda <[email protected]> |
||
403 | * |
||
404 | * @return self |
||
405 | */ |
||
406 | View Code Duplication | private function setHttpOptions(): self |
|
415 | } |
||
416 |
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.