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 | |||
13 | public function __construct() |
||
14 | { |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Allow access, if the ip address is in the whitelist. |
||
19 | * @param $ip |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function allowIP($ip) |
||
23 | { |
||
24 | // Allow local ip |
||
25 | if ($ip == '127.0.0.1') { |
||
26 | return true; |
||
27 | } |
||
28 | |||
29 | return in_array($ip, config('unitpay.allowed_ips')); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Return JSON error message |
||
34 | * @param $message |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function responseError($message) |
||
38 | { |
||
39 | $result['error']['message'] = $message; |
||
|
|||
40 | |||
41 | return $result; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Return JSON success message |
||
46 | * @param $message |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function responseOK($message) |
||
50 | { |
||
51 | $result['result']['message'] = $message; |
||
52 | |||
53 | return $result; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Fill event details to pass the title and request params as array. |
||
58 | * @param $event_type |
||
59 | * @param $event_title |
||
60 | * @param Request $request |
||
61 | */ |
||
62 | public function eventFillAndSend($event_type, $event_title, Request $request) |
||
63 | { |
||
64 | $event_details = [ |
||
65 | 'title' => 'UnitPay: '.$event_title, |
||
66 | 'ip' => $request->ip(), |
||
67 | 'request' => $request->all(), |
||
68 | ]; |
||
69 | |||
70 | event( |
||
71 | new UnitPayEvent($event_type, $event_details) |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return hash for the order form params. |
||
77 | * @param $account |
||
78 | * @param $currency |
||
79 | * @param $desc |
||
80 | * @param $sum |
||
81 | * @param $secretKey |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getFormSignature($account, $currency, $desc, $sum, $secretKey) |
||
85 | { |
||
86 | $hashStr = $account.'{up}'.$currency.'{up}'.$desc.'{up}'.$sum.'{up}'.$secretKey; |
||
87 | |||
88 | return hash('sha256', $hashStr); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return hash for params from UnitPay gate. |
||
93 | * @param $method |
||
94 | * @param array $params |
||
95 | * @param $secretKey |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getSignature($method, array $params, $secretKey) |
||
99 | { |
||
100 | ksort($params); |
||
101 | unset($params['sign'], $params['signature']); |
||
102 | array_push($params, $secretKey); |
||
103 | array_unshift($params, $method); |
||
104 | return hash('sha256', implode('{up}', $params)); |
||
105 | } |
||
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) |
||
202 | { |
||
221 | |||
222 | /** |
||
223 | * Validate request signature from UnitPay gate. |
||
224 | * @param Request $request |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function validateSignature(Request $request) |
||
237 | |||
238 | /** |
||
239 | * Validate ip, request params and signature from UnitPay gate. |
||
240 | * @param Request $request |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function validateOrderRequestFromGate(Request $request) |
||
253 | |||
254 | /** |
||
255 | * Validate the required attributes of the found order |
||
256 | * @param Request $request |
||
257 | * @param $order |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function validateSearchOrderRequiredAttributes(Request $request, $order) |
||
291 | |||
292 | /** |
||
293 | * Call SearchOrderFilter and check return order params. |
||
294 | * @param Request $request |
||
295 | * @return bool |
||
296 | * @throws InvalidConfiguration |
||
297 | */ |
||
298 | public function callFilterSearchOrder(Request $request) |
||
324 | |||
325 | /** |
||
326 | * Call PaidOrderFilter if order not paid. |
||
327 | * @param Request $request |
||
328 | * @param $order |
||
329 | * @return mixed |
||
330 | * @throws InvalidConfiguration |
||
331 | */ |
||
332 | public function callFilterPaidOrder(Request $request, $order) |
||
343 | |||
344 | /** |
||
345 | * Run UnitPay::payOrderFromGate($request) when receive request from UnitPay gate. |
||
346 | * @param Request $request |
||
347 | * @return bool |
||
348 | */ |
||
349 | public function payOrderFromGate(Request $request) |
||
398 | } |
||
399 |
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.