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 |
||
30 | abstract class AbstractRedisStore implements KeyValueStore |
||
31 | { |
||
32 | /** |
||
33 | * Redis client. |
||
34 | * |
||
35 | * @var object |
||
36 | */ |
||
37 | protected $client; |
||
38 | |||
39 | /** |
||
40 | * Return the value corresponding to "not found" |
||
41 | * for the internal client. |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | abstract protected function clientNotFoundValue(); |
||
46 | |||
47 | /** |
||
48 | * Call the internal client method to fetch a key. |
||
49 | * Don't have to catch the exceptions. |
||
50 | * |
||
51 | * @param string $key The key to fetch |
||
52 | * |
||
53 | * @return mixed The raw value |
||
54 | */ |
||
55 | abstract protected function clientGet($key); |
||
56 | |||
57 | /** |
||
58 | * Call the internal client method to fetch multiple keys. |
||
59 | * Don't have to catch the exceptions. |
||
60 | * |
||
61 | * @param array $keys The keys to fetch |
||
62 | * |
||
63 | * @return array The raw values |
||
64 | */ |
||
65 | abstract protected function clientGetMultiple(array $keys); |
||
66 | |||
67 | /** |
||
68 | * Call the internal client method to set a value associated to a key. |
||
69 | * Don't have to catch the exceptions. |
||
70 | * |
||
71 | * @param string $key |
||
72 | * @param mixed $value |
||
73 | */ |
||
74 | abstract protected function clientSet($key, $value); |
||
75 | |||
76 | /** |
||
77 | * Call the internal client method to remove a key. |
||
78 | * Don't have to catch the exceptions. |
||
79 | * |
||
80 | * @param string $key |
||
81 | * |
||
82 | * @return bool true if the removal worked, false otherwise |
||
83 | */ |
||
84 | abstract protected function clientRemove($key); |
||
85 | |||
86 | /** |
||
87 | * Call the internal client method to check if a key exists. |
||
88 | * Don't have to catch the exceptions. |
||
89 | * |
||
90 | * @param string $key |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | abstract protected function clientExists($key); |
||
95 | |||
96 | /** |
||
97 | * Call the internal client method to clear all the keys. |
||
98 | * Don't have to catch the exceptions. |
||
99 | */ |
||
100 | abstract protected function clientClear(); |
||
101 | |||
102 | /** |
||
103 | * Call the internal client method to fetch all the keys. |
||
104 | * Don't have to catch the exceptions. |
||
105 | * |
||
106 | * @return array The keys |
||
107 | */ |
||
108 | abstract protected function clientKeys(); |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 12 | public function getMultiple(array $keys, $default = null) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 64 | public function getMultipleOrFail(array $keys) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 14 | View Code Duplication | public function get($key, $default = null) |
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 64 | View Code Duplication | public function getOrFail($key) |
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 116 | public function set($key, $value) |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 20 | public function remove($key) |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 62 | public function exists($key) |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | 164 | public function clear() |
|
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | 8 | public function keys() |
|
276 | } |
||
277 |
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.