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 IPayNow 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 IPayNow, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class IPayNow |
||
13 | { |
||
14 | const TRADE_URL = 'https://pay.ipaynow.cn'; |
||
15 | const TRADE_URL_TEST = 'https://tls-pay.ipaynow.cn'; |
||
16 | const TRADE_TIME_OUT = '3600'; |
||
17 | const TRADE_FUNCODE = 'WP001'; |
||
18 | const QUERY_FUNCODE = 'MQ002'; |
||
19 | const NOTIFY_FUNCODE = 'N001'; |
||
20 | const FRONT_NOTIFY_FUNCODE = 'N002'; |
||
21 | const TRADE_TYPE = '01'; |
||
22 | const TRADE_CURRENCY_TYPE = '156'; |
||
23 | const TRADE_CHARSET = 'UTF-8'; |
||
24 | const TRADE_DEVICE_TYPE = '0601'; |
||
25 | const TRADE_SIGN_TYPE = 'MD5'; |
||
26 | const TRADE_QSTRING_EQUAL = '='; |
||
27 | const TRADE_QSTRING_SPLIT = '&'; |
||
28 | const TRADE_FUNCODE_KEY = 'funcode'; |
||
29 | const TRADE_SIGNATURE_KEY = 'mhtSignature'; |
||
30 | const TRADE_OUTPUT_TYPE = '2'; |
||
31 | const SIGNATURE_KEY = 'signature'; |
||
32 | const VERSION = '1.0.0'; |
||
33 | const ALI_PAY_CHANNEL = 12; // 用户所选渠道类型 支付宝:12 |
||
34 | const WECHAT_PAY_CHANNEL = 13; // 用户所选渠道类型 微信:13 |
||
35 | |||
36 | /** |
||
37 | * 应用ID. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $appid; |
||
42 | |||
43 | /** |
||
44 | * 应用Key. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $key; |
||
49 | |||
50 | /** |
||
51 | * 支付渠道. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $channel; |
||
56 | |||
57 | /** |
||
58 | * 异步回调地址 |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $notifyUrl; |
||
63 | |||
64 | /** |
||
65 | * 同步回调地址 |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $returnUrl; |
||
70 | |||
71 | /** |
||
72 | * NowSdk constructor. |
||
73 | * |
||
74 | * @param $appid |
||
75 | * @param $key |
||
76 | * @param $channel |
||
77 | * @param $notifyUrl |
||
78 | * @param $returnUrl |
||
79 | */ |
||
80 | protected function __construct($appid, $key, $channel, $notifyUrl, $returnUrl) |
||
88 | |||
89 | /** |
||
90 | * 获取现在支付微信SDK. |
||
91 | * |
||
92 | * @param $config |
||
93 | * |
||
94 | * @throws InvalidConfigException |
||
95 | * |
||
96 | * @return IPayNow |
||
97 | */ |
||
98 | View Code Duplication | public static function wechat($config) |
|
|
|||
99 | { |
||
100 | try { |
||
101 | return new static( |
||
102 | $config['appid'], |
||
103 | $config['key'], |
||
104 | static::WECHAT_PAY_CHANNEL, |
||
105 | isset($config['notify_url']) ? $config['notify_url'] : '', |
||
106 | isset($config['return_url']) ? $config['return_url'] : '' |
||
107 | ); |
||
108 | } catch (\Exception $e) { |
||
109 | throw new InvalidConfigException($e->getMessage()); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * 获取现在支付支付宝SDK. |
||
115 | * |
||
116 | * @param $config |
||
117 | * |
||
118 | * @throws InvalidConfigException |
||
119 | * |
||
120 | * @return IPayNow |
||
121 | */ |
||
122 | View Code Duplication | public static function ali($config) |
|
123 | { |
||
124 | try { |
||
125 | return new static( |
||
126 | $config['appid'], |
||
127 | $config['key'], |
||
128 | static::ALI_PAY_CHANNEL, |
||
129 | isset($config['notify_url']) ? $config['notify_url'] : '', |
||
130 | isset($config['return_url']) ? $config['return_url'] : '' |
||
131 | ); |
||
132 | } catch (\Exception $e) { |
||
133 | throw new InvalidConfigException($e->getMessage()); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * 预下单. |
||
139 | * |
||
140 | * @see https://mch.ipaynow.cn/h5Pay |
||
141 | * |
||
142 | * @param array $order |
||
143 | * |
||
144 | * @throws \CloudyCity\IPayNowSDK\Exceptions\GatewayException |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function pre(array $order) |
||
195 | |||
196 | /** |
||
197 | * 验证回调通知. |
||
198 | * |
||
199 | * @param $params |
||
200 | * |
||
201 | * @throws InvalidSignException |
||
202 | */ |
||
203 | public function verify($params = null) |
||
218 | |||
219 | /** |
||
220 | * 获取下单请求的字符串. |
||
221 | * |
||
222 | * @param array $params |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function getRequestString(array $params) |
||
240 | |||
241 | /** |
||
242 | * 计算签名. |
||
243 | * |
||
244 | * @param array $params 参数 |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | protected function getSignature(array $params) |
||
262 | |||
263 | /** |
||
264 | * 过滤参数. |
||
265 | * |
||
266 | * @param array $params |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function filterParams(array $params) |
||
294 | } |
||
295 |
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.