| Conditions | 7 |
| Paths | 7 |
| Total Lines | 65 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | 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 namespace Comodojo\Dispatcher\Router; |
||
| 131 | public function compose(Response $response) { |
||
| 132 | |||
| 133 | $this->response = $response; |
||
| 134 | |||
| 135 | if (is_null($this->route)) { |
||
| 136 | |||
| 137 | throw new DispatcherException("Route has not been loaded!"); |
||
| 138 | |||
| 139 | } |
||
| 140 | |||
| 141 | if ( $this->bypass_service ) { |
||
| 142 | |||
| 143 | return; |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | $service = $this->route->getInstance( |
||
| 148 | $this->request, |
||
| 149 | $this->response, |
||
| 150 | $this->extra |
||
| 151 | ); |
||
| 152 | |||
| 153 | if (!is_null($service)) { |
||
| 154 | |||
| 155 | $result = ""; |
||
| 156 | |||
| 157 | $method = $this->request->method()->get(); |
||
| 158 | |||
| 159 | $methods = $service->getImplementedMethods(); |
||
| 160 | |||
| 161 | if ( in_array($method, $methods) ) { |
||
| 162 | |||
| 163 | $callable = $service->getMethod($method); |
||
| 164 | |||
| 165 | try { |
||
| 166 | |||
| 167 | $result = call_user_func(array($service, $callable)); |
||
| 168 | |||
| 169 | } catch (DispatcherException $de) { |
||
| 170 | |||
| 171 | throw new DispatcherException(sprintf("Service '%s' exception for method '%s': %s", $this->service, $method, $de->getMessage()), 0, $de, 500); |
||
| 172 | |||
| 173 | } catch (Exception $e) { |
||
| 174 | |||
| 175 | throw new DispatcherException(sprintf("Service '%s' execution failed for method '%s': %s", $this->service, $method, $e->getMessage()), 0, $e, 500); |
||
| 176 | |||
| 177 | } |
||
| 178 | |||
| 179 | } else { |
||
| 180 | |||
| 181 | throw new DispatcherException(sprintf("Service '%s' doesn't implement method '%s'", $this->route->getServiceName(), $method), 0, null, 501, array( |
||
| 182 | "Allow" => implode(",", $methods) |
||
| 183 | )); |
||
| 184 | |||
| 185 | } |
||
| 186 | |||
| 187 | $this->response->content()->set($result); |
||
| 188 | |||
| 189 | } else { |
||
| 190 | |||
| 191 | throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->service), 0, null, 500); |
||
| 192 | |||
| 193 | } |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 222 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: