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 | class FileSystemCacheEngine extends BaseCacheEngine implements CacheLockInterface |
||
10 | { |
||
11 | |||
12 | protected $logger = null; |
||
13 | |||
14 | protected $prefix = null; |
||
15 | |||
16 | public function __construct($prefix = 'cache', $logger = null) |
||
25 | |||
26 | /** |
||
27 | * @param string $key The object KEY |
||
28 | * @param mixed $default IGNORED IN MEMCACHED. |
||
29 | * @return mixed Description |
||
30 | */ |
||
31 | public function get($key, $default = null) |
||
63 | |||
64 | /** |
||
65 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
66 | * |
||
67 | * @param string $key The key of the item to store. |
||
68 | * @param mixed $value The value of the item to store, must be serializable. |
||
69 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
70 | * the driver supports TTL then the library may set a default value |
||
71 | * for it or let the driver take care of that. |
||
72 | * |
||
73 | * @return bool True on success and false on failure. |
||
74 | * |
||
75 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
76 | * MUST be thrown if the $key string is not a legal value. |
||
77 | */ |
||
78 | public function set($key, $value, $ttl = null) |
||
111 | |||
112 | /** |
||
113 | * @param string $key |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function delete($key) |
||
121 | |||
122 | /** |
||
123 | * Lock resource before set it. |
||
124 | * @param string $key |
||
125 | */ |
||
126 | public function lock($key) |
||
138 | |||
139 | /** |
||
140 | * UnLock resource after set it. |
||
141 | * @param string $key |
||
142 | */ |
||
143 | public function unlock($key) |
||
154 | |||
155 | public function isAvailable() |
||
159 | |||
160 | protected function fixKey($key) |
||
167 | |||
168 | /** |
||
169 | * Wipes clean the entire cache's keys. |
||
170 | * |
||
171 | * @return bool True on success and false on failure. |
||
172 | */ |
||
173 | public function clear() |
||
182 | |||
183 | /** |
||
184 | * Determines whether an item is present in the cache. |
||
185 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
186 | * and not to be used within your live applications operations for get/set, as this method |
||
187 | * is subject to a race condition where your has() will return true and immediately after, |
||
188 | * another script can remove it making the state of your app out of date. |
||
189 | * |
||
190 | * @param string $key The cache item key. |
||
191 | * @return bool |
||
192 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
193 | * MUST be thrown if the $key string is not a legal value. |
||
194 | */ |
||
195 | public function has($key) |
||
215 | } |
||
216 |
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.