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