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 |
||
| 9 | class MemcachedCache implements CacheInterface |
||
| 10 | { |
||
| 11 | |||
| 12 | const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
||
| 13 | |||
| 14 | public $handler; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Connect to Memcached service |
||
| 18 | * |
||
| 19 | * @param Memcached $handler Memcached handler object |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | 5 | public function __construct(Memcached $handler) |
|
| 23 | { |
||
| 24 | 5 | $this->handler = $handler; |
|
| 25 | 5 | if (defined('Memcached::HAVE_IGBINARY') && extension_loaded('igbinary')) { |
|
| 26 | ini_set('memcached.serializer', 'igbinary'); |
||
| 27 | } |
||
| 28 | 5 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritDoc} |
||
| 32 | */ |
||
| 33 | 1 | public function get($key, $default = null) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritDoc} |
||
| 43 | */ |
||
| 44 | 3 | View Code Duplication | public function set($key, $value, $ttl = null) |
|
|
|||
| 45 | { |
||
| 46 | |||
| 47 | 3 | $this->checkReservedCharacters($key); |
|
| 48 | 3 | if ($ttl instanceof DateInterval) { |
|
| 49 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
||
| 50 | } |
||
| 51 | 3 | return $this->handler->set($key, $value, (int) $ttl); |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritDoc} |
||
| 56 | */ |
||
| 57 | 1 | public function delete($key) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritDoc} |
||
| 66 | */ |
||
| 67 | 5 | public function clear() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritDoc} |
||
| 74 | */ |
||
| 75 | 1 | public function getMultiple($keys, $default = null) |
|
| 76 | { |
||
| 77 | 1 | $defaults = array_fill(0, count($keys), $default); |
|
| 78 | 1 | foreach ($keys as $key) { |
|
| 79 | 1 | $this->checkReservedCharacters($key); |
|
| 80 | } |
||
| 81 | 1 | return array_merge($this->handler->getMulti($keys), $defaults); |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | 1 | View Code Duplication | public function setMultiple($values, $ttl = null) |
| 88 | { |
||
| 89 | 1 | foreach ($values as $key => $value) { |
|
| 90 | 1 | $this->checkReservedCharacters($key); |
|
| 91 | } |
||
| 92 | 1 | if ($ttl instanceof DateInterval) { |
|
| 93 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
||
| 94 | } |
||
| 95 | 1 | return $this->handler->setMulti($values, (int) $ttl); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | */ |
||
| 101 | 1 | public function deleteMultiple($keys) |
|
| 102 | { |
||
| 103 | 1 | foreach ($keys as $key) { |
|
| 104 | 1 | $this->checkReservedCharacters($key); |
|
| 105 | } |
||
| 106 | 1 | return $this->handler->deleteMulti($keys); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritDoc} |
||
| 111 | */ |
||
| 112 | 1 | public function increment($key, $step = 1) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritDoc} |
||
| 119 | */ |
||
| 120 | 1 | public function decrement($key, $step = 1) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritDoc} |
||
| 127 | */ |
||
| 128 | 1 | public function has($key) |
|
| 129 | { |
||
| 134 | |||
| 135 | 4 | private function checkReservedCharacters($key) |
|
| 148 | } |
||
| 149 |
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.