Complex classes like TKernel 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 TKernel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | trait TKernel |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * app path |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $path; |
||
27 | |||
28 | /** |
||
29 | * app environment |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $env; |
||
34 | |||
35 | /** |
||
36 | * app config |
||
37 | * |
||
38 | * @var Config |
||
39 | */ |
||
40 | protected $config; |
||
41 | |||
42 | /** |
||
43 | * service container |
||
44 | * |
||
45 | * @var Container |
||
46 | */ |
||
47 | protected $container; |
||
48 | |||
49 | /** |
||
50 | * monolog logger |
||
51 | * |
||
52 | * @var Logger |
||
53 | */ |
||
54 | protected $logger; |
||
55 | |||
56 | /** |
||
57 | * app request |
||
58 | * |
||
59 | * @var Request |
||
60 | */ |
||
61 | protected $req; |
||
62 | |||
63 | /** |
||
64 | * app response |
||
65 | * |
||
66 | * @var Response |
||
67 | */ |
||
68 | protected $res; |
||
69 | |||
70 | /** |
||
71 | * app router |
||
72 | * |
||
73 | * @var Router |
||
74 | */ |
||
75 | protected $router; |
||
76 | |||
77 | /** |
||
78 | * app dispatcher |
||
79 | * |
||
80 | * @var Dispatcher |
||
81 | */ |
||
82 | protected $dispatcher; |
||
83 | |||
84 | /** |
||
85 | * ctrl class name |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $className; |
||
90 | |||
91 | /** |
||
92 | * controller namespace |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $spacename; |
||
97 | |||
98 | /** |
||
99 | * controller instance |
||
100 | * |
||
101 | * @var mixed |
||
102 | */ |
||
103 | protected $controller; |
||
104 | |||
105 | /** |
||
106 | * reflection class instance |
||
107 | * |
||
108 | * @var ReflectionClass |
||
109 | */ |
||
110 | protected $reflector; |
||
111 | |||
112 | /** |
||
113 | * controller actions |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $actions; |
||
118 | |||
119 | /** |
||
120 | * controller action |
||
121 | * |
||
122 | * @var string |
||
123 | */ |
||
124 | protected $action; |
||
125 | |||
126 | /** |
||
127 | * action annotations |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $actionAnnotations; |
||
132 | |||
133 | /** |
||
134 | * middlewares stack |
||
135 | * |
||
136 | * @var array |
||
137 | */ |
||
138 | protected $middlewares; |
||
139 | |||
140 | /** |
||
141 | * run error |
||
142 | * |
||
143 | * @var Boolean |
||
144 | */ |
||
145 | protected $error; |
||
146 | |||
147 | /** |
||
148 | * http status error code |
||
149 | * |
||
150 | * @var int |
||
151 | */ |
||
152 | protected $errorCode; |
||
153 | |||
154 | /** |
||
155 | * error message |
||
156 | * |
||
157 | * @var string |
||
158 | */ |
||
159 | protected $errorMsg; |
||
160 | |||
161 | /** |
||
162 | * return service container for service name |
||
163 | * |
||
164 | * @param string $serviceName |
||
165 | * @throws Exception |
||
166 | * @return object |
||
167 | */ |
||
168 | 28 | public function getService(string $serviceName) |
|
172 | |||
173 | /** |
||
174 | * init kernel |
||
175 | * |
||
176 | * @param string $env |
||
177 | * @param string $path |
||
178 | * @return void |
||
179 | */ |
||
180 | 2 | protected function init(string $env, string $path) |
|
200 | |||
201 | /** |
||
202 | * execute controller action in middleware core closure |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | 3 | protected function execute(...$args) |
|
226 | |||
227 | /** |
||
228 | * invoke action from a controller an return exec code. |
||
229 | * for testing purpose return retValue if false |
||
230 | * |
||
231 | * @param array $args |
||
232 | * @return mixed |
||
233 | */ |
||
234 | 1 | protected function invokeAction(...$args) |
|
238 | |||
239 | /** |
||
240 | * return controller instance |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | 3 | protected function getController() |
|
248 | |||
249 | /** |
||
250 | * set middlewares from config then run before/after layers around core |
||
251 | * |
||
252 | */ |
||
253 | 1 | protected function setMiddleware() |
|
270 | |||
271 | /** |
||
272 | * set action annotations for runnig action |
||
273 | * |
||
274 | */ |
||
275 | 1 | protected function setActionAnnotations() |
|
288 | |||
289 | /** |
||
290 | * return action docblock as string |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | 1 | protected function getActionAnnotations(): string |
|
298 | |||
299 | /** |
||
300 | * return true if action is valid |
||
301 | * |
||
302 | * @return boolean |
||
303 | */ |
||
304 | 5 | protected function isValidAction(): bool |
|
308 | |||
309 | /** |
||
310 | * instanciate controller for a classname |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | 4 | protected function setController() |
|
318 | |||
319 | /** |
||
320 | * set relflector on class name |
||
321 | * |
||
322 | */ |
||
323 | 8 | protected function setReflector() |
|
327 | |||
328 | /** |
||
329 | * return reflexion class on current classname |
||
330 | * |
||
331 | * @return ReflectionClass |
||
332 | */ |
||
333 | 1 | protected function getReflector(): ReflectionClass |
|
337 | |||
338 | /** |
||
339 | * return core controller action |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 1 | protected function getAction(): string |
|
347 | |||
348 | /** |
||
349 | * set public final actions from controller class name |
||
350 | * |
||
351 | */ |
||
352 | 8 | protected function setActions() |
|
360 | |||
361 | /** |
||
362 | * get final methods for the current classname |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | 1 | protected function getFinalMethods(): array |
|
372 | |||
373 | /** |
||
374 | * return public final actions from controller class name |
||
375 | * |
||
376 | */ |
||
377 | 4 | protected function getActions(): array |
|
381 | |||
382 | /** |
||
383 | * set service container |
||
384 | * |
||
385 | */ |
||
386 | 1 | protected function setContainer() |
|
396 | |||
397 | /** |
||
398 | * get service container |
||
399 | * |
||
400 | */ |
||
401 | 2 | protected function getContainer(): Container |
|
405 | |||
406 | /** |
||
407 | * set request |
||
408 | * |
||
409 | */ |
||
410 | 1 | protected function setRequest() |
|
414 | |||
415 | /** |
||
416 | * return request |
||
417 | * |
||
418 | */ |
||
419 | 1 | protected function getRequest(): Request |
|
423 | |||
424 | /** |
||
425 | * set response |
||
426 | * |
||
427 | */ |
||
428 | 1 | protected function setResponse() |
|
432 | |||
433 | /** |
||
434 | * return reponse |
||
435 | * |
||
436 | */ |
||
437 | 1 | protected function getResponse(): Response |
|
441 | |||
442 | /** |
||
443 | * set router |
||
444 | * |
||
445 | */ |
||
446 | 1 | protected function setRouter() |
|
450 | |||
451 | /** |
||
452 | * return router |
||
453 | * |
||
454 | */ |
||
455 | 1 | protected function getRouter(): Router |
|
459 | |||
460 | /** |
||
461 | * set controller class name |
||
462 | * |
||
463 | * @param array $routerGroups |
||
464 | */ |
||
465 | 8 | protected function setClassname(array $routerGroups) |
|
473 | |||
474 | /** |
||
475 | * set controller class name |
||
476 | * |
||
477 | */ |
||
478 | 4 | protected function getClassname(): string |
|
482 | |||
483 | /** |
||
484 | * set app config |
||
485 | * |
||
486 | */ |
||
487 | 1 | protected function setConfig() |
|
494 | |||
495 | /** |
||
496 | * returns config |
||
497 | * |
||
498 | * @return Config |
||
499 | */ |
||
500 | 1 | protected function getConfig(): Config |
|
504 | |||
505 | /** |
||
506 | * set app logger |
||
507 | * |
||
508 | */ |
||
509 | 1 | protected function setLogger() |
|
527 | |||
528 | /** |
||
529 | * return monolog logger with handlers set |
||
530 | * |
||
531 | * @return Logger |
||
532 | */ |
||
533 | 2 | public function getLogger(): Logger |
|
537 | |||
538 | |||
539 | |||
540 | /** |
||
541 | * set app root path |
||
542 | * |
||
543 | */ |
||
544 | 1 | protected function setPath(string $path) |
|
548 | |||
549 | /** |
||
550 | * return kernel run path |
||
551 | * |
||
552 | * @return string |
||
553 | */ |
||
554 | 2 | protected function getPath(): string |
|
558 | |||
559 | /** |
||
560 | * set kernel error |
||
561 | * |
||
562 | */ |
||
563 | 2 | protected function setError(bool $error) |
|
567 | |||
568 | /** |
||
569 | * return kernel error |
||
570 | * |
||
571 | */ |
||
572 | 5 | protected function getError(): bool |
|
576 | |||
577 | /** |
||
578 | * return kernel error message |
||
579 | * |
||
580 | */ |
||
581 | 3 | protected function getErrorMsg(): string |
|
585 | } |
||
586 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.