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 |
||
35 | abstract class Cache extends Fwolflib { |
||
|
|||
36 | |||
37 | |||
38 | /** |
||
39 | * Compute path of a key's data file |
||
40 | * |
||
41 | * @param string $key |
||
42 | * @return string |
||
43 | */ |
||
44 | View Code Duplication | public function CacheFilePath($key) { |
|
45 | $s_path = $this->aCfg['cache-file-dir']; |
||
46 | |||
47 | $ar_rule = str_split($this->aCfg['cache-file-rule'], 2); |
||
48 | if (empty($ar_rule)) |
||
49 | return $s_path; |
||
50 | |||
51 | foreach ($ar_rule as $rule) |
||
52 | $s_path .= $this->CacheFilePathSec($rule, $key) . '/'; |
||
53 | |||
54 | // Filename |
||
55 | $s_path .= $this->CacheFilePathFile($key); |
||
56 | |||
57 | return $s_path; |
||
58 | } // end of func Path |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Compute name of a key's data file |
||
63 | * |
||
64 | * @param string $key |
||
65 | * @return string |
||
66 | */ |
||
67 | protected function CacheFilePathFile($key) { |
||
68 | return substr(md5($key), 0, 8); |
||
69 | } // end of func CacheFilePathFile |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Compute path of a key by a single rule section |
||
74 | * |
||
75 | * @param string $rule |
||
76 | * @param string $key |
||
77 | * @return string |
||
78 | * @see $sCacheRule |
||
79 | */ |
||
80 | View Code Duplication | protected function CacheFilePathSec($rule, $key) { |
|
81 | $i_len = 2; |
||
82 | |||
83 | if ($i_len > strlen($rule)) |
||
84 | return ''; |
||
85 | |||
86 | $i = intval($rule{1}); |
||
87 | if (1 == $rule{0}) { |
||
88 | // md5 from start |
||
89 | $i_start = $i_len * $i; |
||
90 | $s_seed = md5($key); |
||
91 | } elseif (2 == $rule{0}) { |
||
92 | // md5 from end |
||
93 | $i_start = -1 * $i_len * ($i + 1); |
||
94 | $s_seed = md5($key); |
||
95 | } elseif (3 == $rule{0}) { |
||
96 | // raw from start |
||
97 | $i_start = $i_len * $i; |
||
98 | $s_seed = $key; |
||
99 | } elseif (4 == $rule{0}) { |
||
100 | // raw from end |
||
101 | $i_start = -1 * $i_len * ($i + 1); |
||
102 | $s_seed = $key; |
||
103 | } elseif (5 == $rule{0}) { |
||
104 | // crc32 |
||
105 | if (3 < $i) |
||
106 | $i = $i % 3; |
||
107 | $i_start = $i_len * $i; |
||
108 | $s_seed = hash('crc32', $key); |
||
109 | } |
||
110 | return(substr($s_seed, $i_start, 2)); |
||
111 | } // end of func CacheFilePathSec |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Gen and write cache data file |
||
116 | * |
||
117 | * @param string $key |
||
118 | * @return mixed // Generated cache data. |
||
119 | */ |
||
120 | protected function CacheGen($key) { |
||
121 | $val = $this->CacheGenVal($key); |
||
122 | $this->CacheSet($key, $val); |
||
123 | return $val; |
||
124 | } // end of func CacheGen |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Gen cache data, implement by child class |
||
129 | * |
||
130 | * @param string $key |
||
131 | * @return string |
||
132 | */ |
||
133 | abstract protected function CacheGenVal($key); |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Load cache data |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @param int $flag May used by type func. |
||
141 | * @return mixed |
||
142 | */ |
||
143 | View Code Duplication | public function CacheGet($key, $flag) { |
|
144 | // Error check |
||
145 | if (empty($this->aCfg['cache-type'])) { |
||
146 | $this->Log('Cache type is not set.', 5); |
||
147 | return NULL; |
||
148 | } |
||
149 | |||
150 | $s = 'CacheGet' . ucfirst($this->aCfg['cache-type']); |
||
151 | if (method_exists($this, $s)) |
||
152 | return $this->{$s}($key, $flag); |
||
153 | else { |
||
154 | $this->Log('Cache get method for type ' |
||
155 | . $this->aCfg['cache-type'] . ' not implement.', 5); |
||
156 | return NULL; |
||
157 | } |
||
158 | } // end of func CacheGet |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Read cache file and return value |
||
163 | * |
||
164 | * @param string $key |
||
165 | * @param int $flag Which type value shoud I return ? |
||
166 | * 0=string, 1=array, 2=object |
||
167 | * 3=raw string |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function CacheGetFile($key, $flag = 0) { |
||
171 | if ($this->CacheNeedUpdate($key)) |
||
172 | return $this->CacheGen($key); |
||
173 | else { |
||
174 | // Read from file and parse it. |
||
175 | $s_file = $this->CacheFilePath($key); |
||
176 | $s_cache = file_get_contents($s_file); |
||
177 | |||
178 | $rs = null; |
||
179 | switch ($flag) { |
||
180 | case 0: |
||
181 | $rs = json_decode($s_cache, true); |
||
182 | if (is_array($rs)) |
||
183 | $rs = $rs[0]; |
||
184 | break; |
||
185 | case 1: |
||
186 | $rs = json_decode($s_cache, true); |
||
187 | break; |
||
188 | case 2: |
||
189 | $rs = json_decode($s_cache, false); |
||
190 | break; |
||
191 | case 3: |
||
192 | default: |
||
193 | $rs = &$s_cache; |
||
194 | } |
||
195 | return $rs; |
||
196 | } |
||
197 | } // end of func CacheGetFile |
||
198 | |||
199 | |||
200 | /** |
||
201 | * CacheLifetime of cache data, meature by second |
||
202 | * |
||
203 | * @param string $key |
||
204 | * @return int |
||
205 | */ |
||
206 | abstract public function CacheLifetime($key); |
||
207 | |||
208 | |||
209 | /** |
||
210 | * Is cache data file need update/create ? |
||
211 | * |
||
212 | * @param string $key |
||
213 | * @return boolean |
||
214 | */ |
||
215 | protected function CacheNeedUpdate($key) { |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Write data to cache |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param mixed $val |
||
236 | * @return $this |
||
237 | */ |
||
238 | View Code Duplication | public function CacheSet ($key, $val) { |
|
255 | |||
256 | |||
257 | /** |
||
258 | * Write data to cache, type file |
||
259 | * |
||
260 | * @param string $key |
||
261 | * @param mixed $val |
||
262 | * @return $this |
||
263 | */ |
||
264 | View Code Duplication | public function CacheSetFile ($key, $val) { |
|
277 | |||
278 | |||
279 | /** |
||
280 | * Check config/cache store dir valid and writable |
||
281 | * If error, return error msg, else return empty str. |
||
282 | * |
||
283 | * @param string $dir |
||
284 | * @return string |
||
285 | */ |
||
286 | public function ChkCfgFileDir($dir) { |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Check cache rule exist and valid |
||
309 | * If error, return error msg, else return empty str. |
||
310 | * |
||
311 | * @param string $rule |
||
312 | * @return string |
||
313 | */ |
||
314 | View Code Duplication | public function ChkCfgFileRule($rule) { |
|
323 | |||
324 | |||
325 | /** |
||
326 | * Set default config |
||
327 | * |
||
328 | * @return this |
||
329 | */ |
||
330 | View Code Duplication | protected function SetCfgDefault () { |
|
354 | |||
355 | |||
356 | /** |
||
357 | * Init treatment |
||
358 | */ |
||
359 | public function Init () { |
||
374 | |||
375 | |||
376 | } // end of class Cache |
||
377 | |||
379 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.