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 namespace Comodojo\Cache\Providers; |
||
| 33 | abstract class AbstractEnhancedProvider |
||
| 34 | extends AbstractProvider |
||
| 35 | implements EnhancedCacheItemPoolInterface { |
||
| 36 | |||
| 37 | use StatefulTrait; |
||
| 38 | use NamespaceTrait; |
||
| 39 | |||
| 40 | protected $driver; |
||
| 41 | |||
| 42 | protected $default_properties = []; |
||
| 43 | |||
| 44 | protected $properties; |
||
| 45 | |||
| 46 | private $queue = []; |
||
| 47 | |||
| 48 | 211 | View Code Duplication | public function __construct(array $properties = [], LoggerInterface $logger = null) { |
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | abstract public function getStats(); |
||
| 62 | |||
| 63 | 82 | public function getProperties() { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 176 | public function getItem($key) { |
|
| 73 | |||
| 74 | 176 | if ( KeyValidator::validateKey($key) === false ) { |
|
| 75 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
| 76 | 28 | } |
|
| 77 | |||
| 78 | try { |
||
| 79 | |||
| 80 | 176 | $data = $this->driver->get($key, $this->getNamespace()); |
|
| 81 | |||
| 82 | 176 | } catch (Exception $e) { |
|
| 83 | |||
| 84 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 85 | $data = null; |
||
| 86 | |||
| 87 | } |
||
| 88 | |||
| 89 | 176 | if ( $data === null ) return new Item($key); |
|
| 90 | |||
| 91 | 93 | $item = new Item($key, true); |
|
| 92 | |||
| 93 | 93 | return $item->set(unserialize($data)); |
|
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 42 | View Code Duplication | public function hasItem($key) { |
| 101 | |||
| 102 | 42 | if ( KeyValidator::validateKey($key) === false ) { |
|
| 103 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
| 104 | } |
||
| 105 | |||
| 106 | try { |
||
| 107 | |||
| 108 | 42 | $data = $this->driver->has($key, $this->getNamespace()); |
|
| 109 | |||
| 110 | 42 | } catch (Exception $e) { |
|
| 111 | |||
| 112 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 113 | $data = false; |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 117 | 42 | return $data; |
|
| 118 | |||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 57 | public function clear() { |
|
| 125 | |||
| 126 | try { |
||
| 127 | |||
| 128 | 57 | $data = $this->driver->clear(); |
|
| 129 | |||
| 130 | 57 | } catch (Exception $e) { |
|
| 131 | |||
| 132 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 133 | $data = false; |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | 57 | return $data; |
|
| 138 | |||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | 8 | View Code Duplication | public function clearNamespace() { |
| 145 | |||
| 146 | try { |
||
| 147 | |||
| 148 | 8 | $data = $this->driver->clear($this->getNamespace()); |
|
| 149 | |||
| 150 | 8 | } catch (Exception $e) { |
|
| 151 | |||
| 152 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 153 | $data = false; |
||
| 154 | |||
| 155 | } |
||
| 156 | |||
| 157 | 8 | return $data; |
|
| 158 | |||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | 10 | View Code Duplication | public function deleteItem($key) { |
| 165 | |||
| 166 | 10 | if ( KeyValidator::validateKey($key) === false ) { |
|
| 167 | throw new InvalidCacheArgumentException('Invalid key provided'); |
||
| 168 | } |
||
| 169 | |||
| 170 | try { |
||
| 171 | |||
| 172 | 10 | $data = $this->driver->delete($key, $this->getNamespace()); |
|
| 173 | |||
| 174 | 10 | } catch (Exception $e) { |
|
| 175 | |||
| 176 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 177 | $data = false; |
||
| 178 | |||
| 179 | } |
||
| 180 | |||
| 181 | 10 | return $data; |
|
| 182 | |||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 169 | public function save(CacheItemInterface $item) { |
|
| 189 | |||
| 190 | 169 | $ttl = $item->getTtl(); |
|
| 191 | |||
| 192 | 169 | if ( $ttl < 0 ) return false; |
|
| 193 | |||
| 194 | try { |
||
| 195 | |||
| 196 | 120 | $data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl); |
|
| 197 | |||
| 198 | 120 | } catch (Exception $e) { |
|
| 199 | |||
| 200 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 201 | $data = false; |
||
| 202 | |||
| 203 | } |
||
| 204 | |||
| 205 | 120 | return $data; |
|
| 206 | |||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 211 | View Code Duplication | public function test() { |
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | 12 | public function getItems(array $keys = []) { |
|
| 236 | |||
| 237 | 12 | if ( empty($keys) ) return []; |
|
| 238 | |||
| 239 | 6 | $result = []; |
|
| 240 | |||
| 241 | try { |
||
| 242 | |||
| 243 | 6 | $data = $this->driver->getMultiple($keys, $this->getNamespace()); |
|
| 244 | |||
| 245 | 6 | } catch (Exception $e) { |
|
| 246 | |||
| 247 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 248 | $data = array_combine($keys, array_fill(0, count($keys), null)); |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | 6 | foreach ( $data as $key => $value ) { |
|
| 253 | |||
| 254 | 6 | if ( $value == null ) { |
|
| 255 | $result[$key] = new Item($key); |
||
| 256 | } else { |
||
| 257 | 6 | $result[$key] = new Item($key, true); |
|
| 258 | 6 | $result[$key]->set(unserialize($value)); |
|
| 259 | } |
||
| 260 | |||
| 261 | 6 | } |
|
| 262 | |||
| 263 | 6 | return $result; |
|
| 264 | |||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | 12 | View Code Duplication | public function deleteItems(array $keys) { |
| 271 | |||
| 272 | try { |
||
| 273 | |||
| 274 | 12 | $data = $this->driver->deleteMultiple($keys, $this->getNamespace()); |
|
| 275 | |||
| 276 | 12 | } catch (Exception $e) { |
|
| 277 | |||
| 278 | $this->setState(self::CACHE_ERROR, $e->getMessage()); |
||
| 279 | $data = false; |
||
| 280 | |||
| 281 | } |
||
| 282 | |||
| 283 | 12 | return $data; |
|
| 284 | |||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | 12 | View Code Duplication | public function saveDeferred(CacheItemInterface $item) { |
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | 12 | View Code Duplication | public function commit() { |
| 306 | |||
| 307 | 12 | $result = []; |
|
| 308 | |||
| 309 | 12 | $active_namespace = $this->getNamespace(); |
|
| 310 | |||
| 311 | 12 | foreach ( $this->queue as $namespace => $queue ) { |
|
| 312 | |||
| 313 | 12 | $this->setNamespace($namespace); |
|
| 314 | |||
| 315 | 12 | foreach ( $queue as $key => $item ) { |
|
| 316 | |||
| 317 | 12 | $result[] = $this->save($item); |
|
| 318 | |||
| 319 | 12 | } |
|
| 320 | |||
| 321 | 12 | } |
|
| 322 | |||
| 323 | 12 | $this->queue = []; |
|
| 324 | |||
| 325 | 12 | $this->setNamespace($active_namespace); |
|
| 326 | |||
| 327 | 12 | return in_array(false, $result) ? false : true; |
|
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | 12 | View Code Duplication | private function checkQueueNamespace($create = false) { |
| 345 | |||
| 346 | } |
||
| 347 |