Conditions | 7 |
Paths | 9 |
Total Lines | 89 |
Code Lines | 45 |
Lines | 0 |
Ratio | 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 |
||
91 | public function process( |
||
92 | RequestInterface $request, |
||
93 | ResponseInterface $response, |
||
94 | RequestContextInterface $requestContext, |
||
95 | $hook |
||
96 | ) { |
||
97 | |||
98 | // if false hook is coming do nothing |
||
99 | if (ModuleHooks::REQUEST_POST !== $hook) { |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | // check if we are the handler that has to process this request |
||
104 | if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | // load the application associated with this request |
||
109 | $application = $this->findRequestedApplication($requestContext); |
||
110 | $application->registerClassLoaders(); |
||
111 | |||
112 | // check if the application has already been connected |
||
113 | if ($application->isConnected() === false) { |
||
114 | throw new \Exception(sprintf('Application %s has not connected yet', $application->getName()), 503); |
||
115 | } |
||
116 | |||
117 | // create a copy of the valve instances |
||
118 | $valves = $this->valves; |
||
119 | $handlers = $this->handlers; |
||
120 | |||
121 | // create a new request instance from the HTTP request |
||
122 | $servletRequest = new Request(); |
||
123 | $servletRequest->injectHandlers($handlers); |
||
124 | $servletRequest->injectHttpRequest($request); |
||
125 | $servletRequest->injectServerVars($requestContext->getServerVars()); |
||
126 | $servletRequest->init(); |
||
127 | |||
128 | // initialize servlet response |
||
129 | $servletResponse = new Response(); |
||
130 | $servletResponse->init(); |
||
131 | |||
132 | // load the session and the authentication manager |
||
133 | $sessionManager = $application->search(SessionManagerInterface::IDENTIFIER); |
||
134 | $authenticationManager = $application->search(AuthenticationManagerInterface::IDENTIFIER); |
||
135 | |||
136 | // inject the sapplication and servlet response |
||
137 | $servletRequest->injectContext($application); |
||
138 | $servletRequest->injectResponse($servletResponse); |
||
139 | $servletRequest->injectSessionManager($sessionManager); |
||
140 | $servletRequest->injectAuthenticationManager($authenticationManager); |
||
141 | |||
142 | // prepare the request instance |
||
143 | $servletRequest->prepare(); |
||
144 | |||
145 | // initialize static request and application context |
||
146 | RequestHandler::$requestContext = $servletRequest; |
||
147 | RequestHandler::$applicationContext = $application; |
||
148 | |||
149 | // process the valves |
||
150 | foreach ($valves as $valve) { |
||
151 | $valve->invoke($servletRequest, $servletResponse); |
||
152 | if ($servletRequest->isDispatched() === true) { |
||
153 | break; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // copy response values to the HTTP response |
||
158 | $response->setState($servletResponse->getState()); |
||
159 | $response->setVersion($servletResponse->getVersion()); |
||
160 | $response->setStatusCode($servletResponse->getStatusCode()); |
||
161 | $response->setStatusReasonPhrase($servletResponse->getStatusReasonPhrase()); |
||
162 | |||
163 | // copy the body content to the HTTP response |
||
164 | $response->appendBodyStream($servletResponse->getBodyStream()); |
||
165 | |||
166 | // copy headers to the HTTP response |
||
167 | foreach ($servletResponse->getHeaders() as $headerName => $headerValue) { |
||
168 | $response->addHeader($headerName, $headerValue); |
||
169 | } |
||
170 | |||
171 | // copy cookies to the HTTP response |
||
172 | $response->setCookies($servletResponse->getCookies()); |
||
173 | |||
174 | // append the servlet engine's signature |
||
175 | $response->addHeader(Protocol::HEADER_X_POWERED_BY, get_class($this), true); |
||
176 | |||
177 | // set response state to be dispatched after this without calling other modules process |
||
178 | $response->setState(HttpResponseStates::DISPATCH); |
||
179 | } |
||
180 | } |
||
181 |
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.