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 |
||
27 | class Support |
||
28 | { |
||
29 | use HasHttpRequest; |
||
30 | |||
31 | /** |
||
32 | * Alipay gateway. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $baseUri; |
||
37 | |||
38 | /** |
||
39 | * Config. |
||
40 | * |
||
41 | * @var Config |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * Instance. |
||
47 | * |
||
48 | * @var Support |
||
49 | */ |
||
50 | private static $instance; |
||
51 | |||
52 | /** |
||
53 | * Bootstrap. |
||
54 | * |
||
55 | * @author yansongda <[email protected]> |
||
56 | * |
||
57 | * @param Config $config |
||
58 | */ |
||
59 | View Code Duplication | private function __construct(Config $config) |
|
66 | |||
67 | /** |
||
68 | * __get. |
||
69 | * |
||
70 | * @author yansongda <[email protected]> |
||
71 | * |
||
72 | * @param $key |
||
73 | * |
||
74 | * @return mixed|null|Config |
||
75 | */ |
||
76 | public function __get($key) |
||
80 | |||
81 | /** |
||
82 | * create. |
||
83 | * |
||
84 | * @author yansongda <[email protected]> |
||
85 | * |
||
86 | * @param Config $config |
||
87 | * |
||
88 | * @return Support |
||
89 | */ |
||
90 | View Code Duplication | public static function create(Config $config) |
|
98 | |||
99 | /** |
||
100 | * clear. |
||
101 | * |
||
102 | * @author yansongda <[email protected]> |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function clear() |
||
110 | |||
111 | /** |
||
112 | * Get Alipay API result. |
||
113 | * |
||
114 | * @author yansongda <[email protected]> |
||
115 | * |
||
116 | * @param array $data |
||
117 | * |
||
118 | * @throws GatewayException |
||
119 | * @throws InvalidConfigException |
||
120 | * @throws InvalidSignException |
||
121 | * |
||
122 | * @return Collection |
||
123 | */ |
||
124 | public static function requestApi(array $data): Collection |
||
138 | |||
139 | /** |
||
140 | * Generate sign. |
||
141 | * |
||
142 | * @author yansongda <[email protected]> |
||
143 | * |
||
144 | * @param array $params |
||
145 | * |
||
146 | * @throws InvalidConfigException |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public static function generateSign(array $params): string |
||
180 | |||
181 | /** |
||
182 | * Verify sign. |
||
183 | * |
||
184 | * @author yansongda <[email protected]> |
||
185 | * |
||
186 | * @param array $data |
||
187 | * @param bool $sync |
||
188 | * @param string|null $sign |
||
189 | * |
||
190 | * @throws InvalidConfigException |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public static function verifySign(array $data, $sync = false, $sign = null): bool |
||
224 | |||
225 | /** |
||
226 | * Get signContent that is to be signed. |
||
227 | * |
||
228 | * @author yansongda <[email protected]> |
||
229 | * |
||
230 | * @param array $data |
||
231 | * @param bool $verify |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public static function getSignContent(array $data, $verify = false): string |
||
253 | |||
254 | /** |
||
255 | * Convert encoding. |
||
256 | * |
||
257 | * @author yansongda <[email protected]> |
||
258 | * |
||
259 | * @param string|array $data |
||
260 | * @param string $to |
||
261 | * @param string $from |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public static function encoding($data, $to = 'utf-8', $from = 'gb2312'): array |
||
269 | |||
270 | /** |
||
271 | * Get service config. |
||
272 | * |
||
273 | * @author yansongda <[email protected]> |
||
274 | * |
||
275 | * @param null|string $key |
||
276 | * @param null|mixed $default |
||
277 | * |
||
278 | * @return mixed|null |
||
279 | */ |
||
280 | View Code Duplication | public function getConfig($key = null, $default = null) |
|
292 | |||
293 | /** |
||
294 | * Get Base Uri. |
||
295 | * |
||
296 | * @author yansongda <[email protected]> |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getBaseUri() |
||
304 | |||
305 | /** |
||
306 | * processingApiResult. |
||
307 | * |
||
308 | * @author yansongda <[email protected]> |
||
309 | * |
||
310 | * @param $data |
||
311 | * @param $result |
||
312 | * |
||
313 | * @throws GatewayException |
||
314 | * @throws InvalidConfigException |
||
315 | * @throws InvalidSignException |
||
316 | * |
||
317 | * @return Collection |
||
318 | */ |
||
319 | protected static function processingApiResult($data, $result): Collection |
||
339 | |||
340 | /** |
||
341 | * Set Http options. |
||
342 | * |
||
343 | * @author yansongda <[email protected]> |
||
344 | * |
||
345 | * @return self |
||
346 | */ |
||
347 | View Code Duplication | protected function setHttpOptions(): self |
|
356 | } |
||
357 |
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.