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 |
||
26 | abstract class AbstractAdapter extends Adapter implements CacheAdapter |
||
27 | { |
||
28 | const CACHE_TTL = 'cache_ttl'; |
||
29 | const CACHE_VALUE = 'cache_value'; |
||
30 | |||
31 | const TABLE_CACHE_ID = 'cache_id'; |
||
32 | const TABLE_CACHE_VALUE = 'cache_value'; |
||
33 | const TABLE_CACHE_TTL = 'cache_ttl'; |
||
34 | |||
35 | const QUERY_ID_PLACEHOLDER = ':id'; |
||
36 | const QUERY_VALUE_PLACEHOLDER = ':value'; |
||
37 | const QUERY_TTL_PLACEHOLDER = ':ttl'; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $connectionClass = ''; |
||
43 | |||
44 | /** |
||
45 | * @var \PDO |
||
46 | */ |
||
47 | protected $connection; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $cacheTableName; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $parameters = []; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $requiredKeys = [ |
||
63 | AbstractPDOConnection::USER, |
||
64 | AbstractPDOConnection::PASSWORD, |
||
65 | AbstractPDOConnection::DATABASE, |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * @param array $connection |
||
70 | * @param string $tableName |
||
71 | * @param CacheAdapter $next |
||
72 | */ |
||
73 | public function __construct(array $connection, $tableName, CacheAdapter $next = null) |
||
82 | |||
83 | /** |
||
84 | * @param array $parameters |
||
85 | * |
||
86 | * @throws InvalidArgumentException |
||
87 | */ |
||
88 | protected function checkMandatoryParameterFields(array &$parameters) |
||
98 | |||
99 | /** |
||
100 | * @return object |
||
101 | */ |
||
102 | public function getConnection() |
||
118 | |||
119 | /** |
||
120 | * Get a value identified by $key. |
||
121 | * |
||
122 | * @param string $key |
||
123 | * |
||
124 | * @return bool|mixed |
||
125 | */ |
||
126 | public function get($key) |
||
152 | |||
153 | /** |
||
154 | * @param string $key |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | View Code Duplication | protected function getFromDatabase($key) |
|
181 | |||
182 | /** |
||
183 | * Delete a value identified by $key. |
||
184 | * |
||
185 | * @param string $key |
||
186 | */ |
||
187 | public function delete($key) |
||
192 | |||
193 | /** |
||
194 | * @param string $key |
||
195 | */ |
||
196 | protected function deleteFromDatabase($key) |
||
210 | |||
211 | /** |
||
212 | * Set a value identified by $key and with an optional $ttl. |
||
213 | * |
||
214 | * @param string $key |
||
215 | * @param mixed $value |
||
216 | * @param int $ttl |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function set($key, $value, $ttl = 0) |
||
227 | |||
228 | |||
229 | /** |
||
230 | * @param $ttl |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | View Code Duplication | protected function getCalculatedTtl($ttl) |
|
242 | |||
243 | /** |
||
244 | * @param $key |
||
245 | * @param $value |
||
246 | * @param int $ttl |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | protected function insertToDatabase($key, $value, $ttl = 0) |
||
284 | |||
285 | /** |
||
286 | * @param $key |
||
287 | * @param $value |
||
288 | * @param DateTime $ttl |
||
289 | * @return $this |
||
290 | */ |
||
291 | protected function updateToDatabase($key, $value, DateTime $ttl) |
||
316 | |||
317 | /** |
||
318 | * Checks the availability of the cache service. |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function isAvailable() |
||
340 | |||
341 | /** |
||
342 | * Clears all expired values from cache. |
||
343 | * |
||
344 | * @return mixed |
||
345 | */ |
||
346 | public function clear() |
||
351 | |||
352 | /** |
||
353 | * |
||
354 | */ |
||
355 | protected function clearFromDatabase() |
||
371 | |||
372 | /** |
||
373 | * Clears all values from the cache. |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function drop() |
||
382 | |||
383 | /** |
||
384 | * |
||
385 | */ |
||
386 | protected function dropFromDatabase() |
||
394 | } |
||
395 |
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.