| Conditions | 7 |
| Paths | 7 |
| Total Lines | 56 |
| Code Lines | 37 |
| 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 |
||
| 34 | protected function query($query, $params = []) |
||
|
|
|||
| 35 | { |
||
| 36 | $key = self::getApiKey(); |
||
| 37 | if (!$key) { |
||
| 38 | throw new Exception('No api key defined in env'); |
||
| 39 | } |
||
| 40 | |||
| 41 | $query = str_ireplace(" ", "%20", $query); |
||
| 42 | $findURL = self::API_URL . "/" . $query . "?incl=ciso2&key=" . $key; |
||
| 43 | $output = file_get_contents($findURL); |
||
| 44 | |||
| 45 | if (!$output) { |
||
| 46 | throw new Exception("The api returned no result"); |
||
| 47 | } |
||
| 48 | |||
| 49 | $result = json_decode($output, true); |
||
| 50 | |||
| 51 | if (!$result) { |
||
| 52 | throw new Exception(json_last_error_msg()); |
||
| 53 | } |
||
| 54 | |||
| 55 | $firstResult = $result['resourceSets'][0]['resources'][0] ?? null; |
||
| 56 | if (!$firstResult) { |
||
| 57 | throw new Exception("Empty resultset"); |
||
| 58 | } |
||
| 59 | |||
| 60 | $point = $firstResult['point']['coordinates']; |
||
| 61 | $lat = $point[0]; |
||
| 62 | $lon = $point[1]; |
||
| 63 | |||
| 64 | $address = $firstResult['address']; |
||
| 65 | $countryName = $address['countryRegion']; |
||
| 66 | $countryCode = $address['countryRegionIso2']; |
||
| 67 | |||
| 68 | $streetAndNumber = $address['addressLine'] ?? null; |
||
| 69 | $location = []; |
||
| 70 | if ($streetAndNumber) { |
||
| 71 | $number = null; |
||
| 72 | $street = $streetAndNumber; |
||
| 73 | $parts = explode(' ', $streetAndNumber, 2); |
||
| 74 | if (intval($parts[0]) > 0) { |
||
| 75 | $number = $parts[0]; |
||
| 76 | $street = $parts[1]; |
||
| 77 | } |
||
| 78 | $location = [ |
||
| 79 | 'streetName' => $street, |
||
| 80 | 'streetNumber' => $number, |
||
| 81 | 'postalCode' => $address['postalCode'] ?? null, |
||
| 82 | 'locality' => $address['locality'] ?? null, |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | $country = new Country($countryCode, $countryName); |
||
| 87 | $coordinates = new Coordinates($lat, $lon); |
||
| 88 | |||
| 89 | return new Address($location, $country, $coordinates); |
||
| 90 | } |
||
| 108 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.