Conditions | 18 |
Paths | 948 |
Total Lines | 95 |
Code Lines | 57 |
Lines | 10 |
Ratio | 10.53 % |
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 |
||
182 | public function run() |
||
183 | { |
||
184 | $this->startMeasure(__METHOD__); |
||
185 | |||
186 | $html = null; |
||
187 | // lets try to get html full content to page render |
||
188 | try { |
||
189 | /** @var \Ffcms\Core\Arch\Controller $callClass */ |
||
190 | $callClass = null; |
||
191 | $callMethod = 'action' . self::$Request->getAction(); |
||
192 | |||
193 | // founded callback injection alias |
||
194 | if (self::$Request->getCallbackAlias() !== false) { |
||
195 | $cName = self::$Request->getCallbackAlias(); |
||
196 | View Code Duplication | if (class_exists($cName)) { |
|
197 | $callClass = new $cName; |
||
198 | } else { |
||
199 | throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded'); |
||
200 | } |
||
201 | } else { // typical parsing of native apps |
||
202 | $cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController(); |
||
203 | |||
204 | // try to initialize class object |
||
205 | View Code Duplication | if (class_exists($cName)) { |
|
206 | $callClass = new $cName; |
||
207 | } else { |
||
208 | throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName)); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | // try to call method of founded callback class |
||
213 | if (method_exists($callClass, $callMethod)) { |
||
214 | $actionQuery = []; |
||
215 | // prepare action params for callback |
||
216 | if (!Str::likeEmpty(self::$Request->getID())) { |
||
217 | $actionQuery[] = self::$Request->getID(); |
||
218 | if (!Str::likeEmpty(self::$Request->getAdd())) { |
||
219 | $actionQuery[] = self::$Request->getAdd(); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | // get controller method arguments count |
||
224 | $reflection = new \ReflectionMethod($callClass, $callMethod); |
||
225 | $argumentCount = 0; |
||
226 | foreach ($reflection->getParameters() as $arg) { |
||
227 | if (!$arg->isOptional()) { |
||
228 | $argumentCount++; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | // check method arguments count and current request count to prevent warnings |
||
233 | if (count($actionQuery) < $argumentCount) { |
||
234 | throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [ |
||
235 | 'method' => $callMethod, |
||
236 | 'required' => $argumentCount, |
||
237 | 'current' => count($actionQuery) |
||
238 | ])); |
||
239 | } |
||
240 | |||
241 | $this->startMeasure($cName . '::' . $callMethod); |
||
242 | // make callback call to action in controller and get response |
||
243 | $actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery); |
||
244 | $this->stopMeasure($cName . '::' . $callMethod); |
||
245 | |||
246 | if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) { |
||
247 | // set response to controller property object |
||
248 | $callClass->setOutput($actionResponse); |
||
249 | } |
||
250 | |||
251 | // build full compiled output html data |
||
252 | $html = $callClass->buildOutput(); |
||
253 | } else { |
||
254 | throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"'); |
||
255 | } |
||
256 | } catch (NotFoundException $e) { // catch exceptions and set output |
||
257 | $html = $e->display(); |
||
258 | } catch (ForbiddenException $e) { |
||
259 | $html = $e->display(); |
||
260 | } catch (SyntaxException $e) { |
||
261 | $html = $e->display(); |
||
262 | } catch (JsonException $e) { |
||
263 | $html = $e->display(); |
||
264 | } catch (NativeException $e) { |
||
265 | $html = $e->display(); |
||
266 | } catch (\Exception $e) { // catch all other exceptions |
||
267 | $html = (new NativeException($e->getMessage()))->display(); |
||
268 | } |
||
269 | |||
270 | // set full rendered content to response builder |
||
271 | self::$Response->setContent($html); |
||
272 | // echo full response to user via http foundation |
||
273 | self::$Response->send(); |
||
274 | |||
275 | $this->stopMeasure(__METHOD__); |
||
276 | } |
||
277 | |||
278 | } |
If you suppress an error, we recommend checking for the error condition explicitly: