| Conditions | 20 |
| Paths | 5 |
| Total Lines | 48 |
| Code Lines | 31 |
| 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 |
||
| 75 | private function executeQuery(string $url): AddressCollection |
||
| 76 | { |
||
| 77 | $content = $this->getUrlContents($url); |
||
| 78 | $json = json_decode($content, true); |
||
| 79 | |||
| 80 | if (!is_array($json) || !count($json)) { |
||
| 81 | throw InvalidServerResponse::create($url); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!array_key_exists('geoplugin_status', $json) || (200 !== $json['geoplugin_status'] && 206 !== $json['geoplugin_status'])) { |
||
| 85 | return new AddressCollection([]); |
||
| 86 | } |
||
| 87 | |||
| 88 | // Return empty collection if address was not found |
||
| 89 | if ('' === $json['geoplugin_regionName'] |
||
| 90 | && '' === $json['geoplugin_regionCode'] |
||
| 91 | && '' === $json['geoplugin_city'] |
||
| 92 | && '' === $json['geoplugin_countryName'] |
||
| 93 | && '' === $json['geoplugin_countryCode'] |
||
| 94 | && '0' === $json['geoplugin_latitude'] |
||
| 95 | && '0' === $json['geoplugin_longitude']) { |
||
| 96 | return new AddressCollection([]); |
||
| 97 | } |
||
| 98 | |||
| 99 | $data = array_filter($json); |
||
| 100 | |||
| 101 | $adminLevels = []; |
||
| 102 | |||
| 103 | $region = \igorw\get_in($data, ['geoplugin_regionName']); |
||
| 104 | $regionCode = \igorw\get_in($data, ['geoplugin_regionCode']); |
||
| 105 | |||
| 106 | if (null !== $region || null !== $regionCode) { |
||
| 107 | $adminLevels[] = ['name' => $region, 'code' => $regionCode, 'level' => 1]; |
||
| 108 | } |
||
| 109 | |||
| 110 | $results = []; |
||
| 111 | $results[] = Address::createFromArray([ |
||
| 112 | 'providedBy' => $this->getName(), |
||
| 113 | 'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null, |
||
| 114 | 'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null, |
||
| 115 | 'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null, |
||
| 116 | 'adminLevels' => $adminLevels, |
||
| 117 | 'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null, |
||
| 118 | 'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null, |
||
| 119 | ]); |
||
| 120 | |||
| 121 | return new AddressCollection($results); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |