Conditions | 12 |
Paths | 40 |
Total Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
117 | public function resolve($route, $req, $res, array $args) |
||
118 | { |
||
119 | $result = false; |
||
120 | if (is_array($route) || is_string($route)) { |
||
121 | // method name and controller supplied |
||
122 | if (is_string($route) && $req->params('controller')) { |
||
123 | $route = [$req->params('controller'), $route]; |
||
124 | // method name supplied |
||
125 | } elseif (is_string($route)) { |
||
126 | $route = [$this->defaultController, $route]; |
||
127 | // no method name? fallback to the index() method |
||
128 | } elseif (count($route) == 1) { |
||
129 | $route[] = $this->defaultAction; |
||
130 | } |
||
131 | |||
132 | list($controller, $method) = $route; |
||
133 | |||
134 | $controller = $this->namespace.'\\'.$controller; |
||
135 | |||
136 | if (!class_exists($controller)) { |
||
137 | throw new \Exception("Controller does not exist: $controller"); |
||
138 | } |
||
139 | |||
140 | $controllerObj = new $controller(); |
||
141 | |||
142 | // give the controller access to the DI container |
||
143 | if (method_exists($controllerObj, 'setApp')) { |
||
144 | $controllerObj->setApp($this->app); |
||
145 | } |
||
146 | |||
147 | // collect any preset route parameters |
||
148 | if (isset($route[2])) { |
||
149 | $params = $route[2]; |
||
150 | $req->setParams($params); |
||
151 | } |
||
152 | |||
153 | $result = $controllerObj->$method($req, $res, $args); |
||
154 | } elseif (is_callable($route)) { |
||
155 | $result = call_user_func($route, $req, $res, $args); |
||
156 | } |
||
157 | |||
158 | if ($result instanceof View) { |
||
159 | $res->render($result); |
||
160 | } |
||
161 | |||
162 | return $res; |
||
163 | } |
||
164 | } |
||
165 |