| Conditions | 26 |
| Paths | 7688 |
| Total Lines | 101 |
| Code Lines | 59 |
| 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 |
||
| 283 | private function httpHandler() |
||
| 284 | { |
||
| 285 | // Instantiate Http Router Service |
||
| 286 | $this->services->load(Reactor\Http\Router::class); |
||
| 287 | |||
| 288 | if (profiler() !== false) { |
||
| 289 | profiler()->watch('Parse Router Request'); |
||
| 290 | } |
||
| 291 | router()->parseRequest(); |
||
| 292 | |||
| 293 | // Instantiate Http Middleware Service |
||
| 294 | $this->services->load(Reactor\Http\Middleware::class); |
||
| 295 | |||
| 296 | if (profiler() !== false) { |
||
| 297 | profiler()->watch('Running Middleware Service: Pre Controller'); |
||
| 298 | } |
||
| 299 | middleware()->run(); |
||
| 300 | |||
| 301 | if (false !== ($controller = $this->services->get('controller'))) { |
||
| 302 | if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
||
| 303 | // Autoload Model |
||
| 304 | foreach ($this->modules as $module) { |
||
| 305 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
| 306 | continue; |
||
| 307 | } |
||
| 308 | $module->loadModel(); |
||
| 309 | } |
||
| 310 | |||
| 311 | // Autoload Model |
||
| 312 | $modelClassName = str_replace('Controllers', 'Models', $controller->getName()); |
||
| 313 | |||
| 314 | if (class_exists($modelClassName)) { |
||
| 315 | $this->models->register($modelClassName, 'controller'); |
||
| 316 | } |
||
| 317 | |||
| 318 | // Initialize Controller |
||
| 319 | if (profiler() !== false) { |
||
| 320 | profiler()->watch('Calling Hooks Service: Pre Controller'); |
||
| 321 | } |
||
| 322 | |||
| 323 | if (profiler() !== false) { |
||
| 324 | profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
||
| 325 | } |
||
| 326 | $requestController = $controller->getInstance(); |
||
| 327 | |||
| 328 | if (method_exists($requestController, '__reconstruct')) { |
||
| 329 | $requestController->__reconstruct(); |
||
| 330 | } elseif (method_exists($requestController, 'initialize')) { |
||
| 331 | $requestController->initialize(); |
||
| 332 | } |
||
| 333 | |||
| 334 | $this->services->add($requestController, 'controller'); |
||
| 335 | |||
| 336 | if (profiler() !== false) { |
||
| 337 | profiler()->watch('Calling Middleware Service: Post Controller'); |
||
| 338 | } |
||
| 339 | hooks()->callEvent(Reactor\Services\Hooks::POST_CONTROLLER); |
||
| 340 | |||
| 341 | $requestMethod = $controller->getRequestMethod(); |
||
| 342 | $requestMethodArgs = $controller->getRequestMethodArgs(); |
||
| 343 | |||
| 344 | // Call the requested controller method |
||
| 345 | if (profiler() !== false) { |
||
| 346 | profiler()->watch('Execute Requested Controller Method'); |
||
| 347 | } |
||
| 348 | ob_start(); |
||
| 349 | $requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
||
| 350 | |||
| 351 | if (empty($requestControllerOutput)) { |
||
| 352 | $requestControllerOutput = ob_get_contents(); |
||
| 353 | ob_end_clean(); |
||
| 354 | } |
||
| 355 | |||
| 356 | if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
||
| 357 | // Send default error 204 - No Content |
||
| 358 | output()->sendError(204); |
||
| 359 | } elseif (is_bool($requestControllerOutput)) { |
||
| 360 | if ($requestControllerOutput === true) { |
||
| 361 | output()->sendError(200); |
||
| 362 | } elseif ($requestControllerOutput === false) { |
||
| 363 | output()->sendError(204); |
||
| 364 | } |
||
| 365 | } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
||
| 366 | $requestController->sendPayload($requestControllerOutput); |
||
| 367 | } elseif (is_numeric($requestControllerOutput)) { |
||
| 368 | output()->sendError($requestControllerOutput); |
||
| 369 | } elseif (is_string($requestControllerOutput)) { |
||
| 370 | if (is_json($requestControllerOutput)) { |
||
| 371 | output()->setContentType('application/json'); |
||
| 372 | output()->send($requestControllerOutput); |
||
| 373 | } elseif (is_serialized($requestControllerOutput)) { |
||
| 374 | output()->send($requestControllerOutput); |
||
| 375 | } else { |
||
| 376 | output()->send($requestControllerOutput); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | // Show Error (404) Page Not Found |
||
| 383 | output()->sendError(404); |
||
| 384 | } |
||
| 386 |