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