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 FileCache 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 FileCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class FileCache implements CacheInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * 缓存目录 |
||
17 | * @var |
||
18 | */ |
||
19 | private $cache_dir; |
||
20 | |||
21 | /** |
||
22 | * @param string $cache_dir |
||
23 | * @throws \Exception |
||
24 | */ |
||
25 | 12 | public function __construct($cache_dir) |
|
33 | |||
34 | |||
35 | /** |
||
36 | * get value by key, and check if it is expired |
||
37 | * @param string $key |
||
38 | * @param string $default |
||
39 | * @return mixed |
||
40 | */ |
||
41 | 12 | public function get($key, $default = null) |
|
48 | |||
49 | /** |
||
50 | * 添加或覆盖一个key |
||
51 | * @param string $key |
||
52 | * @param mixed $value |
||
53 | * @param int $expire expire time in seconds |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 12 | public function set($key, $value, $expire = 0) |
|
60 | |||
61 | /** |
||
62 | * 设置包含元数据的信息 |
||
63 | * @param $key |
||
64 | * @param $value |
||
65 | * @param $time |
||
66 | * @param $expire |
||
67 | * @return bool |
||
68 | */ |
||
69 | 12 | private function setItem($key, $value, $time, $expire) |
|
82 | |||
83 | /** |
||
84 | * 创建缓存文件 |
||
85 | * @param $key |
||
86 | * @return bool|string |
||
87 | */ |
||
88 | 12 | private function createCacheFile($key) |
|
103 | |||
104 | /** |
||
105 | * 判断Key是否存在 |
||
106 | * @param $key |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function has($key) |
||
116 | |||
117 | /** |
||
118 | * 加法递增 |
||
119 | * @param $key |
||
120 | * @param int $value |
||
121 | * @return mixed |
||
122 | */ |
||
123 | View Code Duplication | public function increment($key, $value = 1) |
|
142 | |||
143 | /** |
||
144 | * 减法递增 |
||
145 | * @param $key |
||
146 | * @param int $value |
||
147 | * @return mixed |
||
148 | */ |
||
149 | View Code Duplication | public function decrement($key, $value = 1) |
|
169 | |||
170 | /** |
||
171 | * 删除一个key,同事会删除缓存文件 |
||
172 | * @param $key |
||
173 | * @return boolean |
||
174 | */ |
||
175 | 12 | public function delete($key) |
|
185 | |||
186 | /** |
||
187 | * 清楚所有缓存 |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 12 | public function flush() |
|
194 | |||
195 | /** |
||
196 | * 递归删除目录 |
||
197 | * @param $dir |
||
198 | * @return bool |
||
199 | */ |
||
200 | 12 | function delTree($dir) |
|
208 | |||
209 | /** |
||
210 | * 根据key获取缓存文件路径 |
||
211 | * |
||
212 | * @param string $key |
||
213 | * @return string |
||
214 | */ |
||
215 | 12 | protected function path($key) |
|
220 | |||
221 | /** |
||
222 | * 获取含有元数据的信息 |
||
223 | * @param $key |
||
224 | * @return bool|mixed|string |
||
225 | */ |
||
226 | 12 | protected function getItem($key) |
|
249 | |||
250 | /** |
||
251 | * 检查key是否过期 |
||
252 | * @param $cache_data |
||
253 | * @return bool |
||
254 | */ |
||
255 | 12 | protected function checkExpire($cache_data) |
|
263 | } |
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.