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:
1 | <?php /** Micro */ |
||
29 | class Micro |
||
30 | { |
||
31 | /** @const string VERSION Version framework */ |
||
32 | const VERSION = '1.1'; |
||
33 | |||
34 | |||
35 | /** @var string $environment Application environment */ |
||
36 | protected $environment = 'devel'; |
||
37 | /** @var bool $debug Debug-mode flag */ |
||
38 | protected $debug = true; |
||
39 | /** @var float $startTime Time of start framework */ |
||
40 | protected $startTime; |
||
41 | /** @var bool $loaded Micro loaded flag */ |
||
42 | protected $loaded; |
||
43 | /** @var IContainer $container Container is a container for components and options */ |
||
44 | protected $container; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Initialize framework |
||
49 | * |
||
50 | * @access public |
||
51 | * |
||
52 | * @param string $environment Application environment: devel , prod , test |
||
53 | * @param bool $debug Debug-mode flag |
||
54 | * |
||
55 | * @result void |
||
56 | */ |
||
57 | View Code Duplication | public function __construct($environment = 'devel', $debug = true) |
|
68 | |||
69 | /** |
||
70 | * Clone application |
||
71 | * |
||
72 | * @access public |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | View Code Duplication | public function __clone() |
|
85 | |||
86 | /** |
||
87 | * Default config path |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | protected function getConfig() |
||
95 | |||
96 | /** |
||
97 | * Running application |
||
98 | * |
||
99 | * @access public |
||
100 | * |
||
101 | * @param IRequest $request Request object |
||
102 | * @param string $configPath Path to config file |
||
103 | * |
||
104 | * @return Response |
||
105 | * @throws \Exception |
||
106 | */ |
||
107 | public function run(IRequest $request) |
||
126 | |||
127 | /** |
||
128 | * Boot Loader |
||
129 | * |
||
130 | * @access public |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | public function loader() |
||
144 | |||
145 | /** |
||
146 | * Initialize container |
||
147 | * |
||
148 | * @access public |
||
149 | * |
||
150 | * @param string $configPath Path to configure Container |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | public function initContainer() |
||
165 | |||
166 | /** |
||
167 | * Starting ... |
||
168 | * |
||
169 | * @access public |
||
170 | * |
||
171 | * @return \Micro\web\IResponse |
||
172 | * @throws \Micro\base\Exception |
||
173 | */ |
||
174 | private function doRun() |
||
192 | |||
193 | /** |
||
194 | * Get resolver |
||
195 | * |
||
196 | * @access public |
||
197 | * |
||
198 | * @param bool|false $isCli CLI or Web |
||
199 | * |
||
200 | * @return ConsoleResolver|HMVCResolver |
||
201 | */ |
||
202 | public function getResolver() |
||
210 | |||
211 | // Methods for components |
||
212 | |||
213 | /** |
||
214 | * Do exception |
||
215 | * |
||
216 | * @access private |
||
217 | * |
||
218 | * @param \Exception $e Exception |
||
219 | * |
||
220 | * @return IOutput |
||
221 | * @throws \Micro\base\Exception |
||
222 | */ |
||
223 | private function doException(\Exception $e) |
||
264 | |||
265 | /** |
||
266 | * Terminate application |
||
267 | * |
||
268 | * @access public |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function terminate() |
||
283 | |||
284 | /** |
||
285 | * Get start time |
||
286 | * |
||
287 | * @access public |
||
288 | * |
||
289 | * @return float|null |
||
290 | */ |
||
291 | public function getStartTime() |
||
295 | |||
296 | /** |
||
297 | * Unloader subsystem |
||
298 | * |
||
299 | * @access public |
||
300 | * |
||
301 | * @return void |
||
302 | */ |
||
303 | public function unloader() |
||
312 | |||
313 | // Methods helpers |
||
314 | |||
315 | /** |
||
316 | * Get status of debug |
||
317 | * |
||
318 | * @access public |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function isDebug() |
||
326 | |||
327 | /** |
||
328 | * Get character set |
||
329 | * |
||
330 | * @access public |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getCharset() |
||
338 | |||
339 | /** |
||
340 | * Get environment name |
||
341 | * |
||
342 | * @access public |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getEnvironment() |
||
350 | |||
351 | /** |
||
352 | * Get components container |
||
353 | * |
||
354 | * @access public |
||
355 | * |
||
356 | * @return IContainer |
||
357 | */ |
||
358 | public function getContainer() |
||
362 | |||
363 | public function getAppDir() |
||
367 | } |
||
368 |
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.