Conditions | 10 |
Paths | 35 |
Total Lines | 64 |
Code Lines | 42 |
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 |
||
56 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
57 | { |
||
58 | $address = $query->getText(); |
||
59 | // This API does not support IP |
||
60 | if (filter_var($address, FILTER_VALIDATE_IP)) { |
||
61 | throw new UnsupportedOperation('The SPW provider does not support IP addresses, only street addresses.'); |
||
62 | } |
||
63 | |||
64 | // Save a request if no valid address entered |
||
65 | if (empty($address)) { |
||
66 | throw new InvalidArgument('Address cannot be empty.'); |
||
67 | } |
||
68 | |||
69 | $url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address)); |
||
70 | $json = $this->executeQuery($url); |
||
71 | |||
72 | // no result |
||
73 | if (is_null($json->x) && is_null($json->y)) { |
||
74 | return new AddressCollection([]); |
||
75 | } |
||
76 | |||
77 | $results = []; |
||
78 | |||
79 | $proj4 = new Proj4php(); |
||
80 | |||
81 | $proj31370 = new Proj('EPSG:31370', $proj4); |
||
82 | $proj4326 = new Proj('EPSG:4326', $proj4); |
||
83 | |||
84 | $pointSrc = new Point($json->x, $json->y, $proj31370); |
||
85 | $coordinates = $proj4->transform($proj4326, $pointSrc); |
||
86 | |||
87 | $streetName = !empty($json->rue->nom) ? $json->rue->nom : null; |
||
88 | $number = !empty($json->num) ? $json->num : null; |
||
89 | $municipality = !empty($json->rue->commune) ? $json->rue->commune : null; |
||
90 | $postCode = !empty($json->rue->cps) ? implode(', ', $json->rue->cps) : null; |
||
91 | $subLocality = !empty($json->rue->localites) ? implode(', ', $json->rue->localites) : null; |
||
92 | $countryCode = 'BE'; |
||
93 | |||
94 | $lowerLeftSrc = new Point($json->rue->xMin, $json->rue->yMin, $proj31370); |
||
95 | $lowerLeft = $proj4->transform($proj4326, $lowerLeftSrc); |
||
96 | $upperRightSrc = new Point($json->rue->xMax, $json->rue->yMax, $proj31370); |
||
97 | $upperRight = $proj4->transform($proj4326, $upperRightSrc); |
||
98 | |||
99 | $bounds = [ |
||
100 | 'west' => $lowerLeft->x, |
||
101 | 'south' => $lowerLeft->y, |
||
102 | 'east' => $upperRight->x, |
||
103 | 'north' => $upperRight->y, |
||
104 | ]; |
||
105 | |||
106 | $results[] = Address::createFromArray([ |
||
107 | 'providedBy' => $this->getName(), |
||
108 | 'latitude' => $coordinates->y, |
||
109 | 'longitude' => $coordinates->x, |
||
110 | 'streetNumber' => $number, |
||
111 | 'streetName' => $streetName, |
||
112 | 'locality' => $municipality, |
||
113 | 'subLocality' => $subLocality, |
||
114 | 'postalCode' => $postCode, |
||
115 | 'countryCode' => $countryCode, |
||
116 | 'bounds' => $bounds, |
||
117 | ]); |
||
118 | |||
119 | return new AddressCollection($results); |
||
120 | } |
||
154 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: