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 |
||
25 | View Code Duplication | class CouchbaseStore extends TaggableStore implements Store |
|
|
|||
26 | { |
||
27 | use RetrievesMultipleKeys; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $prefix; |
||
31 | |||
32 | /** @var CouchbaseBucket */ |
||
33 | protected $bucket; |
||
34 | |||
35 | /** @var CouchbaseCluster */ |
||
36 | protected $cluster; |
||
37 | |||
38 | /** |
||
39 | * CouchbaseStore constructor. |
||
40 | * |
||
41 | * @param CouchbaseCluster $cluster |
||
42 | * @param $bucket |
||
43 | * @param string $password |
||
44 | * @param null $prefix |
||
45 | * @param string $serialize |
||
46 | */ |
||
47 | 13 | public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php') |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 6 | public function get($key) |
|
67 | |||
68 | /** |
||
69 | * Store an item in the cache if the key doesn't exist. |
||
70 | * |
||
71 | * @param string|array $key |
||
72 | * @param mixed $value |
||
73 | * @param int $minutes |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | 5 | public function add($key, $value, $minutes = 0) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 1 | public function put($key, $value, $minutes) |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 1 | public function increment($key, $value = 1) |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 1 | public function decrement($key, $value = 1) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function forever($key, $value) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 8 | public function forget($key) |
|
131 | |||
132 | /** |
||
133 | * flush bucket. |
||
134 | * |
||
135 | * @throws FlushException |
||
136 | * @codeCoverageIgnore |
||
137 | */ |
||
138 | public function flush() |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function getPrefix() |
|
153 | |||
154 | /** |
||
155 | * Set the cache key prefix. |
||
156 | * |
||
157 | * @param string $prefix |
||
158 | */ |
||
159 | 13 | public function setPrefix($prefix) |
|
163 | |||
164 | /** |
||
165 | * @param $bucket |
||
166 | * @param string $password |
||
167 | * @param string $serialize |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | 13 | public function setBucket($bucket, $password = '', $serialize = 'php') |
|
180 | |||
181 | /** |
||
182 | * @param $keys |
||
183 | * |
||
184 | * @return array|string |
||
185 | 9 | */ |
|
186 | private function resolveKey($keys) |
||
199 | |||
200 | /** |
||
201 | * @param $meta |
||
202 | * |
||
203 | * @return array|null |
||
204 | 5 | */ |
|
205 | protected function getMetaDoc($meta) |
||
221 | } |
||
222 |
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.