| Total Complexity | 48 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseTemplateController 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.
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 BaseTemplateController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class BaseTemplateController extends Controller |
||
| 11 | { |
||
| 12 | public function smtpConfig($driver, $port, $host, $enc, $email, $password, $name) |
||
| 13 | { |
||
| 14 | Config::set('mail.driver', $driver); |
||
| 15 | Config::set('mail.password', $password); |
||
| 16 | Config::set('mail.username', $email); |
||
| 17 | Config::set('mail.encryption', $enc); |
||
| 18 | Config::set('mail.from', ['address' => $email, 'name' => $name]); |
||
| 19 | Config::set('mail.port', intval($port)); |
||
| 20 | Config::set('mail.host', $host); |
||
| 21 | |||
| 22 | return 'success'; |
||
| 23 | } |
||
| 24 | |||
| 25 | |||
| 26 | public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '') |
||
| 27 | { |
||
| 28 | try { |
||
| 29 | $tax_rule = $this->tax_rule->find(1); |
||
| 30 | $product = $tax_rule->inclusive; |
||
| 31 | $shop = $tax_rule->shop_inclusive; |
||
| 32 | $cart = $tax_rule->cart_inclusive; |
||
| 33 | $result = $price; |
||
| 34 | $controller = new \App\Http\Controllers\Front\GetPageTemplateController(); |
||
| 35 | $location = $controller->getLocation(); |
||
| 36 | |||
| 37 | $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']); |
||
| 38 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']); |
||
| 39 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 40 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 41 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 42 | $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']); |
||
| 43 | $country_iso = $location['countryCode']; |
||
| 44 | |||
| 45 | $geoip_country = ''; |
||
| 46 | $geoip_state = ''; |
||
| 47 | if (\Auth::user()) { |
||
| 48 | $geoip_country = \Auth::user()->country; |
||
| 49 | $geoip_state = \Auth::user()->state; |
||
| 50 | } |
||
| 51 | if ($geoip_country == '') { |
||
| 52 | $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso); |
||
| 53 | } |
||
| 54 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 55 | if ($geoip_state == '') { |
||
| 56 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 57 | $geoip_state = $geoip_state_array['id']; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | $result = $this->getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price); |
||
| 61 | return $result; |
||
| 62 | } catch (\Exception $ex) { |
||
| 63 | Bugsnag::notifyException($ex); |
||
| 64 | throw new \Exception($ex->getMessage()); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price) |
||
| 69 | { |
||
| 70 | if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) { |
||
| 71 | $result = $this->getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $result; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 78 | { |
||
| 79 | if ($product == 1) { |
||
| 80 | $result = $this->getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($product == 0) { |
||
| 84 | $result = $this->getTotalCart($shop, $cart, $price, $cart1, $shop1); |
||
|
|
|||
| 85 | } |
||
| 86 | |||
| 87 | return $result; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getTotalCart($shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 91 | { |
||
| 92 | if ($shop == 0 && $cart == 0) { |
||
| 93 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0); |
||
| 94 | } |
||
| 95 | if ($shop == 1 && $cart == 1) { |
||
| 96 | $result = $this->calculateTotalcart($rate, $price, $cart1, $shop1); |
||
| 97 | } |
||
| 98 | if ($shop == 1 && $cart == 0) { |
||
| 99 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1); |
||
| 100 | } |
||
| 101 | if ($shop == 0 && $cart == 1) { |
||
| 102 | $result = $this->calculateTotalcart($rate, $price, $cart = 1, $shop = 1); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $result; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 109 | { |
||
| 110 | if ($shop == 1 && $cart == 1) { |
||
| 111 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0); |
||
| 112 | } |
||
| 113 | if ($shop == 0 && $cart == 0) { |
||
| 114 | $result = $this->calculateSub($rate, $price, $cart1 = 1, $shop1 = 1); |
||
| 115 | } |
||
| 116 | if ($shop == 1 && $cart == 0) { |
||
| 117 | $result = $this->calculateSub($rate, $price, $cart1, $shop1 = 0); |
||
| 118 | } |
||
| 119 | if ($shop == 0 && $cart == 1) { |
||
| 120 | $result = $this->calculateSub($rate, $price, $cart1 = 0, $shop1); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $result; |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | public function getPrice($price, $currency, $value, $cost) |
||
| 136 | } |
||
| 137 | |||
| 138 | public function prices($id) |
||
| 139 | { |
||
| 140 | $plan = new Plan(); |
||
| 141 | $plans = $plan->where('product', $id)->get(); |
||
| 142 | $price = []; |
||
| 143 | $cart_controller = new \App\Http\Controllers\Front\CartController(); |
||
| 144 | $currency = $cart_controller->currency(); |
||
| 145 | |||
| 146 | foreach ($plans as $value) { |
||
| 147 | $cost = $value->planPrice()->where('currency', $currency)->first()->add_price; |
||
| 148 | $cost = \App\Http\Controllers\Front\CartController::rounding($cost); |
||
| 149 | $months = round($value->days / 30 / 12); |
||
| 150 | $price = $this->getPrice($price, $currency, $value, $cost); |
||
| 151 | } |
||
| 152 | $this->leastAmount($id); |
||
| 153 | |||
| 154 | return $price; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function withoutTaxRelation($productid, $currency) |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | public function taxProcess($taxes, $price, $cart, $shop) |
||
| 173 | { |
||
| 174 | try { |
||
| 175 | $rate = ''; |
||
| 176 | $tax_amount = ''; |
||
| 177 | foreach ($taxes as $tax) { |
||
| 178 | if ($tax->compound != 1) { |
||
| 179 | $rate += $tax->rate; |
||
| 180 | } else { |
||
| 181 | $rate = $tax->rate; |
||
| 182 | } |
||
| 183 | $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $tax_amount; |
||
| 187 | } catch (\Exception $ex) { |
||
| 188 | Bugsnag::notifyException($ex); |
||
| 189 | throw new \Exception($ex->getMessage()); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getTaxAmount($cart, $taxes, $price, $cart1, $shop) |
||
| 194 | { |
||
| 195 | if ($cart == 1) { |
||
| 196 | $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop); |
||
| 197 | } else { |
||
| 198 | $rate = ''; |
||
| 199 | foreach ($taxes as $tax) { |
||
| 200 | if ($tax->compound != 1) { |
||
| 201 | $rate += $tax->rate; |
||
| 202 | } else { |
||
| 203 | $rate = $tax->rate; |
||
| 204 | $price = $this->calculateTotal($rate, $price); |
||
| 205 | } |
||
| 206 | $tax_amount = $this->calculateTotal($rate, $price); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | return $tax_amount; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function calculateTotal($rate, $price) |
||
| 219 | } |
||
| 220 | } |
||
| 221 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.