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