Complex classes like Kernel 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Kernel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Kernel implements KernelContract |
||
16 | { |
||
17 | /** |
||
18 | * The application implementation. |
||
19 | * |
||
20 | * @var \Illuminate\Contracts\Foundation\Application |
||
21 | */ |
||
22 | protected $app; |
||
23 | |||
24 | /** |
||
25 | * The router instance. |
||
26 | * |
||
27 | * @var \Illuminate\Routing\Router |
||
28 | */ |
||
29 | protected $router; |
||
30 | |||
31 | /** |
||
32 | * The bootstrap classes for the application. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $bootstrappers = []; |
||
37 | |||
38 | /** |
||
39 | * The application's middleware stack. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $middleware = []; |
||
44 | |||
45 | /** |
||
46 | * The application's route middleware groups. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $middlewareGroups = []; |
||
51 | |||
52 | /** |
||
53 | * The application's route middleware. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $routeMiddleware = []; |
||
58 | |||
59 | /** |
||
60 | * The priority-sorted list of middleware. |
||
61 | * |
||
62 | * Forces non-global middleware to always be in the given order. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $middlewarePriority = [ |
||
67 | \Illuminate\Session\Middleware\StartSession::class, |
||
68 | \Illuminate\View\Middleware\ShareErrorsFromSession::class, |
||
69 | \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class, |
||
70 | \Illuminate\Routing\Middleware\ThrottleRequests::class, |
||
71 | \Illuminate\Session\Middleware\AuthenticateSession::class, |
||
72 | \Illuminate\Routing\Middleware\SubstituteBindings::class, |
||
73 | \Illuminate\Auth\Middleware\Authorize::class, |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * Create a new HTTP kernel instance. |
||
78 | * |
||
79 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
80 | * @param \Illuminate\Routing\Router $router |
||
81 | * |
||
82 | * @return void |
||
|
|||
83 | */ |
||
84 | public function __construct(Container $app, Router $router) |
||
91 | |||
92 | /** |
||
93 | * Handle an incoming HTTP request. |
||
94 | * |
||
95 | * @param \Illuminate\Http\Request $request |
||
96 | * |
||
97 | * @return \Illuminate\Http\Response |
||
98 | */ |
||
99 | public function handle($request) |
||
117 | |||
118 | /** |
||
119 | * Send the given request through the middleware / router. |
||
120 | * |
||
121 | * @param \Illuminate\Http\Request $request |
||
122 | * |
||
123 | * @return \Illuminate\Http\Response |
||
124 | */ |
||
125 | protected function sendRequestThroughRouter($request) |
||
139 | |||
140 | /** |
||
141 | * Bootstrap the application for HTTP requests. |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function bootstrap() |
||
151 | |||
152 | /** |
||
153 | * Get the route dispatcher callback. |
||
154 | * |
||
155 | * @return \Closure |
||
156 | */ |
||
157 | protected function dispatchToRouter() |
||
165 | |||
166 | /** |
||
167 | * Call the terminate method on any terminable middleware. |
||
168 | * |
||
169 | * @param \Illuminate\Http\Request $request |
||
170 | * @param \Illuminate\Http\Response $response |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | public function terminate($request, $response) |
||
180 | |||
181 | /** |
||
182 | * Call the terminate method on any terminable middleware. |
||
183 | * |
||
184 | * @param \Illuminate\Http\Request $request |
||
185 | * @param \Illuminate\Http\Response $response |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | protected function terminateMiddleware($request, $response) |
||
210 | |||
211 | /** |
||
212 | * Gather the route middleware for the given request. |
||
213 | * |
||
214 | * @param \Illuminate\Http\Request $request |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | protected function gatherRouteMiddleware($request) |
||
226 | |||
227 | /** |
||
228 | * Parse a middleware string to get the name and parameters. |
||
229 | * |
||
230 | * @param string $middleware |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | protected function parseMiddleware($middleware) |
||
244 | |||
245 | /** |
||
246 | * Determine if the kernel has a given middleware. |
||
247 | * |
||
248 | * @param string $middleware |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function hasMiddleware($middleware) |
||
256 | |||
257 | /** |
||
258 | * Add a new middleware to beginning of the stack if it does not already exist. |
||
259 | * |
||
260 | * @param string $middleware |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function prependMiddleware($middleware) |
||
272 | |||
273 | /** |
||
274 | * Add a new middleware to end of the stack if it does not already exist. |
||
275 | * |
||
276 | * @param string $middleware |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function pushMiddleware($middleware) |
||
288 | |||
289 | /** |
||
290 | * Prepend the given middleware to the given middleware group. |
||
291 | * |
||
292 | * @param string $group |
||
293 | * @param string $middleware |
||
294 | * |
||
295 | * @throws \InvalidArgumentException |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function prependMiddlewareToGroup($group, $middleware) |
||
313 | |||
314 | /** |
||
315 | * Append the given middleware to the given middleware group. |
||
316 | * |
||
317 | * @param string $group |
||
318 | * @param string $middleware |
||
319 | * |
||
320 | * @throws \InvalidArgumentException |
||
321 | * |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function appendMiddlewareToGroup($group, $middleware) |
||
338 | |||
339 | /** |
||
340 | * Prepend the given middleware to the middleware priority list. |
||
341 | * |
||
342 | * @param string $middleware |
||
343 | * |
||
344 | * @return $this |
||
345 | */ |
||
346 | public function prependToMiddlewarePriority($middleware) |
||
356 | |||
357 | /** |
||
358 | * Append the given middleware to the middleware priority list. |
||
359 | * |
||
360 | * @param string $middleware |
||
361 | * |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function appendToMiddlewarePriority($middleware) |
||
374 | |||
375 | /** |
||
376 | * Sync the current state of the middleware to the router. |
||
377 | * |
||
378 | * @return void |
||
379 | */ |
||
380 | protected function syncMiddlewareToRouter() |
||
392 | |||
393 | /** |
||
394 | * Get the bootstrap classes for the application. |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | protected function bootstrappers() |
||
402 | |||
403 | /** |
||
404 | * Report the exception to the exception handler. |
||
405 | * |
||
406 | * @param \Throwable $e |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | protected function reportException(Throwable $e) |
||
414 | |||
415 | /** |
||
416 | * Render the exception to a response. |
||
417 | * |
||
418 | * @param \Illuminate\Http\Request $request |
||
419 | * @param \Throwable $e |
||
420 | * |
||
421 | * @return \Symfony\Component\HttpFoundation\Response |
||
422 | */ |
||
423 | protected function renderException($request, Throwable $e) |
||
427 | |||
428 | /** |
||
429 | * Get the application's route middleware groups. |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | public function getMiddlewareGroups() |
||
437 | |||
438 | /** |
||
439 | * Get the application's route middleware. |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | public function getRouteMiddleware() |
||
447 | |||
448 | /** |
||
449 | * Get the Laravel application instance. |
||
450 | * |
||
451 | * @return \Illuminate\Contracts\Foundation\Application |
||
452 | */ |
||
453 | public function getApplication() |
||
457 | } |
||
458 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.