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 */ |
||
| 31 | class Micro |
||
| 32 | { |
||
| 33 | /** @const string VERSION Version framework */ |
||
| 34 | const VERSION = '1.1'; |
||
| 35 | |||
| 36 | /** @var IContainer $container Container is a container for components and options */ |
||
| 37 | protected $container; |
||
| 38 | /** @var string $appDir */ |
||
| 39 | protected $appDir; |
||
| 40 | |||
| 41 | /** @var bool $loaded Micro loaded flag */ |
||
| 42 | private $loaded; |
||
| 43 | /** @var bool $debug Debug-mode flag */ |
||
| 44 | private $debug = true; |
||
| 45 | /** @var string $environment Application environment */ |
||
| 46 | private $environment = 'devel'; |
||
| 47 | /** @var float $startTime Time of start framework */ |
||
| 48 | private $startTime; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Initialize application |
||
| 53 | * |
||
| 54 | * @access public |
||
| 55 | * |
||
| 56 | * @param string $environment Application environment: devel , production , test, other |
||
| 57 | * @param bool $debug Debug-mode flag |
||
| 58 | * |
||
| 59 | * @result void |
||
| 60 | */ |
||
| 61 | public function __construct($environment = 'devel', $debug = true) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Clone application |
||
| 78 | * |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function __clone() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Running application |
||
| 95 | * |
||
| 96 | * @access public |
||
| 97 | * |
||
| 98 | * @param IRequest $request Request object |
||
| 99 | * |
||
| 100 | * @return Response |
||
| 101 | * @throws \Exception |
||
| 102 | */ |
||
| 103 | public function run(IRequest $request) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Initialization container |
||
| 119 | * |
||
| 120 | * @access protected |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | protected function initializeContainer() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get full class name |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | protected function getContainerClass() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Default config path |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | protected function getConfig() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get application directory |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getAppDir() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Add listener on event |
||
| 185 | * |
||
| 186 | * @access public |
||
| 187 | * |
||
| 188 | * @param string $listener listener name |
||
| 189 | * @param mixed $event ['Object', 'method'] or callable |
||
| 190 | * @param int|null $prior priority |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | protected function addListener($listener, $event, $prior = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Send signal to dispatcher |
||
| 207 | * |
||
| 208 | * @param $signal |
||
| 209 | * @param $params |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | protected function sendSignal($signal, $params) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get start time |
||
| 219 | * |
||
| 220 | * @access public |
||
| 221 | * |
||
| 222 | * @return float|null |
||
| 223 | */ |
||
| 224 | public function getStartTime() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Starting ... |
||
| 231 | * |
||
| 232 | * @access private |
||
| 233 | * |
||
| 234 | * @param IRequest $request |
||
| 235 | * |
||
| 236 | * @return Web\IResponse|Response|string |
||
| 237 | * @throws \Micro\Base\Exception |
||
| 238 | */ |
||
| 239 | private function doRun(IRequest $request) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get resolver |
||
| 274 | * |
||
| 275 | * @access protected |
||
| 276 | * |
||
| 277 | * @return IResolver |
||
| 278 | * @throws \Micro\Base\Exception |
||
| 279 | */ |
||
| 280 | protected function getResolver() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Do exception |
||
| 301 | * |
||
| 302 | * @access private |
||
| 303 | * |
||
| 304 | * @param \Exception $e Exception |
||
| 305 | * |
||
| 306 | * @return IOutput |
||
| 307 | * @throws \Micro\base\Exception |
||
| 308 | */ |
||
| 309 | private function doException(\Exception $e) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Terminate application |
||
| 351 | * |
||
| 352 | * @access public |
||
| 353 | * |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | public function terminate() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Unloader subsystem |
||
| 365 | * |
||
| 366 | * @access public |
||
| 367 | * |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | public function unloader() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get status of debug |
||
| 382 | * |
||
| 383 | * @access public |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function isDebug() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get components container |
||
| 394 | * |
||
| 395 | * @access public |
||
| 396 | * |
||
| 397 | * @return IContainer |
||
| 398 | */ |
||
| 399 | public function getContainer() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get character set |
||
| 406 | * |
||
| 407 | * @access public |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getCharset() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get logs directory |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getLogDir() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get cache directory |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getCacheDir() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get environment name |
||
| 438 | * |
||
| 439 | * @access public |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function getEnvironment() |
||
| 447 | } |
||
| 448 |