| Conditions | 5 |
| Paths | 16 |
| Total Lines | 75 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 42 | public function up() |
||
| 43 | { |
||
| 44 | $project = Config::inst()->get(ModuleManifest::class, 'project'); |
||
| 45 | $config_path = Controller::join_links( |
||
| 46 | BASE_PATH, |
||
| 47 | $project, |
||
| 48 | "_config", |
||
| 49 | "regions.yml" |
||
| 50 | ); |
||
| 51 | |||
| 52 | $legacy = Region::get(); |
||
| 53 | $helper = GeoZonesHelper::create(); |
||
| 54 | $data = [ |
||
| 55 | IntlLocales::class => [ |
||
| 56 | 'countries' => [] |
||
| 57 | ], |
||
| 58 | GeoZonesHelper::class => [ |
||
| 59 | 'iso_3166_regions' => [] |
||
| 60 | ] |
||
| 61 | ]; |
||
| 62 | |||
| 63 | $this->log('Migrating Regions to YML'); |
||
| 64 | $this->log('(This may take some time)'); |
||
| 65 | |||
| 66 | $i = 0; |
||
| 67 | $j = 0; |
||
| 68 | $k = 0; |
||
| 69 | |||
| 70 | foreach ($legacy as $region) { |
||
| 71 | $name = $region->Name; |
||
| 72 | $country = $region->CountryCode; |
||
| 73 | $code = $region->Code; |
||
| 74 | $type = $region->Type; |
||
| 75 | |||
| 76 | // If country is not in system, add to countries |
||
| 77 | try { |
||
| 78 | $helper->setCountriesList([$region->CountryCode]); |
||
| 79 | $helper->setLimitRegionCodes([$region->Code]); |
||
| 80 | $region->delete(); |
||
| 81 | $j++; |
||
| 82 | } catch (LogicException $e) { |
||
| 83 | // If either country or region throw an exception, |
||
| 84 | // add them to the new map |
||
| 85 | if (!in_array(strtolower($country), $data[IntlLocales::class]['countries'])) { |
||
| 86 | $data[IntlLocales::class]['countries'][] = strtolower($country); |
||
| 87 | $k++; |
||
| 88 | } |
||
| 89 | |||
| 90 | $data[GeoZonesHelper::class]['iso_3166_regions'][] = [ |
||
| 91 | 'code' => strtoupper($country) . "-" . $code, |
||
| 92 | 'name' => $name, |
||
| 93 | 'type' => $type |
||
| 94 | ]; |
||
| 95 | |||
| 96 | $region->delete(); |
||
| 97 | $i++; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $yml = <<<YAML |
||
| 102 | --- |
||
| 103 | Name: customregions |
||
| 104 | --- |
||
| 105 | |||
| 106 | YAML; |
||
| 107 | |||
| 108 | $yml .= Yaml::dump($data, 4); |
||
| 109 | |||
| 110 | $this->log("Migrated {$k} Countries"); |
||
| 111 | $this->log("Migrated {$i} Regions"); |
||
| 112 | $this->log("Deleted {$j} Regions"); |
||
| 113 | |||
| 114 | if ($i > 0) { |
||
| 115 | file_put_contents($config_path, $yml); |
||
| 116 | $this->log("Created regions.yml"); |
||
| 117 | } |
||
| 162 |