Conditions | 10 |
Paths | 98 |
Total Lines | 98 |
Code Lines | 40 |
Lines | 14 |
Ratio | 14.29 % |
Changes | 15 | ||
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 |
||
70 | public function dispatch(Request $request, Entity $user = null): Response |
||
71 | { |
||
72 | $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $request, $user); |
||
73 | |||
74 | /** |
||
75 | * Figure out what controller to use and what action to call |
||
76 | */ |
||
77 | try { |
||
78 | /** |
||
79 | * @var Router $router |
||
80 | */ |
||
81 | $router = $this->configuration->get('Router'); |
||
82 | |||
83 | list($controllerName, $action, $arguments) = array_values($router->route($request->getPath())); |
||
84 | |||
85 | /** |
||
86 | * Validate and initialize controller |
||
87 | */ |
||
88 | $controller = $controllerFactory->create($controllerName); |
||
89 | } catch (RouteNonexistentException $e) { |
||
90 | /** |
||
91 | * Log nonexistent route (404) |
||
92 | */ |
||
93 | if ($this->logger) { |
||
94 | $this->logger->addWarning('Route not connected', [ |
||
95 | 'path' => $request->getPath() |
||
96 | ]); |
||
97 | } |
||
98 | |||
99 | $controller = $controllerFactory->create(NotFoundController::class); |
||
100 | $action = 'index'; |
||
101 | $arguments = []; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Set arguments |
||
106 | */ |
||
107 | $controller->setArguments($arguments); |
||
108 | |||
109 | /** |
||
110 | * Validate and set controller action |
||
111 | */ |
||
112 | try { |
||
113 | $controller->setAction($action); |
||
114 | } catch (ControllerActionProtectedInsufficientAuthenticationException $e) { |
||
115 | /** |
||
116 | * Log unauthed protected controller action (403) |
||
117 | */ |
||
118 | View Code Duplication | if ($this->logger) { |
|
|
|||
119 | $this->logger->addWarning('Unauthenticated attempt to access protected action', [ |
||
120 | 'path' => $request->getPath(), |
||
121 | 'controller' => $controller->getShortName(), |
||
122 | 'action' => $action |
||
123 | ]); |
||
124 | } |
||
125 | |||
126 | if ($this->configuration->exists('User.SignIn.Controller.Class') && $this->configuration->exists('User.SignIn.Controller.Action')) { |
||
127 | /** |
||
128 | * Save what controller and action was requested and then redirect to sign in form |
||
129 | */ |
||
130 | // @todo test that this works |
||
131 | $request->getCookie()->set('SignIn.onSuccess.path', $request->getPath()); |
||
132 | |||
133 | $controller = $controllerFactory->create($this->configuration->get('User.SignIn.Controller.Class')); |
||
134 | $controller->setAction($this->configuration->get('User.SignIn.Controller.Action')); |
||
135 | } else { |
||
136 | $controller = $controllerFactory->create(NotFoundController::class); |
||
137 | $controller->setAction('index'); |
||
138 | } |
||
139 | } catch (ControllerActionPrivateInsufficientAuthenticationException $e) { |
||
140 | /** |
||
141 | * Log unauthed private controller action (403) |
||
142 | */ |
||
143 | View Code Duplication | if ($this->logger) { |
|
144 | $this->logger->addWarning('Unauthenticated attempt to access private action', [ |
||
145 | 'path' => $request->getPath(), |
||
146 | 'controller' => $controller->getShortName(), |
||
147 | 'action' => $action |
||
148 | ]); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Create response from controller action headers and output |
||
154 | */ |
||
155 | $response = $controller->callAction(); |
||
156 | |||
157 | /** |
||
158 | * Performance logging |
||
159 | */ |
||
160 | if ($this->logger) { |
||
161 | $time = number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2); // @todo $_SERVER usage |
||
162 | |||
163 | $this->logger->addDebug("Dispatched request in $time ms", ['path' => $request->getPath()]); |
||
164 | } |
||
165 | |||
166 | return $response; |
||
167 | } |
||
168 | } |
||
169 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.