Conditions | 10 |
Paths | 20 |
Total Lines | 59 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
42 | public function in(Request $request) |
||
43 | { |
||
44 | // Get the controller |
||
45 | $controllerSpec = $request->getParameter('_controller'); |
||
46 | |||
47 | if (!$controllerSpec) { |
||
48 | throw DispatcherException::notTagged( |
||
49 | $request->getMethod(), |
||
50 | $request->getUri() ? $request->getUri()->getPath() : '' |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | $controller = $this->container->get($controllerSpec['class']); |
||
55 | |||
56 | if (!method_exists($controller, $controllerSpec['method'])) { |
||
57 | throw DispatcherException::controllerActionDoesNotExist( |
||
58 | $controllerSpec['method'], |
||
59 | $controllerSpec['class'] |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | // Prepare to run the action method |
||
64 | $actionMethod = new \ReflectionMethod($controller, $controllerSpec['method']); |
||
65 | $parameters = $actionMethod->getParameters(); |
||
66 | $passedArgs = array(); |
||
67 | |||
68 | foreach ($parameters as $parameter) { |
||
69 | |||
70 | $hintedClass = $parameter->getClass(); |
||
71 | $parameterName = $parameter->getName(); |
||
|
|||
72 | |||
73 | if ($hintedClass) { |
||
74 | $hintedClass = $hintedClass->getName(); |
||
75 | } |
||
76 | |||
77 | // Special case - should the Request object be passed here? |
||
78 | if ($parameterName == 'request' && $hintedClass == 'Veto\HTTP\Request') { |
||
79 | $passedArgs[] = $request; |
||
80 | } |
||
81 | |||
82 | // Should a request parameter be passed here? |
||
83 | if ($request->hasParameter($parameterName)) { |
||
84 | $passedArgs[] = $request->getParameter($parameterName); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | $response = $actionMethod->invokeArgs($controller, $passedArgs); |
||
89 | |||
90 | // By the end of the inbound layer list, a response should have been obtained |
||
91 | if (!$response instanceof Response) { |
||
92 | throw DispatcherException::controllerActionDidNotReturnResponse( |
||
93 | $controllerSpec['method'], |
||
94 | $controllerSpec['class'], |
||
95 | gettype($response) |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | return $response; |
||
100 | } |
||
101 | } |
||
102 |