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 |
||
20 | class Memcache extends Common |
||
21 | { |
||
22 | /** |
||
23 | * Separator which concatenates tags. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $tagSeparator = '|'; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | * |
||
32 | * Additional options: |
||
33 | * "client" => the instance of \Memcache object |
||
34 | * "compress" => boolean value which indicates to enable zlib compression or not |
||
35 | * |
||
36 | * @codeCoverageIgnore |
||
37 | * @param array $options |
||
38 | * @throws \Endeveit\Cache\Exception |
||
39 | */ |
||
40 | public function __construct(array $options = array()) |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | * |
||
59 | * @param string $id |
||
60 | * @param integer $value |
||
61 | * @return integer |
||
62 | */ |
||
63 | View Code Duplication | public function increment($id, $value = 1) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | * |
||
89 | * @param string $id |
||
90 | * @param integer $value |
||
91 | * @return integer |
||
92 | */ |
||
93 | View Code Duplication | public function decrement($id, $value = 1) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | * |
||
120 | * @param string $id |
||
121 | * @return mixed|false |
||
122 | */ |
||
123 | protected function doLoad($id) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | * |
||
133 | * @param array $identifiers |
||
134 | * @return array |
||
135 | */ |
||
136 | View Code Duplication | protected function doLoadMany(array $identifiers) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | * |
||
163 | * @param string $id |
||
164 | * @return mixed|false |
||
165 | */ |
||
166 | protected function doLoadRaw($id) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | * |
||
174 | * @param mixed $data |
||
175 | * @param string $id |
||
176 | * @param array $tags |
||
177 | * @return boolean |
||
178 | */ |
||
179 | View Code Duplication | protected function doSave($data, $id, array $tags = array()) |
|
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | * |
||
193 | * @param mixed $data |
||
194 | * @param string $id |
||
195 | * @param integer|boolean $lifetime |
||
196 | * @return boolean |
||
197 | */ |
||
198 | protected function doSaveScalar($data, $id, $lifetime = false) |
||
202 | |||
203 | /** |
||
204 | * Remove an items by cache tags. |
||
205 | * |
||
206 | * @param array $tags |
||
207 | * @return boolean |
||
208 | */ |
||
209 | protected function doRemoveByTags(array $tags) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | * |
||
221 | * @return boolean |
||
222 | */ |
||
223 | protected function doFlush() |
||
227 | |||
228 | /** |
||
229 | * Validates cache identifier or a tag, throws an exception in |
||
230 | * case of a problem. |
||
231 | * |
||
232 | * @param string $id |
||
233 | * @throws \Endeveit\Cache\Exception |
||
234 | */ |
||
235 | protected function validateIdentifier($id) |
||
244 | |||
245 | /** |
||
246 | * Save the tags for identifier. |
||
247 | * |
||
248 | * @param string $id |
||
249 | * @param array $tags |
||
250 | */ |
||
251 | protected function saveTagsForId($id, array $tags) |
||
263 | |||
264 | /** |
||
265 | * Return an array of stored cache ids which match given tags. |
||
266 | * |
||
267 | * @param array $tags |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function getIdsMatchingAnyTags($tags = array()) |
||
280 | |||
281 | /** |
||
282 | * Returns list of identifiers for tag. |
||
283 | * |
||
284 | * @param string $tag |
||
285 | * @return array |
||
286 | */ |
||
287 | protected function getIdentifiersForTag($tag) |
||
300 | } |
||
301 |
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.