| Conditions | 8 |
| Paths | 42 |
| Total Lines | 55 |
| Code Lines | 29 |
| 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 |
||
| 35 | public function __invoke(ViewController $view, string $name, array $parameters = [], $absolute = false) |
||
| 36 | { |
||
| 37 | /** @var Config $config */ |
||
| 38 | $config = ServiceLocator::instance()->get(Config::class); |
||
| 39 | $routes = $config->get('routes'); |
||
| 40 | |||
| 41 | try { |
||
| 42 | $apiRoutes = $config->get('routes:rest'); |
||
| 43 | } catch (ConfigInvalidException $e) { |
||
| 44 | $apiRoutes = []; |
||
| 45 | } |
||
| 46 | |||
| 47 | $routes = array_merge($routes, $apiRoutes); |
||
| 48 | $path = ''; |
||
| 49 | |||
| 50 | foreach ($routes as $routeName => $data) { |
||
| 51 | |||
| 52 | if ($routeName === $name) { |
||
| 53 | $path = preg_replace('|/\((.*)\)|', '', $data['path']); |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | |||
| 57 | } |
||
| 58 | |||
| 59 | if (empty($path)) { |
||
| 60 | throw new RouteInvalidException('No route for name "' . $name . '" found'); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!empty($parameters)) { |
||
| 64 | |||
| 65 | if (in_array('query', array_keys($parameters), true)) { |
||
| 66 | $query = $parameters['query']; |
||
| 67 | $query = http_build_query($query); |
||
| 68 | $path = $path . '?' . $query; |
||
| 69 | } else { |
||
| 70 | $path = $path . '/' . implode('/', $parameters); |
||
| 71 | } |
||
| 72 | |||
| 73 | } |
||
| 74 | |||
| 75 | $path = str_replace('//' , '/', $path); |
||
| 76 | |||
| 77 | if ($absolute) { |
||
| 78 | |||
| 79 | /** @var Request $request */ |
||
| 80 | $request = $this->getServiceLocator()->get(RequestService::class); |
||
| 81 | |||
| 82 | $path = $request->getScheme() |
||
| 83 | . $request->getHost() |
||
| 84 | . $path; |
||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | return $path; |
||
| 89 | } |
||
| 90 | |||
| 91 | } |