| Conditions | 32 |
| Paths | 11739 |
| Total Lines | 123 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 198 | public static function get_real_country($country = null) |
||
| 199 | { |
||
| 200 | |||
| 201 | if($country && $country instanceof EcommerceCountry) { |
||
| 202 | $cacheKey = $country->Code; |
||
| 203 | } elseif($country) { |
||
| 204 | $cacheKey = $country; |
||
| 205 | } else { |
||
| 206 | $cacheKey = 'notprovided'; |
||
| 207 | } |
||
| 208 | if(isset(self::$_get_real_country_cache[$cacheKey]) && self::$_get_real_country_cache[$cacheKey]) { |
||
| 209 | |||
| 210 | } else { |
||
| 211 | //save original - just in case... |
||
| 212 | $originalCountry = $country; |
||
| 213 | |||
| 214 | //no country provided |
||
| 215 | if (! $country) { |
||
| 216 | $param = Config::inst()->get('CountryPrice_Translation', 'locale_get_parameter'); |
||
| 217 | |||
| 218 | // 1. CHECK FROM URL |
||
| 219 | $urlCountryCode = null; |
||
| 220 | if (isset($_GET[$param])) { |
||
| 221 | $urlCountryCode = Convert::raw2sql(preg_replace("/[^A-Z]+/", "", strtoupper($_GET[$param]))); |
||
| 222 | } |
||
| 223 | |||
| 224 | // 2. CHECK WHAT THE SYSTEM THINKS THE COUNTRY CHOULD BE |
||
| 225 | |||
| 226 | //now we check it from order / session .... |
||
| 227 | $order = ShoppingCart::current_order(); |
||
| 228 | if($order && $order->exists()) { |
||
| 229 | Session::clear('temporary_country_order_store'); |
||
| 230 | $countryCode = $order->getCountry(); |
||
| 231 | } else { |
||
| 232 | $countryCode = Session::get('temporary_country_order_store'); |
||
| 233 | } |
||
| 234 | |||
| 235 | //if we still dont have a country then we use the standard e-commerce methods ... |
||
| 236 | if(! $countryCode) { |
||
| 237 | $countryCode = EcommerceCountry::get_country(); |
||
| 238 | } |
||
| 239 | |||
| 240 | //lets make our object! |
||
| 241 | if($countryCode) { |
||
| 242 | $country = DataObject::get_one('EcommerceCountry', ['Code' => $countryCode]); |
||
| 243 | } |
||
| 244 | |||
| 245 | if($country && $country instanceof EcommerceCountry) { |
||
| 246 | //do nothing |
||
| 247 | } else { |
||
| 248 | $country = null; |
||
| 249 | } |
||
| 250 | //IF THE COUNTRY DOES NOT MATCH THE URL COUNTRY THEN THE URL WINS!!!! |
||
| 251 | if($urlCountryCode) { |
||
| 252 | if ( |
||
| 253 | ($country && $country->Code !== $urlCountryCode) |
||
| 254 | || |
||
| 255 | ! $country |
||
| 256 | |||
| 257 | ){ |
||
| 258 | $country = DataObject::get_one('EcommerceCountry', ['Code' => $urlCountryCode]); |
||
| 259 | if($country) { |
||
| 260 | //change country Object |
||
| 261 | //reset everything ... |
||
| 262 | CountryPrices_ChangeCountryController::set_new_country($country); |
||
| 263 | |||
| 264 | // return self::get_real_country($country); |
||
| 265 | } else { |
||
| 266 | return $this->redirect('404-country-not-found'); |
||
| 267 | } |
||
| 268 | } else { |
||
| 269 | |||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | |||
| 275 | //MAKE SURE WE HAVE AN OBJECT |
||
| 276 | //get the Object |
||
| 277 | if ($country instanceof EcommerceCountry) { |
||
| 278 | //do nothing |
||
| 279 | } elseif (is_numeric($country) && intval($country) == $country) { |
||
| 280 | $country = EcommerceCountry::get()->byID($country); |
||
| 281 | } elseif (is_string($country)) { |
||
| 282 | $country = strtoupper($country); |
||
| 283 | $country = EcommerceCountry::get_country_object(false, $country); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | //LOOK FOR REPLACEMENT COUNTRIES |
||
| 288 | //substitute (always the same as) check .... |
||
| 289 | if ($country && $country instanceof EcommerceCountry) { |
||
| 290 | if ($country->AlwaysTheSameAsID) { |
||
| 291 | $realCountry = $country->AlwaysTheSameAs(); |
||
| 292 | if ($realCountry && $realCountry->exists()) { |
||
| 293 | $country = $realCountry; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } else { |
||
| 297 | //last chance ... do this only once ... |
||
| 298 | $countryCode = EcommerceCountry::get_country_default(); |
||
| 299 | if ($countryCode && !$originalCountry) { |
||
| 300 | $country = self::get_real_country($countryCode); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | //FINAL BOARDING CALL! |
||
| 305 | //surely we have one now??? |
||
| 306 | if ($country && $country instanceof EcommerceCountry) { |
||
| 307 | //do nothing |
||
| 308 | } else { |
||
| 309 | //final backup.... |
||
| 310 | $country = EcommerceCountry::get()->first(); |
||
| 311 | } |
||
| 312 | |||
| 313 | //set to cache ... |
||
| 314 | self::$_get_real_country_cache[$cacheKey] = $country; |
||
| 315 | |||
| 316 | } |
||
| 317 | |||
| 318 | return self::$_get_real_country_cache[$cacheKey]; |
||
| 319 | |||
| 320 | } |
||
| 321 | |||
| 387 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.