Conditions | 18 |
Paths | 804 |
Total Lines | 89 |
Code Lines | 53 |
Lines | 10 |
Ratio | 11.24 % |
Changes | 5 | ||
Bugs | 2 | 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 |
||
147 | public static function run() |
||
148 | { |
||
149 | $html = null; |
||
150 | // lets try to get html full content to page render |
||
151 | try { |
||
152 | /** @var \Ffcms\Core\Arch\Controller $callClass */ |
||
153 | $callClass = null; |
||
154 | $callMethod = 'action' . self::$Request->getAction(); |
||
155 | |||
156 | // founded callback injection alias |
||
157 | if (self::$Request->getCallbackAlias() !== false) { |
||
158 | $cName = self::$Request->getCallbackAlias(); |
||
159 | View Code Duplication | if (class_exists($cName)) { |
|
160 | $callClass = new $cName; |
||
161 | } else { |
||
162 | throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded'); |
||
163 | } |
||
164 | } else { // typical parsing of native apps |
||
165 | $cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController(); |
||
166 | |||
167 | // try to initialize class object |
||
168 | View Code Duplication | if (class_exists($cName)) { |
|
169 | $callClass = new $cName; |
||
170 | } else { |
||
171 | throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName)); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | // try to call method of founded callback class |
||
176 | if (method_exists($callClass, $callMethod)) { |
||
177 | $actionQuery = []; |
||
178 | // prepare action params for callback |
||
179 | if (!Str::likeEmpty(self::$Request->getID())) { |
||
180 | $actionQuery[] = self::$Request->getID(); |
||
181 | if (!Str::likeEmpty(self::$Request->getAdd())) { |
||
182 | $actionQuery[] = self::$Request->getAdd(); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // get controller method arguments count |
||
187 | $reflection = new \ReflectionMethod($callClass, $callMethod); |
||
188 | $argumentCount = 0; |
||
189 | foreach ($reflection->getParameters() as $arg) { |
||
190 | if (!$arg->isOptional()) { |
||
191 | $argumentCount++; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | // check method arguments count and current request count to prevent warnings |
||
196 | if (count($actionQuery) < $argumentCount) { |
||
197 | throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [ |
||
198 | 'method' => $callMethod, |
||
199 | 'required' => $argumentCount, |
||
200 | 'current' => count($actionQuery) |
||
201 | ])); |
||
202 | } |
||
203 | |||
204 | // make callback call to action in controller and get response |
||
205 | $actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery); |
||
206 | |||
207 | if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) { |
||
208 | // set response to controller property object |
||
209 | $callClass->setResponse($actionResponse); |
||
210 | } |
||
211 | |||
212 | // get full compiled response |
||
213 | $html = $callClass->getOutput(); |
||
214 | } else { |
||
215 | throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"'); |
||
216 | } |
||
217 | } catch (NotFoundException $e) { // catch exceptions and set output |
||
218 | $html = $e->display(); |
||
219 | } catch (ForbiddenException $e) { |
||
220 | $html = $e->display(); |
||
221 | } catch (SyntaxException $e) { |
||
222 | $html = $e->display(); |
||
223 | } catch (JsonException $e) { |
||
224 | $html = $e->display(); |
||
225 | } catch (NativeException $e) { |
||
226 | $html = $e->display(); |
||
227 | } catch (\Exception $e) { // catch all other exceptions |
||
228 | $html = (new NativeException($e->getMessage()))->display(); |
||
229 | } |
||
230 | |||
231 | // set full rendered content to response builder |
||
232 | self::$Response->setContent($html); |
||
233 | // echo full response to user via http foundation |
||
234 | self::$Response->send(); |
||
235 | } |
||
236 | |||
237 | } |