| Conditions | 11 |
| Paths | 60 |
| Total Lines | 84 |
| Code Lines | 52 |
| 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 |
||
| 61 | public function execute(array $params) |
||
| 62 | { |
||
| 63 | $sortByHandler = $this->option('h', false); |
||
| 64 | |||
| 65 | $collection = Services::routes()->loadRoutes(); |
||
| 66 | $methods = [ |
||
| 67 | 'get', |
||
| 68 | 'head', |
||
| 69 | 'post', |
||
| 70 | 'patch', |
||
| 71 | 'put', |
||
| 72 | 'delete', |
||
| 73 | 'options', |
||
| 74 | 'trace', |
||
| 75 | 'connect', |
||
| 76 | 'cli', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | $tbody = []; |
||
| 80 | $uriGenerator = new SampleURIGenerator($collection); |
||
| 81 | $middlewareCollector = new MiddlewareCollector(); |
||
| 82 | |||
| 83 | foreach ($methods as $method) { |
||
| 84 | $routes = $collection->getRoutes($method); |
||
| 85 | |||
| 86 | foreach ($routes as $route => $handler) { |
||
| 87 | if (is_string($handler) || $handler instanceof Closure) { |
||
| 88 | $sampleUri = $uriGenerator->get($route); |
||
| 89 | $filters = $middlewareCollector->get($method, $sampleUri); |
||
| 90 | |||
| 91 | if ($handler instanceof Closure) { |
||
| 92 | $handler = '(Closure)'; |
||
| 93 | } |
||
| 94 | |||
| 95 | $routeName = $collection->getRoutesOptions($route)['as'] ?? '»'; |
||
| 96 | |||
| 97 | |||
| 98 | $tbody[] = [ |
||
| 99 | strtoupper($method), |
||
| 100 | $route, |
||
| 101 | $routeName, |
||
| 102 | $handler, |
||
| 103 | implode(' ', array_map([Helpers::class, 'classBasename'], $filters)), |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($collection->shouldAutoRoute()) { |
||
| 110 | $autoRouteCollector = new AutoRouteCollector( |
||
| 111 | $collection->getDefaultNamespace(), |
||
| 112 | $collection->getDefaultController(), |
||
| 113 | $collection->getDefaultMethod() |
||
| 114 | ); |
||
| 115 | |||
| 116 | $autoRoutes = $autoRouteCollector->get(); |
||
| 117 | |||
| 118 | foreach ($autoRoutes as &$routes) { |
||
| 119 | // Il n'y a pas de méthode "auto", mais il est intentionnel de ne pas obtenir de middlewares de route. |
||
| 120 | $filters = $middlewareCollector->get('auto', $uriGenerator->get($routes[1])); |
||
| 121 | |||
| 122 | $routes[] = implode(' ', array_map([Helpers::class, 'classBasename'], $filters)); |
||
| 123 | } |
||
| 124 | |||
| 125 | $tbody = [...$tbody, ...$autoRoutes]; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Trier par gestionnaire. |
||
| 129 | if ($sortByHandler) { |
||
| 130 | usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3])); |
||
| 131 | } |
||
| 132 | |||
| 133 | $table = []; |
||
| 134 | foreach ($tbody as $route) { |
||
| 135 | $table[] = [ |
||
| 136 | 'Methode' => $route[0], |
||
| 137 | 'Route' => $route[1], |
||
| 138 | 'Nom' => $route[2], |
||
| 139 | $sortByHandler ? 'Gestionnaire ↓' : 'Gestionnaire' => $route[3], |
||
| 140 | 'Middlewares' => $route[4], |
||
| 141 | ]; |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->table($table); |
||
| 145 | } |
||
| 147 |