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 namespace Comodojo\Cache\Drivers; |
||
23 | class FilesystemXattr extends AbstractDriver { |
||
24 | |||
25 | const DRIVER_NAME = "filesystem-xattr"; |
||
26 | |||
27 | protected $cache_folder; |
||
28 | |||
29 | 47 | public function __construct(array $configuration) { |
|
36 | |||
37 | 47 | public function test() { |
|
42 | |||
43 | 34 | public function get($key, $namespace) { |
|
60 | |||
61 | public function set($key, $namespace, $value, $ttl = null) { |
||
62 | |||
63 | $cacheFile = $this->cache_folder."$key-$namespace.cache"; |
||
64 | |||
65 | View Code Duplication | if ( $ttl == null || $ttl == 0 ) { |
|
|
|||
66 | $ttl = 0; |
||
67 | } else { |
||
68 | $ttl = time() + intval($ttl); |
||
69 | } |
||
70 | |||
71 | $cached = @file_put_contents($cacheFile, $value, LOCK_EX); |
||
72 | |||
73 | if ( $cached === false ) throw new Exception("Error writing cache object $cacheFile"); |
||
74 | |||
75 | $tagged = @xattr_set($cacheFile, "EXPIRE", $ttl, XATTR_DONTFOLLOW); |
||
76 | |||
77 | if ( $tagged === false ) throw new Exception("Error writing cache ttl $cacheFile"); |
||
78 | |||
79 | return true; |
||
80 | |||
81 | } |
||
82 | |||
83 | public function delete($key, $namespace) { |
||
98 | |||
99 | View Code Duplication | public function clear($namespace = null) { |
|
100 | |||
101 | $result = []; |
||
102 | |||
103 | if ( $namespace === null ) { |
||
104 | |||
105 | $file_list = glob($this->cache_folder."*.cache"); |
||
106 | |||
107 | } else { |
||
108 | |||
109 | $file_list = glob($this->cache_folder."*-$namespace.cache"); |
||
110 | |||
111 | } |
||
112 | |||
113 | if ( empty($file_list) ) return true; |
||
114 | |||
115 | foreach ( $file_list as $file ) $result[] = unlink($file); |
||
116 | |||
117 | return !in_array(false, $result); |
||
118 | |||
119 | } |
||
120 | |||
121 | 2 | View Code Duplication | public function getMultiple(array $keys, $namespace) { |
132 | |||
133 | 1 | View Code Duplication | public function setMultiple(array $key_values, $namespace, $ttl = null) { |
144 | |||
145 | 3 | View Code Duplication | public function deleteMultiple(array $keys, $namespace) { |
156 | |||
157 | public function has($key, $namespace) { |
||
158 | |||
159 | $time = time(); |
||
160 | |||
161 | $cacheFile = $this->cache_folder."$key-$namespace.cache"; |
||
162 | |||
163 | if ( file_exists($cacheFile) ) { |
||
164 | |||
165 | $expire = xattr_get($cacheFile, "EXPIRE", XATTR_DONTFOLLOW); |
||
166 | |||
167 | if ( $expire === false ) { |
||
168 | throw new Exception("Error reading cache ttl for $cacheFile"); |
||
169 | } else if ( $expire < $time && $expire != 0 ) { |
||
170 | $this->delete($key, $namespace); |
||
171 | return false; |
||
172 | } else { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | } |
||
177 | |||
178 | return false; |
||
179 | |||
180 | } |
||
181 | |||
182 | 2 | public function stats() { |
|
187 | |||
188 | } |
||
189 |
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.