Completed
Push — master ( 386972...cf64d5 )
by Lars
02:26 queued 46s
created

AdapterMemcached   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 9
loc 117
ccs 31
cts 40
cp 0.775
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A installed() 0 4 1
A remove() 0 4 1
A removeAll() 0 4 1
A setExpired() 0 8 2
A setSettings() 0 13 2
A __construct() 0 6 2
A setMemcached() 0 6 1
A exists() 0 4 1
A get() 0 4 1
A set() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace voku\cache;
4
5
use voku\cache\Exception\InvalidArgumentException;
6
7
/**
8
 * AdapterMemcached: Memcached-adapter
9
 *
10
 * @package voku\cache
11
 */
12
class AdapterMemcached implements iAdapter
13
{
14
  /**
15
   * @var bool
16
   */
17
  public $installed = false;
18
19
  /**
20
   * @var \Memcached
21
   */
22
  private $memcached;
23
24
  /**
25
   * __construct
26
   *
27
   * @param \Memcached|null $memcached
28
   */
29 9
  public function __construct($memcached = null)
30
  {
31 9
    if ($memcached instanceof \Memcached) {
0 ignored issues
show
Bug introduced by
The class Memcached does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
32 9
      $this->setMemcached($memcached);
33 9
    }
34 9
  }
35
36
  /**
37
   * @param \Memcached $memcached
38
   */
39 9
  public function setMemcached(\Memcached $memcached) {
40 9
    $this->memcached = $memcached;
41 9
    $this->installed = true;
42
43 9
    $this->setSettings();
44 9
  }
45
46
  /**
47
   * @inheritdoc
48
   */
49 2
  public function exists($key)
50
  {
51 2
    return $this->get($key) !== false;
52
  }
53
54
  /**
55
   * @inheritdoc
56
   */
57 5
  public function get($key)
58
  {
59 5
    return $this->memcached->get($key);
60
  }
61
62
  /**
63
   * @inheritdoc
64
   */
65 9
  public function installed()
66
  {
67 9
    return $this->installed;
68
  }
69
70
  /**
71
   * @inheritdoc
72
   */
73
  public function remove($key)
74
  {
75
    return $this->memcached->delete($key);
76
  }
77
78
  /**
79
   * @inheritdoc
80
   */
81
  public function removeAll()
82
  {
83
    return $this->memcached->flush();
84
  }
85
86
  /**
87
   * @inheritdoc
88
   */
89 3 View Code Duplication
  public function set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
90
  {
91
    // Make sure we are under the proper limit
92 3
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
93
      throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true));
94
    }
95
96 3
    return $this->memcached->set($key, $value);
97
  }
98
99
  /**
100
   * @inheritdoc
101
   */
102 1
  public function setExpired($key, $value, $ttl)
103
  {
104 1
    if ($ttl > 2592000) {
105
      $ttl = 2592000;
106
    }
107
108 1
    return $this->memcached->set($key, $value, $ttl);
109
  }
110
111
  /**
112
   * Set the MemCached settings.
113
   */
114 9
  private function setSettings()
115
  {
116
    // Use faster compression if available
117 9
    if (\Memcached::HAVE_IGBINARY) {
118
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
119
    }
120 9
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
121 9
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
122 9
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
123 9
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
124 9
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
125 9
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
126 9
  }
127
128
}
129