Complex classes like UnitPay 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 UnitPay, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class UnitPay |
||
11 | { |
||
12 | public function __construct() |
||
15 | |||
16 | /** |
||
17 | * Allow if ip address is in whitelist. |
||
18 | * @param $ip |
||
19 | * @return bool |
||
20 | */ |
||
21 | public function allowIP($ip) |
||
30 | |||
31 | /** |
||
32 | * Return json error result. |
||
33 | * @param $message |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function responseError($message) |
||
37 | { |
||
38 | $result['error']['message'] = $message; |
||
|
|||
39 | |||
40 | return $result; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Return json success result. |
||
45 | * @param $message |
||
46 | * @return mixed |
||
47 | */ |
||
48 | public function responseOK($message) |
||
49 | { |
||
50 | $result['result']['message'] = $message; |
||
51 | |||
52 | return $result; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Fill event details to pass title and request params as array. |
||
57 | * @param $event_type |
||
58 | * @param $event_title |
||
59 | * @param Request $request |
||
60 | */ |
||
61 | public function eventFillAndSend($event_type, $event_title, Request $request) |
||
73 | |||
74 | /** |
||
75 | * Return hash for order form params. |
||
76 | * @param $account |
||
77 | * @param $currency |
||
78 | * @param $desc |
||
79 | * @param $sum |
||
80 | * @param $secretKey |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getFormSignature($account, $currency, $desc, $sum, $secretKey) |
||
89 | |||
90 | /** |
||
91 | * Return hash for params from UnitPay gate. |
||
92 | * @param $method |
||
93 | * @param array $params |
||
94 | * @param $secretKey |
||
95 | * @return string |
||
96 | */ |
||
97 | public function getSignature($method, array $params, $secretKey) |
||
106 | |||
107 | /** |
||
108 | * Generate UnitPay order array with required array for order form. |
||
109 | * @param $payment_amount |
||
110 | * @param $payment_no |
||
111 | * @param $user_email |
||
112 | * @param $item_name |
||
113 | * @param $currency |
||
114 | * @return array |
||
115 | */ |
||
116 | public function generateUnitPayOrderWithRequiredFields($payment_amount, $payment_no, $user_email, $item_name, $currency) |
||
130 | |||
131 | /** |
||
132 | * Check required order params for order form and raise an exception if fails. |
||
133 | * @param $order |
||
134 | * @throws InvalidConfiguration |
||
135 | */ |
||
136 | public function requiredOrderParamsCheck($order) |
||
164 | |||
165 | /** |
||
166 | * Generate html forms from view with payment buttons |
||
167 | * Note: you can customise the view via artisan:publish. |
||
168 | * @param $order |
||
169 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
170 | */ |
||
171 | public function generatePaymentForm($payment_amount, $payment_no, $user_email, $item_name, $currency) |
||
195 | |||
196 | /** |
||
197 | * Validate request params from UnitPay gate. |
||
198 | * @param Request $request |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function validate(Request $request) |
||
220 | |||
221 | /** |
||
222 | * Validate request signature from UnitPay gate. |
||
223 | * @param Request $request |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function validateSignature(Request $request) |
||
236 | |||
237 | /** |
||
238 | * Validate ip, request params and signature from UnitPay gate. |
||
239 | * @param Request $request |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function validateOrderRequestFromGate(Request $request) |
||
252 | |||
253 | /** |
||
254 | * Call SearchOrderFilter and check return order params. |
||
255 | * @param Request $request |
||
256 | * @return bool |
||
257 | * @throws InvalidConfiguration |
||
258 | */ |
||
259 | public function callFilterSearchOrder(Request $request) |
||
260 | { |
||
261 | $callable = config('unitpay.SearchOrderFilter'); |
||
262 | |||
263 | if (! is_callable($callable)) { |
||
264 | throw InvalidConfiguration::searchOrderFilterInvalid(); |
||
265 | } |
||
266 | |||
267 | /* |
||
268 | * SearchOrderFilter |
||
269 | * Search order in the database and return order details |
||
270 | * Must return array with: |
||
271 | * |
||
272 | * orderStatus |
||
273 | * orderCurrency |
||
274 | * orderSum |
||
275 | */ |
||
276 | |||
277 | $order = $callable($request, $request->input('params.account')); |
||
278 | |||
279 | if (! $order) { |
||
280 | $this->eventFillAndSend('unitpay.error', 'orderNotFound', $request); |
||
281 | |||
282 | return false; |
||
283 | } |
||
284 | |||
285 | if (! array_key_exists('orderStatus', $order)) { |
||
286 | $this->eventFillAndSend('unitpay.error', 'orderStatusInvalid', $request); |
||
287 | |||
288 | return false; |
||
289 | } |
||
290 | |||
291 | if (! array_key_exists('orderSum', $order) && $request->input('params.orderSum') != $order['orderSum']) { |
||
292 | $this->eventFillAndSend('unitpay.error', 'orderSumInvalid', $request); |
||
293 | |||
294 | return false; |
||
295 | } |
||
296 | |||
297 | if (! array_key_exists('orderCurrency', $order) && $request->input('params.orderCurrency') != $order['orderCurrency']) { |
||
298 | $this->eventFillAndSend('unitpay.error', 'orderCurrencyInvalid', $request); |
||
299 | |||
300 | return false; |
||
301 | } |
||
302 | |||
303 | return $order; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Call PaidOrderFilter if order not paid. |
||
308 | * @param Request $request |
||
309 | * @param $order |
||
310 | * @return mixed |
||
311 | * @throws InvalidConfiguration |
||
312 | */ |
||
313 | public function callFilterPaidOrder(Request $request, $order) |
||
314 | { |
||
315 | $callable = config('unitpay.PaidOrderFilter'); |
||
316 | |||
317 | if (! is_callable($callable)) { |
||
318 | throw InvalidConfiguration::orderPaidFilterInvalid(); |
||
319 | } |
||
320 | |||
321 | // Run PaidOrderFilter callback |
||
322 | return $callable($request, $order); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Run UnitPay::payOrderFromGate($request) when receive request from UnitPay gate. |
||
327 | * @param Request $request |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function payOrderFromGate(Request $request) |
||
379 | } |
||
380 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.