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 IInjector $injector */ |
||
38 | protected $injector; |
||
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() |
||
97 | |||
98 | /** |
||
99 | * Running application |
||
100 | * |
||
101 | * @access public |
||
102 | * |
||
103 | * @param IRequest $request Request object |
||
104 | * |
||
105 | * @return \Micro\Web\IOutput|\Micro\Web\IResponse |
||
106 | * @throws \Exception |
||
107 | * @throws \Micro\Base\Exception |
||
108 | */ |
||
109 | public function run(IRequest $request) |
||
125 | |||
126 | /** |
||
127 | * Starting ... |
||
128 | * |
||
129 | * @access private |
||
130 | * |
||
131 | * @param IRequest $request |
||
132 | * |
||
133 | * @return \Micro\Web\IResponse|\Micro\Web\IOutput |
||
134 | * @throws \Micro\Base\Exception |
||
135 | */ |
||
136 | private function doRun(IRequest $request) |
||
186 | |||
187 | /** |
||
188 | * Initialization |
||
189 | * |
||
190 | * @access protected |
||
191 | * @return void |
||
192 | */ |
||
193 | protected function initialize() |
||
214 | |||
215 | /** |
||
216 | * Get full class name |
||
217 | * @return string |
||
218 | */ |
||
219 | protected function getInjectorClass() |
||
223 | |||
224 | /** |
||
225 | * Default config path |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | protected function getConfig() |
||
233 | |||
234 | /** |
||
235 | * Get application directory |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getAppDir() |
||
247 | |||
248 | /** |
||
249 | * Add listener on event |
||
250 | * |
||
251 | * @access public |
||
252 | * |
||
253 | * @param string $listener listener name |
||
254 | * @param \Closure $event ['Object', 'method'] or callable |
||
255 | * @param int|null $prior priority |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | protected function addListener($listener, $event, $prior = null) |
||
271 | |||
272 | /** |
||
273 | * Get status of debug |
||
274 | * |
||
275 | * @access public |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isDebug() |
||
283 | |||
284 | /** |
||
285 | * Get start time |
||
286 | * |
||
287 | * @access public |
||
288 | * |
||
289 | * @return double |
||
290 | */ |
||
291 | public function getStartTime() |
||
295 | |||
296 | /** |
||
297 | * Send signal to dispatcher |
||
298 | * |
||
299 | * @param string $signal |
||
300 | * @param $params |
||
301 | * @return mixed |
||
302 | */ |
||
303 | protected function sendSignal($signal, $params) |
||
311 | |||
312 | /** |
||
313 | * Get resolver |
||
314 | * |
||
315 | * @access protected |
||
316 | * |
||
317 | * @return IResolver |
||
318 | * @throws \Micro\Base\Exception |
||
319 | */ |
||
320 | protected function getResolver() |
||
341 | |||
342 | /** |
||
343 | * Do exception |
||
344 | * |
||
345 | * @access private |
||
346 | * |
||
347 | * @param \Exception $e Exception |
||
348 | * |
||
349 | * @return \Micro\Web\IOutput|\Micro\Web\IResponse |
||
350 | * @throws \Micro\Base\Exception |
||
351 | */ |
||
352 | private function doException(\Exception $e) |
||
385 | |||
386 | /** |
||
387 | * Get web root directory |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public function getWebDir() |
||
395 | |||
396 | /** |
||
397 | * Terminate application |
||
398 | * |
||
399 | * @access public |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | public function terminate() |
||
409 | |||
410 | /** |
||
411 | * Get character set |
||
412 | * |
||
413 | * @access public |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getCharset() |
||
421 | |||
422 | /** |
||
423 | * Get logs directory |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getLogDir() |
||
431 | |||
432 | /** |
||
433 | * Get cache directory |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getCacheDir() |
||
441 | |||
442 | /** |
||
443 | * Get environment name |
||
444 | * |
||
445 | * @access public |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getEnvironment() |
||
453 | } |
||
454 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: