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 */ |
||
28 | class Micro |
||
29 | { |
||
30 | /** @const string VERSION Version framework */ |
||
31 | const VERSION = '1.1'; |
||
32 | |||
33 | |||
34 | /** @var bool $loaded Micro loaded flag */ |
||
35 | private $loaded; |
||
36 | /** @var bool $debug Debug-mode flag */ |
||
37 | private $debug = true; |
||
38 | /** @var string $environment Application environment */ |
||
39 | private $environment = 'devel'; |
||
40 | /** @var float $startTime Time of start framework */ |
||
41 | private $startTime; |
||
42 | |||
43 | /** @var IContainer $container Container is a container for components and options */ |
||
44 | protected $container; |
||
45 | /** @var string $appDir */ |
||
46 | protected $appDir; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Initialize application |
||
51 | * |
||
52 | * @access public |
||
53 | * |
||
54 | * @param string $environment Application environment: devel , production , test, other |
||
55 | * @param bool $debug Debug-mode flag |
||
56 | * |
||
57 | * @result void |
||
58 | */ |
||
59 | public function __construct($environment = 'devel', $debug = true) |
||
73 | |||
74 | /** |
||
75 | * Clone application |
||
76 | * |
||
77 | * @access public |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function __clone() |
||
90 | |||
91 | /** |
||
92 | * Initialization container |
||
93 | * |
||
94 | * @access protected |
||
95 | * @return void |
||
96 | */ |
||
97 | protected function initializeContainer() |
||
124 | |||
125 | /** |
||
126 | * Running application |
||
127 | * |
||
128 | * @access public |
||
129 | * |
||
130 | * @param IRequest $request Request object |
||
131 | * |
||
132 | * @return Response |
||
133 | * @throws \Exception |
||
134 | */ |
||
135 | public function run(IRequest $request) |
||
158 | |||
159 | /** |
||
160 | * Starting ... |
||
161 | * |
||
162 | * @access private |
||
163 | * |
||
164 | * @return \Micro\web\IResponse |
||
165 | * @throws \Micro\base\Exception |
||
166 | */ |
||
167 | private function doRun() |
||
185 | |||
186 | /** |
||
187 | * Do exception |
||
188 | * |
||
189 | * @access private |
||
190 | * |
||
191 | * @param \Exception $e Exception |
||
192 | * |
||
193 | * @return IOutput |
||
194 | * @throws \Micro\base\Exception |
||
195 | */ |
||
196 | private function doException(\Exception $e) |
||
235 | |||
236 | /** |
||
237 | * Terminate application |
||
238 | * |
||
239 | * @access public |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | public function terminate() |
||
249 | |||
250 | /** |
||
251 | * Add listener on event |
||
252 | * |
||
253 | * @access public |
||
254 | * |
||
255 | * @param string $listener listener name |
||
256 | * @param mixed $event ['Object', 'method'] or callable |
||
257 | * @param int|null $prior priority |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | protected function addListener($listener, $event, $prior = null) |
||
271 | |||
272 | // Methods for components |
||
273 | |||
274 | /** |
||
275 | * Default config path |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | protected function getConfig() |
||
283 | |||
284 | /** |
||
285 | * Get full class name |
||
286 | * @return string |
||
287 | */ |
||
288 | protected function getContainerClass() |
||
292 | |||
293 | /** |
||
294 | * Get resolver |
||
295 | * |
||
296 | * @access protected |
||
297 | * |
||
298 | * @param bool|false $isCli CLI or Web |
||
299 | * |
||
300 | * @return ConsoleResolver|HMVCResolver |
||
301 | */ |
||
302 | protected function getResolver() |
||
310 | |||
311 | /** |
||
312 | * Get status of debug |
||
313 | * |
||
314 | * @access public |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function isDebug() |
||
322 | |||
323 | /** |
||
324 | * Get start time |
||
325 | * |
||
326 | * @access public |
||
327 | * |
||
328 | * @return float|null |
||
329 | */ |
||
330 | public function getStartTime() |
||
334 | |||
335 | // Methods helpers |
||
336 | |||
337 | /** |
||
338 | * Unloader subsystem |
||
339 | * |
||
340 | * @access public |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | public function unloader() |
||
353 | |||
354 | /** |
||
355 | * Get environment name |
||
356 | * |
||
357 | * @access public |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function getEnvironment() |
||
365 | |||
366 | /** |
||
367 | * Get components container |
||
368 | * |
||
369 | * @access public |
||
370 | * |
||
371 | * @return IContainer |
||
372 | */ |
||
373 | public function getContainer() |
||
377 | |||
378 | /** |
||
379 | * Get character set |
||
380 | * |
||
381 | * @access public |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | public function getCharset() |
||
389 | |||
390 | /** |
||
391 | * Get application directory |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getAppDir() |
||
403 | |||
404 | /** |
||
405 | * Get logs directory |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getLogDir() |
||
413 | |||
414 | /** |
||
415 | * Get cache directory |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getCacheDir() |
||
423 | } |
||
424 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: