| Conditions | 13 |
| Paths | 14 |
| Total Lines | 70 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 52 | public function beforeDispatch(/** @scrutinizer ignore-unused */ Event $event, Dispatcher $dispatcher): bool |
||
| 53 | { |
||
| 54 | // Check if user is authenticated |
||
| 55 | $isAuthenticated = $this->checkUserAuth() || $this->isLocalHostRequest(); |
||
| 56 | |||
| 57 | // Get the controller and action names |
||
| 58 | $controller = $dispatcher->getControllerName(); |
||
| 59 | $action = $dispatcher->getActionName(); |
||
| 60 | |||
| 61 | // Redirect to login page if user is not authenticated and the controller is not "session" |
||
| 62 | if (!$isAuthenticated && strtoupper($controller) !== 'SESSION') { |
||
| 63 | // Return a 403 response for AJAX requests |
||
| 64 | if ($this->request->isAjax()) { |
||
| 65 | $this->response->setStatusCode(403, 'Forbidden')->setContent('This user is not authorized')->send(); |
||
| 66 | } else { |
||
| 67 | // Redirect to login page for normal requests |
||
| 68 | $dispatcher->forward([ |
||
| 69 | 'controller' => 'session', |
||
| 70 | 'action' => 'index', |
||
| 71 | 'module' => 'admin-cabinet', |
||
| 72 | 'namespace' => 'MikoPBX\AdminCabinet\Controllers' |
||
| 73 | ]); |
||
| 74 | } |
||
| 75 | |||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Check if the authenticated user is allowed to access the requested controller and action |
||
| 80 | if ($isAuthenticated) { |
||
| 81 | // Check if the desired controller exists or show the extensions page |
||
| 82 | $controllerClass = $this->dispatcher->getHandlerClass(); |
||
|
|
|||
| 83 | if (!class_exists($controllerClass) |
||
| 84 | || (strtoupper($controller) === 'SESSION' && strtoupper($action) !== 'END')) { |
||
| 85 | // Redirect to home page if controller does not set or user logged in but still on session page |
||
| 86 | $homePath = $this->session->get(SessionController::SESSION_ID)[SessionController::HOME_PAGE]; |
||
| 87 | if (empty($homePath)){ |
||
| 88 | $dispatcher->forward([ |
||
| 89 | 'module' => 'admin-cabinet', |
||
| 90 | 'controller' => 'errors', |
||
| 91 | 'action' => 'show404', |
||
| 92 | 'namespace' => 'MikoPBX\AdminCabinet\Controllers' |
||
| 93 | ]); |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | $module = explode('/', $homePath)[1]; |
||
| 97 | $controller = explode('/', $homePath)[2]; |
||
| 98 | $action = explode('/', $homePath)[3]; |
||
| 99 | $dispatcher->forward([ |
||
| 100 | 'module' => $module, |
||
| 101 | 'controller' => $controller, |
||
| 102 | 'action' => $action |
||
| 103 | ]); |
||
| 104 | return true; |
||
| 105 | } |
||
| 106 | if (!$this->isLocalHostRequest() |
||
| 107 | && !$this->isAllowedAction($controllerClass, $action) |
||
| 108 | && !in_array(strtoupper($controller), ['ERRORS','SESSION']) |
||
| 109 | ) { |
||
| 110 | // Show a 401 error if not allowed |
||
| 111 | $dispatcher->forward([ |
||
| 112 | 'module' => 'admin-cabinet', |
||
| 113 | 'controller' => 'errors', |
||
| 114 | 'action' => 'show401', |
||
| 115 | 'namespace' => 'MikoPBX\AdminCabinet\Controllers' |
||
| 116 | ]); |
||
| 117 | return true; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return true; |
||
| 122 | } |
||
| 203 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.