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\Providers; |
||
34 | abstract class AbstractEnhancedProvider |
||
35 | extends AbstractProvider |
||
36 | implements EnhancedCacheItemPoolInterface { |
||
37 | |||
38 | use StatefulTrait; |
||
39 | use NamespaceTrait; |
||
40 | |||
41 | protected $driver; |
||
42 | |||
43 | private $queue = []; |
||
44 | |||
45 | 204 | public function __construct(LoggerInterface $logger = null) { |
|
52 | |||
53 | abstract public function getStats(); |
||
54 | |||
55 | 171 | public function getItem($key) { |
|
56 | |||
57 | 171 | if ( KeyValidator::validateKey($key) === false ) { |
|
58 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
59 | } |
||
60 | |||
61 | try { |
||
62 | |||
63 | 171 | $data = $this->driver->get($key, $this->getNamespace()); |
|
64 | |||
65 | } catch (Exception $e) { |
||
66 | |||
67 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
68 | $data = null; |
||
69 | |||
70 | } |
||
71 | |||
72 | 171 | if ( $data === null ) return new Item($key); |
|
73 | |||
74 | 93 | $item = new Item($key, true); |
|
75 | |||
76 | 93 | return $item->set(unserialize($data)); |
|
77 | |||
78 | } |
||
79 | |||
80 | 38 | View Code Duplication | public function hasItem($key) { |
81 | |||
82 | 38 | if ( KeyValidator::validateKey($key) === false ) { |
|
83 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
84 | } |
||
85 | |||
86 | try { |
||
87 | |||
88 | 38 | $data = $this->driver->has($key, $this->getNamespace()); |
|
89 | |||
90 | } catch (Exception $e) { |
||
91 | |||
92 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
93 | $data = false; |
||
94 | |||
95 | } |
||
96 | |||
97 | 38 | return $data; |
|
98 | |||
99 | } |
||
100 | |||
101 | 56 | public function clear() { |
|
102 | |||
103 | try { |
||
104 | |||
105 | 56 | $data = $this->driver->clear(); |
|
106 | |||
107 | } catch (Exception $e) { |
||
108 | |||
109 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
110 | $data = false; |
||
111 | |||
112 | } |
||
113 | |||
114 | 56 | return $data; |
|
115 | |||
116 | } |
||
117 | |||
118 | 7 | View Code Duplication | public function clearNamespace() { |
119 | |||
120 | try { |
||
121 | |||
122 | 7 | $data = $this->driver->clear($this->getNamespace()); |
|
123 | |||
124 | } catch (Exception $e) { |
||
125 | |||
126 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
127 | $data = false; |
||
128 | |||
129 | } |
||
130 | |||
131 | 7 | return $data; |
|
132 | |||
133 | } |
||
134 | |||
135 | 9 | View Code Duplication | public function deleteItem($key) { |
136 | |||
137 | 9 | if ( KeyValidator::validateKey($key) === false ) { |
|
138 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
139 | } |
||
140 | |||
141 | try { |
||
142 | |||
143 | 9 | $data = $this->driver->delete($key, $this->getNamespace()); |
|
144 | |||
145 | } catch (Exception $e) { |
||
146 | |||
147 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
148 | $data = false; |
||
149 | |||
150 | } |
||
151 | |||
152 | 9 | return $data; |
|
153 | |||
154 | } |
||
155 | |||
156 | 165 | public function save(CacheItemInterface $item) { |
|
157 | |||
158 | 165 | $ttl = $item->getTtl(); |
|
159 | |||
160 | 165 | if ( $ttl < 0 ) return false; |
|
161 | |||
162 | try { |
||
163 | |||
164 | 116 | $data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl); |
|
165 | |||
166 | } catch (Exception $e) { |
||
167 | |||
168 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
169 | $data = false; |
||
170 | |||
171 | } |
||
172 | |||
173 | 116 | return $data; |
|
174 | |||
175 | } |
||
176 | |||
177 | 204 | View Code Duplication | public function test() { |
196 | |||
197 | 12 | public function getItems(array $keys = []) { |
|
198 | |||
199 | 12 | if ( empty($keys) ) return []; |
|
200 | |||
201 | 6 | $result = []; |
|
202 | |||
203 | try { |
||
204 | |||
205 | 6 | $data = $this->driver->getMultiple($keys, $this->getNamespace()); |
|
206 | |||
207 | } catch (Exception $e) { |
||
208 | |||
209 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
210 | $data = array_combine($keys, array_fill(0, count($keys), null)); |
||
211 | |||
212 | } |
||
213 | |||
214 | 6 | foreach ( $data as $key => $value ) { |
|
215 | |||
216 | 6 | if ( $value == null ) { |
|
217 | $result[$key] = new Item($key); |
||
218 | } else { |
||
219 | 6 | $result[$key] = new Item($key, true); |
|
220 | 6 | $result[$key]->set(unserialize($value)); |
|
221 | } |
||
222 | |||
223 | } |
||
224 | |||
225 | 6 | return $result; |
|
226 | |||
227 | } |
||
228 | |||
229 | 12 | View Code Duplication | public function deleteItems(array $keys) { |
230 | |||
231 | try { |
||
232 | |||
233 | 12 | $data = $this->driver->deleteMultiple($keys, $this->getNamespace()); |
|
234 | |||
235 | } catch (Exception $e) { |
||
236 | |||
237 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
238 | $data = false; |
||
239 | |||
240 | } |
||
241 | |||
242 | 12 | return $data; |
|
243 | |||
244 | } |
||
245 | |||
246 | 12 | View Code Duplication | public function saveDeferred(CacheItemInterface $item) { |
257 | |||
258 | 12 | View Code Duplication | public function commit() { |
259 | |||
260 | 12 | $result = []; |
|
261 | |||
262 | 12 | $active_namespace = $this->getNamespace(); |
|
263 | |||
264 | 12 | foreach ($this->queue as $namespace => $queue) { |
|
265 | |||
266 | 12 | $this->setNamespace($namespace); |
|
267 | |||
268 | 12 | foreach ($queue as $key => $item) { |
|
269 | |||
270 | 12 | $result[] = $this->save($item); |
|
271 | |||
272 | } |
||
273 | |||
274 | } |
||
275 | |||
276 | 12 | $this->queue = []; |
|
277 | |||
278 | 12 | $this->setNamespace($active_namespace); |
|
279 | |||
280 | 12 | return in_array(false, $result) ? false : true; |
|
281 | |||
282 | } |
||
283 | |||
284 | 12 | View Code Duplication | private function checkQueueNamespace($create = false) { |
298 | |||
299 | } |
||
300 |