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 Money 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 Money, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Money extends Module { |
||
| 12 | |||
| 13 | public $currentMerchant = ''; |
||
| 14 | |||
| 15 | public function init() { |
||
| 16 | if (!empty($this->config['defaultMerchant'])) { |
||
| 17 | $this->currentMerchant = $this->config['defaultMerchant']; |
||
| 18 | } |
||
| 19 | } |
||
| 20 | |||
| 21 | public function refillPayRecive($data) { |
||
| 22 | $wallets = $this->getUserWallets($data['pay']->user_id); |
||
| 23 | foreach ($wallets as $wallet) { |
||
| 24 | if ($wallet->currency_id == $data['pay']->currency_id) { |
||
| 25 | $wallet->diff($data['pay']->sum, 'Пополнение'); |
||
| 26 | break; |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | public function goToMerchant($pay, $merchant, $method, $merchantOptions) { |
||
| 32 | $objectName = $merchant->object_name; |
||
| 33 | |||
| 34 | if (is_array($pay)) { |
||
| 35 | $pay = new Money\Pay($pay); |
||
| 36 | $pay->save(); |
||
| 37 | } |
||
| 38 | switch ($method['type']) { |
||
| 39 | case 'transfer': |
||
| 40 | $sum = $pay->sum / $method['transfer']->rate; |
||
| 41 | break; |
||
| 42 | default: |
||
| 43 | $sum = $pay->sum; |
||
| 44 | } |
||
| 45 | $className = 'Money\MerchantHelper\\' . $objectName; |
||
| 46 | return $className::goToMerchant($pay->id, $sum, $method['currency'], $merchantOptions['description'], $merchantOptions['success'], $merchantOptions['false']); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function reciver($data, $system, $status, $mr) { |
||
| 50 | if ($system) { |
||
| 51 | $merchant = \Money\Merchant::get($system, 'object_name'); |
||
| 52 | } else { |
||
| 53 | $merchant = false; |
||
| 54 | } |
||
| 55 | if ($merchant) { |
||
| 56 | $this->currentMerchant = $system; |
||
| 57 | } |
||
| 58 | $className = 'Money\MerchantHelper\\' . $this->currentMerchant; |
||
| 59 | $result = $className::reciver($data, $status); |
||
| 60 | $result['pay'] = null; |
||
| 61 | if (!empty($result['payId'])) { |
||
| 62 | $result['pay'] = Money\Pay::get($result['payId']); |
||
| 63 | $mr->pay_id = $result['payId']; |
||
| 64 | } |
||
| 65 | if ($result['pay'] && $result['pay']->pay_status_id == 1) { |
||
| 66 | $statuses = \Money\Pay\Status::getList(['key' => 'code']); |
||
| 67 | if (!empty($statuses[$result['status']])) { |
||
| 68 | $result['pay']->pay_status_id = $statuses[$result['status']]->id; |
||
| 69 | } |
||
| 70 | $result['pay']->date_recive = date('Y-m-d H:i:s'); |
||
| 71 | $result['pay']->save(); |
||
| 72 | if ($result['status'] == 'success' && $result['pay']->callback_module && $result['pay']->callback_method) { |
||
| 73 | App::$cur->{$result['pay']->callback_module}->{$result['pay']->callback_method}($result); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | if (!empty($result['callback'])) { |
||
| 77 | echo $result['callback']; |
||
| 78 | $mr->result_callback = $result['callback']; |
||
| 79 | } |
||
| 80 | if (!empty($result['status'])) { |
||
| 81 | $mr->status = $result['status']; |
||
| 82 | } |
||
| 83 | $mr->save(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getUserBlocks($userId = null) { |
||
| 87 | $userId = $userId ? $userId : \Users\User::$cur->id; |
||
| 88 | $blocked = \Money\Wallet\Block::getList(['where' => [ |
||
| 89 | ['wallet:user_id', $userId], |
||
| 90 | [ |
||
| 91 | ['date_expired', '0000-00-00 00:00:00'], |
||
| 92 | ['date_expired', date('Y-m-d H:i:s'), '>', 'OR'] |
||
| 93 | ] |
||
| 94 | ]]); |
||
| 95 | $blocks = []; |
||
| 96 | foreach ($blocked as $block) { |
||
| 97 | if (empty($blocks[$block->wallet->currency_id])) { |
||
| 98 | $blocks[$block->wallet->currency_id] = $block->amount; |
||
| 99 | } else { |
||
| 100 | $blocks[$block->wallet->currency_id] += $block->amount; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | return $blocks; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param null $userId |
||
|
|
|||
| 108 | * @param bool $walletIdasKey |
||
| 109 | * @param bool $forSelect |
||
| 110 | * @param bool $transferOnly |
||
| 111 | * @return \Money\Wallet[] |
||
| 112 | */ |
||
| 113 | public function getUserWallets($userId = null, $walletIdasKey = false, $forSelect = false, $transferOnly = false) { |
||
| 114 | $userId = $userId ? $userId : \Users\User::$cur->id; |
||
| 115 | if (!$userId) { |
||
| 116 | return []; |
||
| 117 | } |
||
| 118 | $this->getUserBlocks($userId); |
||
| 119 | $where = [['wallet', 1]]; |
||
| 120 | if ($transferOnly) { |
||
| 121 | $where[] = ['transfer', 1]; |
||
| 122 | } |
||
| 123 | $currencies = Money\Currency::getList(['where' => $where]); |
||
| 124 | $wallets = Money\Wallet::getList(['where' => ['user_id', $userId], 'key' => 'currency_id']); |
||
| 125 | $result = []; |
||
| 126 | foreach ($currencies as $currency) { |
||
| 127 | if (empty($wallets[$currency->id])) { |
||
| 128 | $wallet = new Money\Wallet(); |
||
| 129 | $wallet->user_id = $userId; |
||
| 130 | $wallet->currency_id = $currency->id; |
||
| 131 | $wallet->save(); |
||
| 132 | $result[$walletIdasKey ? $wallet->id : $currency->id] = $forSelect ? $wallet->name() : $wallet; |
||
| 133 | } else { |
||
| 134 | $result[$walletIdasKey ? $wallets[$currency->id]->id : $currency->id] = $forSelect ? $wallets[$currency->id]->name() : $wallets[$currency->id]; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | return $result; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function rewardTrigger($event) { |
||
| 149 | } |
||
| 150 | |||
| 151 | public function rewardConditionTrigger($event) { |
||
| 152 | $items = Money\Reward\Condition\Item::getList(['where' => [['type', 'event'], ['value', $event['eventName']]]]); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function reward($reward_id, $sums = [], $rootUser = null) { |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |