Complex classes like Micro 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 Micro, and based on these observations, apply Extract Interface, too.
| 1 | <?php /** Micro */ |
||
| 32 | class Micro |
||
| 33 | { |
||
| 34 | /** @const string VERSION Version framework */ |
||
| 35 | const VERSION = '1.1'; |
||
| 36 | |||
| 37 | /** @var IContainer $container Container is a container for components and options */ |
||
| 38 | protected $container; |
||
| 39 | /** @var string $appDir */ |
||
| 40 | protected $appDir; |
||
| 41 | /** @var string $webDir */ |
||
| 42 | protected $webDir; |
||
| 43 | |||
| 44 | /** @var bool $loaded Micro loaded flag */ |
||
| 45 | private $loaded; |
||
| 46 | /** @var bool $debug Debug-mode flag */ |
||
| 47 | private $debug = true; |
||
| 48 | /** @var string $environment Application environment */ |
||
| 49 | private $environment = 'devel'; |
||
| 50 | /** @var float $startTime Time of start framework */ |
||
| 51 | private $startTime; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Initialize application |
||
| 56 | * |
||
| 57 | * @access public |
||
| 58 | * |
||
| 59 | * @param string $environment Application environment: devel , production , test, other |
||
| 60 | * @param bool $debug Debug-mode flag |
||
| 61 | * |
||
| 62 | * @result void |
||
| 63 | */ |
||
| 64 | public function __construct($environment = 'devel', $debug = true) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Clone application |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function __clone() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Running application |
||
| 101 | * |
||
| 102 | * @access public |
||
| 103 | * |
||
| 104 | * @param IRequest $request Request object |
||
| 105 | * |
||
| 106 | * @return Response |
||
| 107 | * @throws \Exception |
||
| 108 | */ |
||
| 109 | public function run(IRequest $request) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Starting ... |
||
| 125 | * |
||
| 126 | * @access private |
||
| 127 | * |
||
| 128 | * @param IRequest $request |
||
| 129 | * |
||
| 130 | * @return Web\IResponse|Response|string |
||
| 131 | * @throws \Micro\Base\Exception |
||
| 132 | */ |
||
| 133 | private function doRun(IRequest $request) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Initialization container |
||
| 184 | * |
||
| 185 | * @access protected |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | protected function initializeContainer() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get full class name |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function getContainerClass() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Default config path |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | protected function getConfig() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get application directory |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getAppDir() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get web root directory |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getWebDir() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add listener on event |
||
| 253 | * |
||
| 254 | * @access public |
||
| 255 | * |
||
| 256 | * @param string $listener listener name |
||
| 257 | * @param \Closure $event ['Object', 'method'] or callable |
||
| 258 | * @param int|null $prior priority |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | protected function addListener($listener, $event, $prior = null) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Send signal to dispatcher |
||
| 275 | * |
||
| 276 | * @param string $signal |
||
| 277 | * @param $params |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | protected function sendSignal($signal, $params) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get resolver |
||
| 287 | * |
||
| 288 | * @access protected |
||
| 289 | * |
||
| 290 | * @return IResolver |
||
| 291 | * @throws \Micro\Base\Exception |
||
| 292 | */ |
||
| 293 | protected function getResolver() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Do exception |
||
| 314 | * |
||
| 315 | * @access private |
||
| 316 | * |
||
| 317 | * @param \Exception $e Exception |
||
| 318 | * |
||
| 319 | * @return IOutput |
||
| 320 | * @throws \Micro\base\Exception |
||
| 321 | */ |
||
| 322 | private function doException(\Exception $e) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get start time |
||
| 355 | * |
||
| 356 | * @access public |
||
| 357 | * |
||
| 358 | * @return double |
||
| 359 | */ |
||
| 360 | public function getStartTime() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Terminate application |
||
| 367 | * |
||
| 368 | * @access public |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | public function terminate() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get status of debug |
||
| 379 | * |
||
| 380 | * @access public |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public function isDebug() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get components container |
||
| 391 | * |
||
| 392 | * @access public |
||
| 393 | * |
||
| 394 | * @return IContainer |
||
| 395 | */ |
||
| 396 | public function getContainer() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get character set |
||
| 403 | * |
||
| 404 | * @access public |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getCharset() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get logs directory |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getLogDir() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get cache directory |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getCacheDir() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get environment name |
||
| 435 | * |
||
| 436 | * @access public |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getEnvironment() |
||
| 444 | } |
||
| 445 |