Conditions | 10 |
Paths | 5 |
Total Lines | 47 |
Code Lines | 31 |
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 |
||
104 | private function executeQuery(string $url): AddressCollection |
||
105 | { |
||
106 | $content = $this->getUrlContents($url); |
||
107 | |||
108 | $json = json_decode($content, true); |
||
109 | |||
110 | if (!isset($json['hits'])) { |
||
111 | return new AddressCollection([]); |
||
112 | } |
||
113 | |||
114 | $locations = $json['hits']; |
||
115 | |||
116 | if (empty($locations)) { |
||
117 | return new AddressCollection([]); |
||
118 | } |
||
119 | |||
120 | $results = []; |
||
121 | foreach ($locations as $location) { |
||
122 | $bounds = [ |
||
123 | 'east' => null, |
||
124 | 'north' => null, |
||
125 | 'west' => null, |
||
126 | 'south' => null, |
||
127 | ]; |
||
128 | if (isset($location['extent'])) { |
||
129 | $bounds = [ |
||
130 | 'east' => $location['extent'][0], |
||
131 | 'north' => $location['extent'][1], |
||
132 | 'west' => $location['extent'][2], |
||
133 | 'south' => $location['extent'][3], |
||
134 | ]; |
||
135 | } |
||
136 | |||
137 | $results[] = Address::createFromArray([ |
||
138 | 'providedBy' => $this->getName(), |
||
139 | 'latitude' => $location['point']['lat'], |
||
140 | 'longitude' => $location['point']['lng'], |
||
141 | 'bounds' => $bounds, |
||
142 | 'streetNumber' => isset($location['housenumber']) ? $location['housenumber'] : null, |
||
143 | 'streetName' => isset($location['street']) ? $location['street'] : null, |
||
144 | 'locality' => isset($location['city']) ? $location['city'] : null, |
||
145 | 'postalCode' => isset($location['postcode']) ? $location['postcode'] : null, |
||
146 | 'country' => isset($location['country']) ? $location['country'] : null, |
||
147 | ]); |
||
148 | } |
||
149 | |||
150 | return new AddressCollection($results); |
||
151 | } |
||
153 |