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 |
||
9 | trait EasyCacheTrait |
||
10 | { |
||
11 | /** |
||
12 | * default expire time (min) |
||
13 | * @var int |
||
14 | */ |
||
15 | private $default_expire = 60; |
||
16 | |||
17 | /** |
||
18 | * cache through __call |
||
19 | * @param $method |
||
20 | * @param $parameters |
||
21 | * @return mixed |
||
22 | */ |
||
23 | public function __call($method, $parameters) |
||
49 | |||
50 | /** |
||
51 | * get expire time by reflection |
||
52 | * @param $method |
||
53 | * @return int |
||
54 | */ |
||
55 | private function getExpire($method) |
||
66 | |||
67 | /** |
||
68 | * delete all cache from the class |
||
69 | * @return bool |
||
70 | */ |
||
71 | View Code Duplication | public function forgetCache() |
|
81 | |||
82 | /** |
||
83 | * delete all cache from method |
||
84 | * @param $method |
||
85 | * @return bool |
||
86 | */ |
||
87 | View Code Duplication | public function forgetMethodCache($method){ |
|
96 | |||
97 | /** |
||
98 | * get cache level1 prefix |
||
99 | * @return string |
||
100 | */ |
||
101 | private static function _getCacheKeyPrefixLevel1(){ |
||
104 | |||
105 | /** |
||
106 | * get cache level2 prefix |
||
107 | * @param $method |
||
108 | * @return string |
||
109 | */ |
||
110 | private static function _getCacheKeyPrefixLevel2($method){ |
||
113 | |||
114 | /** |
||
115 | * get cache key |
||
116 | * @param $method |
||
117 | * @param $params |
||
118 | * @return string |
||
119 | */ |
||
120 | private static function _getCacheKey($method, $params){ |
||
123 | |||
124 | |||
125 | } |
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.