Conditions | 19 |
Paths | 16 |
Total Lines | 71 |
Code Lines | 43 |
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 |
||
106 | private function executeQuery(string $url, string $locale = null, int $limit): AddressCollection |
||
107 | { |
||
108 | if (null !== $locale) { |
||
109 | $url = sprintf('%s&lang=%s', $url, str_replace('_', '-', $locale)); |
||
110 | } |
||
111 | |||
112 | $url = sprintf('%s&results=%d', $url, $limit); |
||
113 | $content = $this->getUrlContents($url); |
||
114 | $json = json_decode($content, true); |
||
115 | |||
116 | if (empty($json) || isset($json['error']) || |
||
117 | (isset($json['response']) && '0' === $json['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found']) |
||
118 | ) { |
||
119 | return new AddressCollection([]); |
||
120 | } |
||
121 | |||
122 | $data = $json['response']['GeoObjectCollection']['featureMember']; |
||
123 | |||
124 | $locations = []; |
||
125 | foreach ($data as $item) { |
||
126 | $builder = new AddressBuilder($this->getName()); |
||
127 | $bounds = null; |
||
|
|||
128 | $flatArray = ['pos' => ' ']; |
||
129 | |||
130 | array_walk_recursive( |
||
131 | $item['GeoObject'], |
||
132 | |||
133 | /** |
||
134 | * @param string $value |
||
135 | */ |
||
136 | function ($value, $key) use (&$flatArray) { |
||
137 | $flatArray[$key] = $value; |
||
138 | } |
||
139 | ); |
||
140 | |||
141 | if (!empty($flatArray['lowerCorner']) && !empty($flatArray['upperCorner'])) { |
||
142 | $lowerCorner = explode(' ', $flatArray['lowerCorner']); |
||
143 | $upperCorner = explode(' ', $flatArray['upperCorner']); |
||
144 | $builder->setBounds( |
||
145 | (float) $lowerCorner[1], |
||
146 | (float) $lowerCorner[0], |
||
147 | (float) $upperCorner[1], |
||
148 | (float) $upperCorner[0] |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | $coordinates = explode(' ', $flatArray['pos']); |
||
153 | $builder->setCoordinates((float) $coordinates[1], (float) $coordinates[0]); |
||
154 | |||
155 | foreach (['AdministrativeAreaName', 'SubAdministrativeAreaName'] as $i => $name) { |
||
156 | if (isset($flatArray[$name])) { |
||
157 | $builder->addAdminLevel($i + 1, $flatArray[$name], null); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $builder->setStreetNumber(isset($flatArray['PremiseNumber']) ? $flatArray['PremiseNumber'] : null); |
||
162 | $builder->setStreetName(isset($flatArray['ThoroughfareName']) ? $flatArray['ThoroughfareName'] : null); |
||
163 | $builder->setSubLocality(isset($flatArray['DependentLocalityName']) ? $flatArray['DependentLocalityName'] : null); |
||
164 | $builder->setLocality(isset($flatArray['LocalityName']) ? $flatArray['LocalityName'] : null); |
||
165 | $builder->setCountry(isset($flatArray['CountryName']) ? $flatArray['CountryName'] : null); |
||
166 | $builder->setCountryCode(isset($flatArray['CountryNameCode']) ? $flatArray['CountryNameCode'] : null); |
||
167 | |||
168 | /** @var YandexAddress $location */ |
||
169 | $location = $builder->build(YandexAddress::class); |
||
170 | $location = $location->withPrecision(isset($flatArray['precision']) ? $flatArray['precision'] : null); |
||
171 | $location = $location->withName(isset($flatArray['name']) ? $flatArray['name'] : null); |
||
172 | $locations[] = $location; |
||
173 | } |
||
174 | |||
175 | return new AddressCollection($locations); |
||
176 | } |
||
177 | } |
||
178 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.