Conditions | 11 |
Paths | 170 |
Total Lines | 68 |
Code Lines | 41 |
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 |
||
183 | final public function run(): void |
||
184 | { |
||
185 | $html = null; |
||
186 | // lets try to get html full content to page render |
||
187 | try { |
||
188 | $this->startMeasure(__METHOD__ . '::callback'); |
||
189 | /** @var \Ffcms\Core\Arch\Controller $callClass */ |
||
190 | $callClass = null; |
||
191 | $callMethod = 'action' . self::$Request->getAction(); |
||
192 | |||
193 | // define callback class namespace/name full path |
||
194 | $cName = (self::$Request->getCallbackAlias() ?? '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController()); |
||
195 | if (!class_exists($cName)) |
||
196 | throw new NotFoundException('Callback class not found: ' . App::$Security->strip_tags($cName)); |
||
197 | |||
198 | $callClass = new $cName; |
||
199 | // check if callback method (action) is exist in class object |
||
200 | if (!method_exists($callClass, $callMethod)) |
||
201 | throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"'); |
||
202 | |||
203 | $this->stopMeasure(__METHOD__ . '::callback'); |
||
204 | $params = []; |
||
205 | if (!Str::likeEmpty(self::$Request->getID())) { |
||
206 | $params[] = self::$Request->getID(); |
||
207 | if (!Str::likeEmpty(self::$Request->getAdd())) |
||
208 | $params[] = self::$Request->getAdd(); |
||
209 | } |
||
210 | |||
211 | // get instance of callback object (class::method) as reflection |
||
212 | $instance = new \ReflectionMethod($callClass, $callMethod); |
||
213 | $argCount = 0; |
||
214 | // calculate method defined arguments count |
||
215 | foreach ($instance->getParameters() as $arg) { |
||
216 | if (!$arg->isOptional()) |
||
217 | $argCount++; |
||
218 | } |
||
219 | // compare method arg count with passed |
||
220 | if (count($params) < $argCount) |
||
221 | throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [ |
||
222 | 'method' => $callMethod, |
||
223 | 'required' => $argCount, |
||
224 | 'current' => count($params) |
||
225 | ])); |
||
226 | |||
227 | $this->startMeasure($cName . '::' . $callMethod); |
||
228 | // make callback call to action in controller and get response |
||
229 | $actionResponse = call_user_func_array([$callClass, $callMethod], $params); |
||
230 | $this->stopMeasure($cName . '::' . $callMethod); |
||
231 | |||
232 | // set response to controller attribute |
||
233 | if (!Str::likeEmpty($actionResponse)) |
||
234 | $callClass->setOutput($actionResponse); |
||
235 | |||
236 | // build full compiled output html data with default layout and widgets |
||
237 | $html = $callClass->buildOutput(); |
||
238 | } catch (\Exception $e) { |
||
239 | // check if exception is system-based throw |
||
240 | if ($e instanceof TemplateException) |
||
241 | $html = $e->display(); |
||
242 | else // or hook exception to system based :))) |
||
243 | $html = (new NativeException($e->getMessage()))->display(); |
||
244 | } |
||
245 | |||
246 | // set full rendered content to response builder |
||
247 | self::$Response->setContent($html); |
||
248 | // echo full response to user via symfony http foundation |
||
249 | self::$Response->send(); |
||
250 | } |
||
251 | |||
252 | } |
If you suppress an error, we recommend checking for the error condition explicitly: