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