Conditions | 6 |
Paths | 6 |
Total Lines | 59 |
Code Lines | 26 |
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 namespace Comodojo\Dispatcher\Router; |
||
115 | public function compose(Response $response) { |
||
116 | |||
117 | $this->response = $response; |
||
118 | |||
119 | if (is_null($this->route)) { |
||
120 | |||
121 | throw new DispatcherException("Route has not been loaded!"); |
||
122 | |||
123 | } |
||
124 | |||
125 | $service = $this->route->getInstance( |
||
126 | $this->request, |
||
127 | $this->response, |
||
128 | $this->extra |
||
129 | ); |
||
130 | |||
131 | if (!is_null($service)) { |
||
132 | |||
133 | $result; |
||
134 | |||
135 | $method = $this->request->method()->get(); |
||
136 | |||
137 | $methods = $service->getImplementedMethods(); |
||
138 | |||
139 | if ( in_array($method, $methods) ) { |
||
140 | |||
141 | $callable = $service->getMethod($method); |
||
142 | |||
143 | try { |
||
144 | |||
145 | $result = call_user_func(array($service, $callable)); |
||
146 | |||
147 | } catch (DispatcherException $de) { |
||
148 | |||
149 | throw new DispatcherException(sprintf("Service '%s' exception for method '%s': %s", $this->service, $method, $de->getMessage()), 0, $de, 500); |
||
150 | |||
151 | } catch (Exception $e) { |
||
152 | |||
153 | throw new DispatcherException(sprintf("Service '%s' execution failed for method '%s': %s", $this->service, $method, $e->getMessage()), 0, $e, 500); |
||
154 | |||
155 | } |
||
156 | |||
157 | } else { |
||
158 | |||
159 | throw new DispatcherException(sprintf("Service '%s' doesn't implement method '%s'", $this->service, $method), 0, null, 501, array( |
||
160 | "Allow" => implode(",", $methods) |
||
161 | )); |
||
162 | |||
163 | } |
||
164 | |||
165 | $this->response->content()->set($result); |
||
166 | |||
167 | } else { |
||
168 | |||
169 | throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->service), 0, null, 500); |
||
170 | |||
171 | } |
||
172 | |||
173 | } |
||
174 | |||
200 |
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: