Conditions | 9 |
Paths | 30 |
Total Lines | 70 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 3 |
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 |
||
151 | public function router(SystemInterface &$core, &$result) |
||
152 | { |
||
153 | //elapsed('Start routing'); |
||
154 | // Flag for matching SamsonPHP asynchronous requests |
||
155 | $async = $this->isAsynchronousRequest(); |
||
156 | // Get HTTP request path |
||
157 | $path = $this->requestURI;//$_SERVER['REQUEST_URI']; |
||
158 | // Get HTTP request method |
||
159 | $method = $_SERVER['REQUEST_METHOD']; |
||
160 | // Prepend HTTP request type, true - asynchronous |
||
161 | $method = ($async ? GenericRouteGenerator::ASYNC_PREFIX : '').$method; |
||
162 | |||
163 | $result = false; |
||
164 | |||
165 | // Remove first slash if present, add method to path, remove GET params, remove trailing slash |
||
166 | $path = rtrim(strtok(ltrim($path, '/'), '?'), '/'); |
||
167 | |||
168 | /** @var mixed $routeMetadata Dispatching result route metadata */ |
||
169 | if (is_array($routeMetadata = call_user_func(Core::ROUTING_LOGIC_FUNCTION, $path, $method))) { |
||
170 | // Get callback info |
||
171 | list($module, $method) = explode("#", $routeMetadata[2]); |
||
172 | // Get module |
||
173 | $module = $core->module($module); |
||
174 | // Create callback |
||
175 | $callback = array($module, $method); |
||
176 | |||
177 | // Check if we have valid callback |
||
178 | if (is_callable($callback)) { |
||
179 | // Routing result |
||
180 | $result = call_user_func_array( |
||
181 | $callback, |
||
182 | $this->parseParameters($callback, $routeMetadata[1]) |
||
183 | ); |
||
184 | |||
185 | // Get object from callback and set it as current active core module |
||
186 | $core->active($module); |
||
187 | |||
188 | // If this is cached method |
||
189 | if (stripos($method, self::CACHE_PREFIX) !== false) { |
||
190 | // perform caching |
||
191 | $core->cached(); |
||
192 | } |
||
193 | |||
194 | // If this route is asynchronous |
||
195 | if ($async) { |
||
196 | // Set async response |
||
197 | $core->async(true); |
||
198 | |||
199 | // If controller action has failed |
||
200 | if (!isset($result['status']) || !$result['status']) { |
||
201 | $result['message'] = "\n" . 'Event failed: ' . $routeMetadata[0]; |
||
202 | $result['status'] = 0; |
||
203 | } |
||
204 | |||
205 | // Encode event result as json object |
||
206 | echo json_encode($result, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); |
||
207 | |||
208 | // Mark as successful |
||
209 | $result = true; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | // If no result is passed - consider success |
||
214 | $result = $result !== false ? true : $result; |
||
215 | } |
||
216 | |||
217 | //elapsed('Finished routing'); |
||
218 | // Return true or false depending on $result |
||
219 | return $result; |
||
220 | } |
||
221 | } |
||
222 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: