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 |
||
27 | class Fwolflib { |
||
28 | |||
29 | /** |
||
30 | * Configuation |
||
31 | * |
||
32 | * Notice: re-define var in sub-class will overwrite var in parent class. |
||
33 | * @var array |
||
34 | */ |
||
35 | public $aCfg = array(); |
||
36 | |||
37 | /** |
||
38 | * Default time format |
||
39 | * @var string |
||
40 | */ |
||
41 | public $sFormatTime = 'Y-m-d H:i:s'; |
||
42 | |||
43 | /** |
||
44 | * Log msg. |
||
45 | * |
||
46 | * Log level 5(or 4) can be used as error or warning. |
||
47 | * |
||
48 | * array( |
||
49 | * i => array( |
||
50 | * level => 1/notice, 3/common, 5/imortant |
||
51 | * Can also be 2 or 4, even 1.5(not suggested), |
||
52 | * Default is 3. |
||
53 | * time => |
||
54 | * msg => |
||
55 | * ) |
||
56 | * ) |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | public $aLog = array(); |
||
61 | |||
62 | |||
63 | /** |
||
64 | * constructor |
||
65 | * |
||
66 | * @param array $ar_cfg |
||
67 | */ |
||
68 | public function __construct ($ar_cfg = array()) { |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Dummy destructor |
||
78 | */ |
||
79 | public function __destruct () { |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Auto new obj if not set, for some special var only |
||
85 | * |
||
86 | * @param string $name |
||
87 | * @return object |
||
88 | */ |
||
89 | View Code Duplication | public function __get ($name) { |
|
101 | |||
102 | |||
103 | /** |
||
104 | * Init func |
||
105 | * Do things according default then user config. |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | public function Init () { |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Record log msg. |
||
116 | * |
||
117 | * @param string $msg |
||
118 | * @param int $level |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function Log ($msg, $level = 3) { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Get all log msg. |
||
156 | * |
||
157 | * @param $level Only output log which's level >= $level |
||
158 | * @return string |
||
159 | */ |
||
160 | public function LogGet ($level = 3) { |
||
176 | |||
177 | |||
178 | /** |
||
179 | * Clear all rules |
||
180 | * |
||
181 | * @param boolean $b_init Re-do init. |
||
182 | */ |
||
183 | public function Reset ($b_init = false) { |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Set config array |
||
193 | * |
||
194 | * @see $aCfg |
||
195 | * @param array $k |
||
196 | * @param mixed $v |
||
197 | * @return this |
||
198 | */ |
||
199 | View Code Duplication | public function SetCfg ($k, $v = null) { |
|
213 | |||
214 | |||
215 | /** |
||
216 | * Set default config. |
||
217 | * |
||
218 | * @return object |
||
219 | */ |
||
220 | protected function SetCfgDefault () { |
||
228 | |||
229 | |||
230 | } // end of class Fwolflib |
||
231 | ?> |
||
232 |
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.