Conditions | 12 |
Paths | 29 |
Total Lines | 54 |
Code Lines | 32 |
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 |
||
159 | private function findMatchedService($requestService, $routes) |
||
160 | { |
||
161 | $matched = []; |
||
162 | $routes = (array) $routes; |
||
163 | $match = array_search($requestService, $routes); |
||
164 | |||
165 | // Fast matching return |
||
166 | if ($match && strlen($routes[$match]) === strlen($requestService)) { |
||
167 | $matched[$match] = $requestService; |
||
168 | return $matched; |
||
169 | } |
||
170 | |||
171 | // When id is given in request |
||
172 | foreach ($routes as $serviceId => $route) { |
||
173 | if ($this->startsWith($route, $requestService)) { |
||
174 | $matched[$serviceId] = $route; |
||
175 | } elseif ($this->startsWith($requestService, $route)) { |
||
176 | $matched[$serviceId] = $route; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // Some routes have same start, but we do not control definition load |
||
181 | if (count($matched) > 1) { |
||
182 | $score = []; |
||
183 | foreach ($matched as $serviceId => $route) { |
||
184 | if (strpos($requestService, $route) !== false) { |
||
185 | $sbResult = substr($requestService, strlen($route)); |
||
186 | $score[strlen($sbResult)] = $serviceId; |
||
187 | } |
||
188 | } |
||
189 | if (!empty($score)) { |
||
190 | ksort($score); |
||
191 | $score = reset($score); |
||
192 | $matched = [$score => $matched[$score]]; |
||
193 | } else { |
||
194 | throw new NotFoundException( |
||
195 | sprintf('Sorry, no route matched. Did you mean: %s', implode(', ', $matched)) |
||
196 | ); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | // Can happen we match a route that's incomplete |
||
201 | $matchedUrl = reset($matched); |
||
202 | if (strlen($matchedUrl) > strlen($requestService)) { |
||
203 | throw new NotFoundException( |
||
204 | sprintf('Sorry, no route matched. Did you mean: %s', $matchedUrl) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | if (empty($matched)) { |
||
209 | throw new NotFoundException('Service not found with given url'); |
||
210 | } |
||
211 | return $matched; |
||
212 | } |
||
213 | |||
225 | } |