Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 */ |
||
33 | class Micro |
||
34 | { |
||
35 | /** @const string VERSION Version framework */ |
||
36 | const VERSION = '1.1'; |
||
37 | |||
38 | /** @var IContainer $container Container is a container for components and options */ |
||
39 | protected $container; |
||
40 | /** @var string $appDir */ |
||
41 | protected $appDir; |
||
42 | /** @var string $webDir */ |
||
43 | protected $webDir; |
||
44 | |||
45 | /** @var bool $loaded Micro loaded flag */ |
||
46 | private $loaded; |
||
47 | /** @var bool $debug Debug-mode flag */ |
||
48 | private $debug = true; |
||
49 | /** @var string $environment Application environment */ |
||
50 | private $environment = 'devel'; |
||
51 | /** @var float $startTime Time of start framework */ |
||
52 | private $startTime; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Initialize application |
||
57 | * |
||
58 | * @access public |
||
59 | * |
||
60 | * @param string $environment Application environment: devel , production , test, other |
||
61 | * @param bool $debug Debug-mode flag |
||
62 | * |
||
63 | * @result void |
||
64 | */ |
||
65 | public function __construct($environment = 'devel', $debug = true) |
||
82 | |||
83 | /** |
||
84 | * Clone application |
||
85 | * |
||
86 | * @access public |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | public function __clone() |
||
99 | |||
100 | /** |
||
101 | * Running application |
||
102 | * |
||
103 | * @access public |
||
104 | * |
||
105 | * @param IRequest $request Request object |
||
106 | * |
||
107 | * @return \Micro\Web\IOutput|\Micro\Web\IResponse |
||
108 | * @throws \Exception |
||
109 | * @throws \Micro\Base\Exception |
||
110 | */ |
||
111 | public function run(IRequest $request) |
||
127 | |||
128 | /** |
||
129 | * Starting ... |
||
130 | * |
||
131 | * @access private |
||
132 | * |
||
133 | * @param IRequest $request |
||
134 | * |
||
135 | * @return \Micro\Web\IResponse|\Micro\Web\IOutput |
||
136 | * @throws \Micro\Base\Exception |
||
137 | */ |
||
138 | private function doRun(IRequest $request) |
||
190 | |||
191 | /** |
||
192 | * Initialization container |
||
193 | * |
||
194 | * @access protected |
||
195 | * @return void |
||
196 | */ |
||
197 | protected function initializeContainer() |
||
218 | |||
219 | /** |
||
220 | * Get full class name |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function getInjectorClass() |
||
227 | |||
228 | /** |
||
229 | * Default config path |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | protected function getConfig() |
||
237 | |||
238 | /** |
||
239 | * Get application directory |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getAppDir() |
||
251 | |||
252 | /** |
||
253 | * Add listener on event |
||
254 | * |
||
255 | * @access public |
||
256 | * |
||
257 | * @param string $listener listener name |
||
258 | * @param \Closure $event ['Object', 'method'] or callable |
||
259 | * @param int|null $prior priority |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | protected function addListener($listener, $event, $prior = null) |
||
275 | |||
276 | /** |
||
277 | * Send signal to dispatcher |
||
278 | * |
||
279 | * @param string $signal |
||
280 | * @param $params |
||
281 | * @return mixed |
||
282 | */ |
||
283 | protected function sendSignal($signal, $params) |
||
291 | |||
292 | /** |
||
293 | * Get resolver |
||
294 | * |
||
295 | * @access protected |
||
296 | * |
||
297 | * @return IResolver |
||
298 | * @throws \Micro\Base\Exception |
||
299 | */ |
||
300 | protected function getResolver() |
||
318 | |||
319 | /** |
||
320 | * Do exception |
||
321 | * |
||
322 | * @access private |
||
323 | * |
||
324 | * @param \Exception $e Exception |
||
325 | * |
||
326 | * @return \Micro\Web\IOutput|\Micro\Web\IResponse |
||
327 | * @throws \Micro\Base\Exception |
||
328 | */ |
||
329 | private function doException(\Exception $e) |
||
362 | |||
363 | /** |
||
364 | * Get web root directory |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getWebDir() |
||
372 | |||
373 | /** |
||
374 | * Get start time |
||
375 | * |
||
376 | * @access public |
||
377 | * |
||
378 | * @return double |
||
379 | */ |
||
380 | public function getStartTime() |
||
384 | |||
385 | /** |
||
386 | * Terminate application |
||
387 | * |
||
388 | * @access public |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | public function terminate() |
||
398 | |||
399 | /** |
||
400 | * Get status of debug |
||
401 | * |
||
402 | * @access public |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function isDebug() |
||
410 | |||
411 | /** |
||
412 | * Get components container |
||
413 | * |
||
414 | * @access public |
||
415 | * |
||
416 | * @return IContainer |
||
417 | */ |
||
418 | public function getContainer() |
||
422 | |||
423 | /** |
||
424 | * Get character set |
||
425 | * |
||
426 | * @access public |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | public function getCharset() |
||
434 | |||
435 | /** |
||
436 | * Get logs directory |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getLogDir() |
||
444 | |||
445 | /** |
||
446 | * Get cache directory |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | public function getCacheDir() |
||
454 | |||
455 | /** |
||
456 | * Get environment name |
||
457 | * |
||
458 | * @access public |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | public function getEnvironment() |
||
466 | } |
||
467 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.