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 access, if the ip address is in the whitelist. |
||
| 18 | * @param $ip |
||
| 19 | * @return bool |
||
| 20 | */ |
||
| 21 | public function allowIP($ip) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Return JSON error message. |
||
| 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 message. |
||
| 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 the 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 the 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) |
||
| 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) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Run UnitPay::payOrderFromGate($request) when receive request from UnitPay gate. |
||
| 349 | * @param Request $request |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function payOrderFromGate(Request $request) |
||
| 401 | } |
||
| 402 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.