Conditions | 12 |
Paths | 131 |
Total Lines | 66 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
125 | private function getLocationByResponse(Response $response): LocationInterface |
||
126 | { |
||
127 | $this->httpStatusCodeValidation((int)$response->getStatusCode()); |
||
128 | |||
129 | if (!$this->isJson($response->getBody())) { |
||
130 | throw new InvalidRequestException('Invalid body format. JSON format expected.'); |
||
131 | } |
||
132 | |||
133 | $body = json_decode($response->getBody(), true); |
||
134 | |||
135 | $resultsCount = isset($body['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['results']) |
||
136 | ? (int)$body['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['results'] |
||
137 | : 0; |
||
138 | |||
139 | if ($resultsCount <= 0) { |
||
140 | return new Location(); |
||
141 | } |
||
142 | |||
143 | $generalizingLocationArray = []; |
||
144 | |||
145 | foreach ($body['response']['GeoObjectCollection']['featureMember'] as $objectItem) { |
||
146 | array_walk_recursive( |
||
147 | $objectItem['GeoObject'], |
||
148 | function ($value, $key) use (&$generalizingLocationArray) { |
||
149 | $generalizingLocationArray[$key] = $value; |
||
150 | } |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | if (!empty($body['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['Point'])) { |
||
155 | $coordinatesString = $body['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['Point']['pos']; |
||
156 | } |
||
157 | |||
158 | if (empty($coordinatesString)) { |
||
159 | $coordinatesString = $generalizingLocationArray['pos']; |
||
160 | } |
||
161 | |||
162 | $coordinatesArray = explode(' ', $coordinatesString); |
||
163 | $coordinates = null; |
||
164 | |||
165 | if (!empty($coordinatesArray[0]) && !empty($coordinatesArray[1])) { |
||
166 | $coordinates = new Coordinates($coordinatesArray[1], $coordinatesArray[0]); |
||
167 | } |
||
168 | |||
169 | $country = null; |
||
170 | |||
171 | if (!empty($generalizingLocationArray['CountryName'] || !empty($generalizingLocationArray['country_code']))) { |
||
172 | $country = new Country( |
||
173 | $generalizingLocationArray['CountryName'], |
||
174 | $generalizingLocationArray['country_code'] |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | $region = null; |
||
179 | |||
180 | if (!empty($generalizingLocationArray['AdministrativeAreaName'])) { |
||
181 | $region = new Region($generalizingLocationArray['AdministrativeAreaName']); |
||
182 | } |
||
183 | |||
184 | return new Location( |
||
185 | $coordinates, |
||
186 | $country, |
||
187 | $region, |
||
188 | $generalizingLocationArray['LocalityName'] ?? '', |
||
189 | $generalizingLocationArray['ThoroughfareName'] ?? '', |
||
190 | $generalizingLocationArray['PostalCodeNumber'] ?? '', |
||
191 | ); |
||
194 |