| Conditions | 12 |
| Paths | 100 |
| Total Lines | 53 |
| Code Lines | 34 |
| 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 |
||
| 61 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 62 | { |
||
| 63 | $address = $query->getText(); |
||
| 64 | if (filter_var($address, FILTER_VALIDATE_IP)) { |
||
| 65 | throw new UnsupportedOperation('The ArcGISOnline provider does not support IP addresses, only street addresses.'); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Save a request if no valid address entered |
||
| 69 | if (empty($address)) { |
||
| 70 | throw new InvalidArgument('Address cannot be empty.'); |
||
| 71 | } |
||
| 72 | |||
| 73 | $url = sprintf(self::ENDPOINT_URL, urlencode($address)); |
||
| 74 | $json = $this->executeQuery($url, $query->getLimit()); |
||
| 75 | |||
| 76 | // no result |
||
| 77 | if (empty($json->locations)) { |
||
| 78 | return new AddressCollection([]); |
||
| 79 | } |
||
| 80 | |||
| 81 | $results = []; |
||
| 82 | foreach ($json->locations as $location) { |
||
| 83 | $data = $location->feature->attributes; |
||
| 84 | |||
| 85 | $coordinates = (array) $location->feature->geometry; |
||
| 86 | $streetName = !empty($data->StAddr) ? $data->StAddr : null; |
||
| 87 | $streetNumber = !empty($data->AddNum) ? $data->AddNum : null; |
||
| 88 | $city = !empty($data->City) ? $data->City : null; |
||
| 89 | $zipcode = !empty($data->Postal) ? $data->Postal : null; |
||
| 90 | $countryCode = !empty($data->Country) ? $data->Country : null; |
||
| 91 | |||
| 92 | $adminLevels = []; |
||
| 93 | foreach (['Region', 'Subregion'] as $i => $property) { |
||
| 94 | if (!empty($data->{$property})) { |
||
| 95 | $adminLevels[] = ['name' => $data->{$property}, 'level' => $i + 1]; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $results[] = Address::createFromArray([ |
||
| 100 | 'providedBy' => $this->getName(), |
||
| 101 | 'latitude' => $coordinates['y'], |
||
| 102 | 'longitude' => $coordinates['x'], |
||
| 103 | 'streetNumber' => $streetNumber, |
||
| 104 | 'streetName' => $streetName, |
||
| 105 | 'locality' => $city, |
||
| 106 | 'postalCode' => $zipcode, |
||
| 107 | 'adminLevels' => $adminLevels, |
||
| 108 | 'countryCode' => $countryCode, |
||
| 109 | ]); |
||
| 110 | } |
||
| 111 | |||
| 112 | return new AddressCollection($results); |
||
| 113 | } |
||
| 114 | |||
| 198 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.