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 |
||
| 15 | trait TKernel |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * app path |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $path; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * app environment |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $env; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * app config |
||
| 34 | * |
||
| 35 | * @var Config |
||
| 36 | */ |
||
| 37 | protected $config; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * service container |
||
| 41 | * |
||
| 42 | * @var Container |
||
| 43 | */ |
||
| 44 | protected $container; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * monolog logger |
||
| 48 | * |
||
| 49 | * @var Logger |
||
| 50 | */ |
||
| 51 | protected $logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * app request |
||
| 55 | * |
||
| 56 | * @var Request |
||
| 57 | */ |
||
| 58 | protected $req; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * app response |
||
| 62 | * |
||
| 63 | * @var Response |
||
| 64 | */ |
||
| 65 | protected $res; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * app router |
||
| 69 | * |
||
| 70 | * @var Router |
||
| 71 | */ |
||
| 72 | protected $router; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * ctrl class name |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $className; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * controller namespace |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $spacename; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * controller instance |
||
| 90 | * |
||
| 91 | * @var mixed |
||
| 92 | */ |
||
| 93 | protected $controller; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * reflection class instance |
||
| 97 | * |
||
| 98 | * @var ReflectionClass |
||
| 99 | */ |
||
| 100 | protected $reflector; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * controller actions |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $actions; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * controller action |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $action; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * action annotations |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $actionAnnotations; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * middlewares stack |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $middlewares; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * run error |
||
| 132 | * |
||
| 133 | * @var Boolean |
||
| 134 | */ |
||
| 135 | protected $error; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * http status error code |
||
| 139 | * |
||
| 140 | * @var int |
||
| 141 | */ |
||
| 142 | protected $errorCode; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * error message |
||
| 146 | * |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $errorMsg; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * return service container for service name |
||
| 153 | * |
||
| 154 | * @param string $serviceName |
||
| 155 | * @throws Exception |
||
| 156 | * @return object |
||
| 157 | */ |
||
| 158 | 26 | public function getService(string $serviceName) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * init kernel |
||
| 165 | * |
||
| 166 | * @param string $env |
||
| 167 | * @param string $path |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | 2 | protected function init(string $env, string $path) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * execute controller action in middleware core closure |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | 3 | protected function execute(...$args) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * invoke action from a controller an return exec code. |
||
| 218 | * for testing purpose return retValue if false |
||
| 219 | * |
||
| 220 | * @param array $args |
||
|
|
|||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | 1 | protected function invokeAction(...$args) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * return controller instance |
||
| 230 | * |
||
| 231 | * @return mixed |
||
| 232 | */ |
||
| 233 | 3 | protected function getController() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * set middlewares from config then run before/after layers around core |
||
| 240 | * |
||
| 241 | */ |
||
| 242 | 1 | protected function setMiddleware() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * set action annotations for runnig action |
||
| 262 | * |
||
| 263 | */ |
||
| 264 | 1 | protected function setActionAnnotations() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * return action docblock as string |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | 1 | protected function getActionAnnotations(): string |
|
| 287 | |||
| 288 | /** |
||
| 289 | * return true if action is valid |
||
| 290 | * |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | 5 | protected function isValidAction(): bool |
|
| 297 | |||
| 298 | /** |
||
| 299 | * instanciate controller for a classname |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | 4 | protected function setController() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * set relflector on class name |
||
| 310 | * |
||
| 311 | */ |
||
| 312 | 8 | protected function setReflector() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * return reflexion class on current classname |
||
| 319 | * |
||
| 320 | * @return ReflectionClass |
||
| 321 | */ |
||
| 322 | 1 | protected function getReflector(): ReflectionClass |
|
| 326 | |||
| 327 | /** |
||
| 328 | * return core controller action |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 1 | protected function getAction(): string |
|
| 336 | |||
| 337 | /** |
||
| 338 | * set public final actions from controller class name |
||
| 339 | * |
||
| 340 | */ |
||
| 341 | 8 | protected function setActions() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * get final methods for the current classname |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | 1 | protected function getFinalMethods(): array |
|
| 361 | |||
| 362 | /** |
||
| 363 | * return public final actions from controller class name |
||
| 364 | * |
||
| 365 | */ |
||
| 366 | 4 | protected function getActions(): array |
|
| 370 | |||
| 371 | /** |
||
| 372 | * set service container |
||
| 373 | * |
||
| 374 | */ |
||
| 375 | 1 | protected function setContainer() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * get service container |
||
| 388 | * |
||
| 389 | */ |
||
| 390 | 2 | protected function getContainer(): Container |
|
| 394 | |||
| 395 | /** |
||
| 396 | * set request |
||
| 397 | * |
||
| 398 | */ |
||
| 399 | 1 | protected function setRequest() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * return request |
||
| 406 | * |
||
| 407 | */ |
||
| 408 | 1 | protected function getRequest(): Request |
|
| 412 | |||
| 413 | /** |
||
| 414 | * set response |
||
| 415 | * |
||
| 416 | */ |
||
| 417 | 1 | protected function setResponse() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * return reponse |
||
| 424 | * |
||
| 425 | */ |
||
| 426 | 1 | protected function getResponse(): Response |
|
| 430 | |||
| 431 | /** |
||
| 432 | * set router |
||
| 433 | * |
||
| 434 | */ |
||
| 435 | 1 | protected function setRouter() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * return router |
||
| 442 | * |
||
| 443 | */ |
||
| 444 | 1 | protected function getRouter(): Router |
|
| 448 | |||
| 449 | /** |
||
| 450 | * set controller class name |
||
| 451 | * |
||
| 452 | * @param array $routerGroups |
||
| 453 | */ |
||
| 454 | 8 | protected function setClassname(array $routerGroups) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * set controller class name |
||
| 465 | * |
||
| 466 | */ |
||
| 467 | 4 | protected function getClassname(): string |
|
| 471 | |||
| 472 | /** |
||
| 473 | * set app config |
||
| 474 | * |
||
| 475 | */ |
||
| 476 | 1 | protected function setConfig() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * returns config |
||
| 486 | * |
||
| 487 | * @return Config |
||
| 488 | */ |
||
| 489 | 1 | protected function getConfig(): Config |
|
| 493 | |||
| 494 | /** |
||
| 495 | * set app logger |
||
| 496 | * |
||
| 497 | */ |
||
| 498 | 1 | protected function setLogger() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * return monolog logger with handlers set |
||
| 519 | * |
||
| 520 | * @return Logger |
||
| 521 | */ |
||
| 522 | 2 | public function getLogger(): Logger |
|
| 526 | |||
| 527 | /** |
||
| 528 | * set app root path |
||
| 529 | * |
||
| 530 | */ |
||
| 531 | 1 | protected function setPath(string $path) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * return kernel run path |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | 2 | protected function getPath(): string |
|
| 545 | |||
| 546 | /** |
||
| 547 | * set kernel error |
||
| 548 | * |
||
| 549 | */ |
||
| 550 | 2 | protected function setError(bool $error) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * return kernel error |
||
| 557 | * |
||
| 558 | */ |
||
| 559 | 5 | protected function getError(): bool |
|
| 563 | |||
| 564 | /** |
||
| 565 | * return kernel error message |
||
| 566 | * |
||
| 567 | */ |
||
| 568 | 3 | protected function getErrorMsg(): string |
|
| 572 | } |
||
| 573 |