| Conditions | 13 |
| Paths | 78 |
| Total Lines | 79 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 94 | public function __invoke($route = NULL) |
||
| 95 | { |
||
| 96 | $error_handler = $this->container->get('error-handler'); |
||
| 97 | $controller_name = AnimeClient::DEFAULT_CONTROLLER; |
||
| 98 | $action_method = AnimeClient::NOT_FOUND_METHOD; |
||
| 99 | $params = []; |
||
| 100 | |||
| 101 | if (is_null($route)) |
||
| 102 | { |
||
| 103 | $route = $this->get_route(); |
||
| 104 | $error_handler->addDataTable('route_args', (array)$route); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ( ! $route) |
||
| 108 | { |
||
| 109 | $failure = $this->router->getFailedRoute(); |
||
| 110 | $error_handler->addDataTable('failed_route', (array)$failure); |
||
| 111 | $action_method = AnimeClient::ERROR_MESSAGE_METHOD; |
||
| 112 | |||
| 113 | switch(TRUE) |
||
| 114 | { |
||
| 115 | case $failure->failedMethod(): |
||
| 116 | $params['title'] = '405 Method Not Allowed'; |
||
| 117 | $params['message'] = 'Invalid HTTP Verb'; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case $failure->failedAccept(): |
||
| 121 | $params['title'] = '406 Not Acceptable'; |
||
| 122 | $params['message'] = 'Unacceptable content type'; |
||
| 123 | break; |
||
| 124 | |||
| 125 | default: |
||
| 126 | $action_method = AnimeClient::NOT_FOUND_METHOD; |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | else |
||
| 131 | { |
||
| 132 | if (isset($route->params['controller'])) |
||
| 133 | { |
||
| 134 | $controller_name = $route->params['controller']; |
||
| 135 | } |
||
| 136 | |||
| 137 | if (isset($route->params['action'])) |
||
| 138 | { |
||
| 139 | $action_method = $route->params['action']; |
||
| 140 | } |
||
| 141 | |||
| 142 | if (is_null($controller_name)) |
||
| 143 | { |
||
| 144 | throw new \LogicException("Missing controller"); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (strpos($controller_name, '\\') === FALSE) |
||
| 148 | { |
||
| 149 | $map = $this->get_controller_list(); |
||
| 150 | $controller_name = $map[$controller_name]; |
||
| 151 | } |
||
| 152 | |||
| 153 | $params = (isset($route->params['params'])) ? $route->params['params'] : []; |
||
| 154 | |||
| 155 | if ( ! empty($route->tokens)) |
||
| 156 | { |
||
| 157 | foreach ($route->tokens as $key => $v) |
||
| 158 | { |
||
| 159 | if (array_key_exists($key, $route->params)) |
||
| 160 | { |
||
| 161 | $params[$key] = $route->params[$key]; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | $controller = new $controller_name($this->container); |
||
| 168 | |||
| 169 | // Run the appropriate controller method |
||
| 170 | $error_handler->addDataTable('controller_args', $params); |
||
| 171 | call_user_func_array([$controller, $action_method], $params); |
||
| 172 | } |
||
| 173 | |||
| 277 | // End of Dispatcher.php |