Total Complexity | 40 |
Total Lines | 342 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HttpApplication often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HttpApplication, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
32 | class HttpApplication extends Application implements |
||
33 | ControllerAggregator, |
||
34 | MiddlewareAggregator, |
||
35 | MiddlewareInterface, |
||
36 | RequestHandlerInterface, |
||
37 | OnRequestListener |
||
38 | { |
||
39 | /** |
||
40 | * @var Router |
||
41 | */ |
||
42 | private $router; |
||
43 | |||
44 | /** |
||
45 | * @var string[]|MiddlewareInterface[] |
||
46 | */ |
||
47 | private $middleware = []; |
||
48 | |||
49 | /** |
||
50 | * @var MiddlewarePipe |
||
51 | */ |
||
52 | private $pipeline; |
||
53 | |||
54 | /** |
||
55 | * @var EmitterInterface |
||
56 | */ |
||
57 | private $emitter; |
||
58 | |||
59 | /** |
||
60 | * Application constructor. |
||
61 | * |
||
62 | * @param ContainerInterface|null $container |
||
63 | */ |
||
64 | public function __construct(ContainerInterface $container = null) |
||
65 | { |
||
66 | parent::__construct($container); |
||
67 | |||
68 | if ($this->getContainer()->has(Router::class)) { |
||
69 | $this->router = $this->getContainer()->get(Router::class); |
||
70 | } else { |
||
71 | $this->router = new GenericRouter(); |
||
72 | } |
||
73 | |||
74 | if ($this->getContainer()->has(EmitterInterface::class)) { |
||
75 | $this->emitter = $this->getContainer()->get(EmitterInterface::class); |
||
76 | } else { |
||
77 | $this->emitter = new SapiEmitter(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * While testing call this method before handle method. |
||
83 | */ |
||
84 | public function startup(): void |
||
85 | { |
||
86 | $this->handleOnBootListeners(); |
||
87 | $this->initialize(); |
||
88 | $this->handleOnRunListeners(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * While testing, call this method after handle method. |
||
93 | */ |
||
94 | public function shutdown(): void |
||
95 | { |
||
96 | $this->handleOnShutDownListeners(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Startups and run application with/or without dedicated server. |
||
101 | * Once application is run it will listen to incoming http requests, |
||
102 | * and takes care of the entire request flow process. |
||
103 | * |
||
104 | * @param HttpServer|null $server |
||
105 | */ |
||
106 | public function run(HttpServer $server = null): void |
||
107 | { |
||
108 | $this->startup(); |
||
109 | if ($server) { |
||
110 | $server->addListener($this); |
||
111 | $server->start(); |
||
112 | } else { |
||
113 | $response = $this->handle(ServerRequest::fromGlobals()); |
||
114 | $this->emitter->emit($response); |
||
115 | if ($response instanceof Response) { |
||
116 | $response->end(); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | $this->shutdown(); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Registers PSR-15 compatible middelware. |
||
125 | * Middleware can be either callable object which accepts PSR-7 server request interface and returns |
||
126 | * response interface, or just class name that implements psr-15 middleware or its instance. |
||
127 | * |
||
128 | * @param MiddlewareInterface|callable $middleware |
||
129 | */ |
||
130 | public function use($middleware): void |
||
144 | } |
||
145 | |||
146 | public function register($controller, Route $route = null): void |
||
147 | { |
||
148 | if (is_callable($controller) && $route !== null) { |
||
149 | $route = $route->withController($controller); |
||
150 | $this->router->add($route); |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | if ($controller instanceof Controller) { |
||
155 | /** @var Route $route */ |
||
156 | $route = $controller::getRoute(); |
||
157 | $route = $route->withController($controller); |
||
158 | $this->router->add($route); |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | if (is_string($controller) && is_subclass_of($controller, Controller::class)) { |
||
163 | /** @var Route $route */ |
||
164 | $route = $controller::getRoute(); |
||
165 | $route = $route->withController($controller); |
||
166 | $this->router->add($route); |
||
167 | return; |
||
168 | } |
||
169 | |||
170 | throw ApplicationException::forInvalidController($controller); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Handles request flow process. |
||
175 | * |
||
176 | * @see MiddlewareInterface::process() |
||
177 | * |
||
178 | * @param ServerRequestInterface $request |
||
179 | * @param RequestHandlerInterface $next |
||
180 | * @return ResponseInterface |
||
181 | */ |
||
182 | public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
||
183 | { |
||
184 | /** @var Route $route */ |
||
185 | $route = $this->router->find( |
||
186 | $request->getMethod(), |
||
187 | $request->getUri()->getPath() |
||
188 | ); |
||
189 | |||
190 | $controller = $route->getController(); |
||
191 | |||
192 | if ($request instanceof ServerRequest) { |
||
193 | $request = $request->withAttributes($route->getAttributes()); |
||
194 | } |
||
195 | |||
196 | if (is_string($controller) && |
||
197 | class_exists($controller) && |
||
198 | is_subclass_of($controller, Controller::class) |
||
199 | ) { |
||
200 | /** @var Controller $instance */ |
||
201 | $instance = $this->resolver->resolve($controller); |
||
202 | return $instance($request); |
||
203 | } |
||
204 | |||
205 | if (is_callable($controller)) { |
||
206 | $response = $controller($request); |
||
207 | if (!$response instanceof ResponseInterface) { |
||
208 | throw ControllerException::forInvalidReturnValue(); |
||
209 | } |
||
210 | |||
211 | return $response; |
||
212 | } |
||
213 | |||
214 | throw ControllerException::forMissingController($route->getPath()); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Runs application listeners and handles request flow process. |
||
219 | * |
||
220 | * @param ServerRequestInterface $request |
||
221 | * @return ResponseInterface |
||
222 | */ |
||
223 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
224 | { |
||
225 | $response = $this->getMiddlewarePipe()->handle($request); |
||
226 | |||
227 | return $response; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Decorator for handle method, used by server instance. |
||
232 | * @see Application::handle() |
||
233 | * @see Server::addListener() |
||
234 | * |
||
235 | * @param ResponseInterface $response |
||
236 | * @param Client $client |
||
237 | * @param ServerRequestInterface $request |
||
238 | * @return ResponseInterface |
||
239 | */ |
||
240 | public function onRequest(Client $client, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
||
241 | { |
||
242 | return $this->handle($request); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Registers new controller that accepts get request |
||
247 | * when request uri matches passed route pattern. |
||
248 | * |
||
249 | * @param string $route |
||
250 | * @param callable $controller |
||
251 | */ |
||
252 | public function get(string $route, callable $controller): void |
||
253 | { |
||
254 | $this->register($controller, Route::get($route)); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Registers new controller that accepts post request |
||
259 | * when request uri matches passed route pattern. |
||
260 | * |
||
261 | * @param string $route |
||
262 | * @param callable $controller |
||
263 | */ |
||
264 | public function post(string $route, callable $controller): void |
||
265 | { |
||
266 | $this->register($controller, Route::post($route)); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Registers new controller that accepts put request |
||
271 | * when request uri matches passed route pattern. |
||
272 | * |
||
273 | * @param string $route |
||
274 | * @param callable $controller |
||
275 | */ |
||
276 | public function put(string $route, callable $controller): void |
||
277 | { |
||
278 | $this->register($controller, Route::put($route)); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Registers new controller that accepts patch request |
||
283 | * when request uri matches passed route pattern. |
||
284 | * |
||
285 | * @param string $route |
||
286 | * @param callable $controller |
||
287 | */ |
||
288 | public function patch(string $route, callable $controller): void |
||
289 | { |
||
290 | $this->register($controller, Route::patch($route)); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Registers new controller that accepts delete request |
||
295 | * when request uri matches passed route pattern. |
||
296 | * |
||
297 | * @param string $route |
||
298 | * @param callable $controller |
||
299 | */ |
||
300 | public function delete(string $route, callable $controller): void |
||
301 | { |
||
302 | $this->register($controller, Route::delete($route)); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Registers new controller that accepts options request |
||
307 | * when request uri matches passed route pattern. |
||
308 | * |
||
309 | * @param string $route |
||
310 | * @param callable $controller |
||
311 | */ |
||
312 | public function options(string $route, callable $controller): void |
||
313 | { |
||
314 | $this->register($controller, Route::options($route)); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Registers new controller that accepts head request |
||
319 | * when request uri matches passed route pattern. |
||
320 | * |
||
321 | * @param string $route |
||
322 | * @param callable $controller |
||
323 | */ |
||
324 | public function head(string $route, callable $controller): void |
||
325 | { |
||
326 | $this->register($controller, Route::head($route)); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Registers new controller that listens on the passed route. |
||
331 | * |
||
332 | * @param Route $route |
||
333 | * @param callable $controller |
||
334 | */ |
||
335 | public function on(Route $route, callable $controller): void |
||
336 | { |
||
337 | $this->register($controller, $route); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Returns application's controller aggregate. |
||
342 | * |
||
343 | * @return ControllerAggregator |
||
344 | */ |
||
345 | public function getControllerAggregator(): ControllerAggregator |
||
348 | } |
||
349 | |||
350 | protected function getMiddlewarePipe(): MiddlewarePipe |
||
351 | { |
||
352 | if ($this->pipeline) { |
||
353 | return $this->pipeline; |
||
354 | } |
||
355 | |||
356 | return $this->pipeline = $this->composeMiddlewarePipe(); |
||
357 | } |
||
358 | |||
359 | private function composeMiddlewarePipe(): MiddlewarePipe |
||
374 | } |
||
375 | } |
||
376 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: