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 RedisCache 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 RedisCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class RedisCache extends BaseEventEmitter implements CacheInterface |
||
19 | { |
||
20 | use LoopAwareTrait; |
||
21 | |||
22 | /** |
||
23 | * @var mixed[] |
||
24 | */ |
||
25 | protected $config; |
||
26 | |||
27 | /** |
||
28 | * @var RedisInterface |
||
29 | */ |
||
30 | protected $redis; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $selected; |
||
36 | |||
37 | /** |
||
38 | * @param LoopInterface $loop |
||
39 | * @param mixed[] $config |
||
40 | */ |
||
41 | 16 | public function __construct(LoopInterface $loop, $config = []) |
|
42 | { |
||
43 | 16 | $this->loop = $loop; |
|
44 | 16 | $this->config = $this->createConfig($config); |
|
45 | 16 | $this->redis = $this->createRedis(); |
|
46 | 16 | $this->selected = 0; |
|
47 | |||
48 | 16 | $this->attachEvents(); |
|
49 | 16 | } |
|
50 | |||
51 | /** |
||
52 | * |
||
53 | */ |
||
54 | public function __destruct() |
||
59 | |||
60 | /** |
||
61 | * @override |
||
62 | * @inheritDoc |
||
63 | */ |
||
64 | 25 | public function isStarted() |
|
68 | |||
69 | /** |
||
70 | * @override |
||
71 | * @inheritDoc |
||
72 | */ |
||
73 | public function start() |
||
77 | |||
78 | /** |
||
79 | * @override |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | public function stop() |
||
86 | |||
87 | /** |
||
88 | * @override |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function end() |
||
95 | |||
96 | /** |
||
97 | * @override |
||
98 | * @inheritDoc |
||
99 | */ |
||
100 | public function isPaused() |
||
104 | |||
105 | /** |
||
106 | * @override |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | public function pause() |
||
113 | |||
114 | /** |
||
115 | * @override |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | public function resume() |
||
122 | |||
123 | /** |
||
124 | * @override |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | 15 | public function set($key, $val, $ttl = 0.0) |
|
128 | { |
||
129 | 15 | if (!$this->isStarted()) |
|
130 | { |
||
131 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
132 | } |
||
133 | 14 | if (is_object($val)) |
|
134 | { |
||
135 | 1 | return Promise::doReject(new WriteException('Objects are not supported.')); |
|
136 | } |
||
137 | |||
138 | 13 | $promise = $ttl > 0 |
|
139 | 2 | ? $this->redis->setEx($key, round($ttl), json_encode($val)) |
|
140 | 13 | : $this->redis->set($key, json_encode($val)); |
|
141 | |||
142 | return $promise->then(function($result) use($val) { |
||
143 | 13 | if ($result !== 'OK') |
|
144 | { |
||
145 | throw new WriteException('Value could not be set.'); |
||
146 | } |
||
147 | 13 | return $val; |
|
148 | 13 | }); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * @override |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | 6 | View Code Duplication | public function get($key) |
|
|||
156 | { |
||
157 | 6 | if (!$this->isStarted()) |
|
158 | { |
||
159 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
160 | } |
||
161 | return $this->redis->get($key)->then(function($result) { |
||
162 | 5 | return $result === null ? null : json_decode($result, true); |
|
163 | 5 | }); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @override |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | 2 | View Code Duplication | public function remove($key) |
171 | { |
||
172 | 2 | if (!$this->isStarted()) |
|
173 | { |
||
174 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
175 | } |
||
176 | return $this->redis->del($key)->then(function($count) { |
||
177 | 1 | return $count ? true : false; |
|
178 | 1 | }); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @override |
||
183 | * @inheritDoc |
||
184 | */ |
||
185 | 5 | View Code Duplication | public function exists($key) |
186 | { |
||
187 | 5 | if (!$this->isStarted()) |
|
188 | { |
||
189 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
190 | } |
||
191 | return $this->redis->exists($key)->then(function($count) { |
||
192 | 4 | return $count ? true : false; |
|
193 | 4 | }); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * @override |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | 4 | public function setTtl($key, $ttl) |
|
201 | { |
||
202 | 4 | if (!$this->isStarted()) |
|
203 | { |
||
204 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
205 | } |
||
206 | 3 | if ($ttl <= 0) |
|
207 | { |
||
208 | return Promise::doReject(new WriteException('TTL needs to be higher than 0.')); |
||
209 | } |
||
210 | return $this->redis->expire($key, round($ttl))->then(function($result) { |
||
211 | 3 | if ($result === 0) |
|
212 | { |
||
213 | throw new WriteException('Timeout cannot be set on undefined key.'); |
||
214 | } |
||
215 | 3 | return $result; |
|
216 | 3 | }); |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * @override |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | 2 | View Code Duplication | public function getTtl($key) |
224 | { |
||
225 | 2 | if (!$this->isStarted()) |
|
226 | { |
||
227 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
228 | } |
||
229 | return $this->redis->ttl($key)->then(function($result) { |
||
230 | 1 | return $result > 0 ? $result : 0; |
|
231 | 1 | }); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * @override |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | 2 | View Code Duplication | public function removeTtl($key) |
239 | { |
||
240 | 2 | if (!$this->isStarted()) |
|
241 | { |
||
242 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
243 | } |
||
244 | return $this->redis->persist($key)->then(function($result) { |
||
245 | 1 | return $result > 0; |
|
246 | 1 | }); |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * @override |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | 2 | View Code Duplication | public function existsTtl($key) |
254 | { |
||
255 | 2 | if (!$this->isStarted()) |
|
256 | { |
||
257 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
258 | } |
||
259 | return $this->redis->ttl($key)->then(function($result) { |
||
260 | 1 | return $result >= 0; |
|
261 | 1 | }); |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * @override |
||
266 | * @inheritDoc |
||
267 | */ |
||
268 | 2 | View Code Duplication | public function getKeys() |
269 | { |
||
270 | 2 | if (!$this->isStarted()) |
|
271 | { |
||
272 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
273 | } |
||
274 | return $this->redis->keys()->then(function($result) { |
||
275 | 1 | sort($result); |
|
276 | 1 | return $result; |
|
277 | 1 | }); |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * @override |
||
282 | * @inheritDoc |
||
283 | */ |
||
284 | 2 | public function getStats() |
|
285 | { |
||
286 | 2 | if (!$this->isStarted()) |
|
287 | { |
||
288 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
289 | } |
||
290 | return $this->redis->info()->then(function($result) { |
||
291 | 1 | preg_match('#keys=([0-9]+)#si', $result['keyspace']['db' . $this->selected], $matches); |
|
292 | 1 | $keys = isset($matches[1]) ? $matches[1] : 0; |
|
293 | return [ |
||
294 | 1 | 'keys' => (int) $keys, |
|
295 | 1 | 'hits' => (int) $result['stats']['keyspace_hits'], |
|
296 | 1 | 'misses' => (int) $result['stats']['keyspace_misses'], |
|
297 | ]; |
||
298 | 1 | }); |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * @override |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | 15 | public function flush() |
|
313 | |||
314 | /** |
||
315 | * Create configuration. |
||
316 | * |
||
317 | * @return mixed[] |
||
318 | */ |
||
319 | 16 | protected function createConfig($config = []) |
|
323 | |||
324 | /** |
||
325 | * Create Redis client. |
||
326 | * |
||
327 | * @return RedisInterface |
||
328 | */ |
||
329 | 16 | protected function createRedis() |
|
333 | |||
334 | /** |
||
335 | * Parse and return endpoint. |
||
336 | * |
||
337 | * @param string $endpoint |
||
338 | * @return string |
||
339 | */ |
||
340 | 16 | protected function createEndpoint($endpoint) |
|
348 | |||
349 | /** |
||
350 | * Attach events. |
||
351 | */ |
||
352 | 16 | private function attachEvents() |
|
364 | } |
||
365 |
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.