| Conditions | 11 |
| Paths | 28 |
| Total Lines | 46 |
| Code Lines | 27 |
| 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 |
||
| 101 | private function executeQuery(string $url, string $locale = null, int $limit): Collection |
||
| 102 | { |
||
| 103 | if (null !== $locale) { |
||
| 104 | $url = sprintf('%s&culture=%s', $url, str_replace('_', '-', $locale)); |
||
| 105 | } |
||
| 106 | |||
| 107 | $content = $this->getUrlContents($url); |
||
| 108 | $json = json_decode($content); |
||
| 109 | |||
| 110 | if (!isset($json->resourceSets[0]) || !isset($json->resourceSets[0]->resources)) { |
||
| 111 | return new AddressCollection([]); |
||
| 112 | } |
||
| 113 | |||
| 114 | $data = (array) $json->resourceSets[0]->resources; |
||
| 115 | |||
| 116 | $results = []; |
||
| 117 | foreach ($data as $item) { |
||
| 118 | $builder = new AddressBuilder($this->getName()); |
||
| 119 | $coordinates = (array) $item->geocodePoints[0]->coordinates; |
||
| 120 | $builder->setCoordinates($coordinates[0], $coordinates[1]); |
||
| 121 | |||
| 122 | if (isset($item->bbox) && is_array($item->bbox) && count($item->bbox) > 0) { |
||
| 123 | $builder->setBounds($item->bbox[0], $item->bbox[1], $item->bbox[2], $item->bbox[3]); |
||
| 124 | } |
||
| 125 | |||
| 126 | $builder->setStreetName($item->address->addressLine ?? null); |
||
| 127 | $builder->setPostalCode($item->address->postalCode ?? null); |
||
| 128 | $builder->setLocality($item->address->locality ?? null); |
||
| 129 | $builder->setCountry($item->address->countryRegion ?? null); |
||
| 130 | $builder->setCountryCode($item->address->countryRegionIso2 ?? null); |
||
| 131 | |||
| 132 | foreach (['adminDistrict', 'adminDistrict2'] as $i => $property) { |
||
| 133 | if (property_exists($item->address, $property)) { |
||
| 134 | $builder->addAdminLevel($i + 1, $item->address->{$property}, null); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $results[] = $builder->build(); |
||
| 139 | |||
| 140 | if (count($results) >= $limit) { |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return new AddressCollection($results); |
||
| 146 | } |
||
| 147 | } |
||
| 148 |
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.