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 |
||
| 12 | class Memcached implements CacheInterface |
||
| 13 | { |
||
| 14 | const SETTINGS_MEMCACHED = 'SETTINGS_MEMCACHED'; |
||
| 15 | /** |
||
| 16 | * @see http://php.net/manual/fr/memcached.expiration.php |
||
| 17 | */ |
||
| 18 | const SETTINGS_TTL_IN_SECOND = 'SETTINGS_TTL_IN_SECOND'; |
||
| 19 | const SETTINGS_CACHE_PREFIX = 'SETTINGS_CACHE_PREFIX'; |
||
| 20 | |||
| 21 | private $isAvailable = null; |
||
| 22 | private $memcachedInstance = null; |
||
| 23 | private $settings = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Check whether memcached is available |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | 1 | public function isAvailable() |
|
| 30 | { |
||
| 31 | 1 | if (is_null($this->isAvailable)) { |
|
| 32 | 1 | $this->isAvailable = class_exists('\Memcached'); |
|
| 33 | } |
||
| 34 | 1 | return $this->isAvailable; |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * constructor of Memcached |
||
| 39 | * @param array $settings |
||
| 40 | */ |
||
| 41 | 15 | public function __construct($settings = array()) |
|
| 42 | { |
||
| 43 | 15 | if (isset($settings[self::SETTINGS_MEMCACHED])) { |
|
| 44 | 1 | $this->setMemcachedInstance($settings[self::SETTINGS_MEMCACHED]); |
|
| 45 | } |
||
| 46 | 15 | $this->settings = $settings; |
|
| 47 | 15 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Get the memcached instance |
||
| 51 | * @return \Memcached |
||
| 52 | * @throws Exception |
||
| 53 | */ |
||
| 54 | 5 | public function getMemcachedInstance() |
|
| 55 | { |
||
| 56 | 5 | if (!$this->memcachedInstance) { |
|
| 57 | 2 | if (!$this->isAvailable()) { |
|
| 58 | 1 | throw new Exception('Memcached is not available'); |
|
| 59 | } |
||
| 60 | 1 | $this->memcachedInstance = $this->createMemcached(); |
|
| 61 | } |
||
| 62 | 4 | return $this->memcachedInstance; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return \Memcached |
||
| 67 | * @codeCoverageIgnore |
||
| 68 | */ |
||
| 69 | protected function createMemcached() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Define a memcached instance |
||
| 76 | * @param \Memcached $memcachedInstance |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | 3 | public function setMemcachedInstance(\Memcached $memcachedInstance) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $key |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 4 | protected function getCacheKey($key) |
|
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Check whether a key exists |
||
| 98 | * @param string $key |
||
| 99 | * @return bool |
||
| 100 | * @throws Exception |
||
| 101 | */ |
||
| 102 | 2 | public function has($key) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $key |
||
| 114 | * @return mixed |
||
| 115 | * @throws Exception |
||
| 116 | */ |
||
| 117 | 2 | View Code Duplication | public function get($key) |
|
|
|||
| 118 | { |
||
| 119 | 2 | if (!$this->isAvailable()) { |
|
| 120 | 1 | throw new Exception('Memcached is not available'); |
|
| 121 | } |
||
| 122 | 1 | $key = $this->getCacheKey($key); |
|
| 123 | 1 | return $this->getMemcachedInstance()->get($key); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Define a |
||
| 128 | * @param string $key |
||
| 129 | * @param mixed $value |
||
| 130 | * @return $this |
||
| 131 | * @throws Exception |
||
| 132 | */ |
||
| 133 | 3 | public function set($key, $value) |
|
| 134 | { |
||
| 135 | 3 | if (!$this->isAvailable()) { |
|
| 136 | 1 | throw new Exception('Memcached is not available'); |
|
| 137 | } |
||
| 138 | 2 | $ttl = (int)$this->getSetting(self::SETTINGS_TTL_IN_SECOND); |
|
| 139 | 2 | $key = $this->getCacheKey($key); |
|
| 140 | 2 | if (!$this->getMemcachedInstance()->set($key, $value, $ttl)) { |
|
| 141 | 1 | throw new Exception( |
|
| 142 | 1 | 'Error while inserting value in memcached : ' . $this->getMemcachedInstance()->getResultMessage(), |
|
| 143 | 1 | $this->getMemcachedInstance()->getResultCode() |
|
| 144 | ); |
||
| 145 | } |
||
| 146 | 1 | return $this; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Delete a value in memcache |
||
| 151 | * @param string $key |
||
| 152 | * @return $this |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | 3 | View Code Duplication | public function remove($key) |
| 166 | |||
| 167 | /** |
||
| 168 | * Define a setting |
||
| 169 | * @param string $setting |
||
| 170 | * @param mixed $value |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 1 | public function setSetting($setting, $value) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve a defined setting |
||
| 181 | * @param string $setting |
||
| 182 | * @return mixed|null |
||
| 183 | */ |
||
| 184 | 5 | public function getSetting($setting) |
|
| 189 | } |
||
| 190 |
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.