Completed
Push — master ( 361c76...2334dd )
by Lars
03:00
created

AdapterMemcached   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 80%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 14
c 6
b 2
f 0
lcom 1
cbo 0
dl 12
loc 148
ccs 36
cts 45
cp 0.8
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B setSettings() 0 24 4
A set() 0 11 1
A setExpired() 0 8 2
A remove() 0 4 1
A exists() 0 4 1
A get() 12 12 2
A installed() 0 4 1

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
/**
6
 * AdapterMemcached: Memcached-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterMemcached implements iAdapter
11
{
12
  /**
13
   * @var bool
14
   */
15
  public $installed = false;
16
17
  /**
18
   * @var \Memcached
19
   */
20
  private $memcached;
21
22
  /**
23
   * __construct
24
   *
25
   * @param \Memcached $memcached
26
   */
27 6
  public function __construct($memcached)
28
  {
29 6
    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...
30 6
      $this->memcached = $memcached;
31 6
      $this->installed = true;
32
33 6
      $this->setSettings();
34 6
    }
35 6
  }
36
37
  /**
38
   * set Memcached settings
39
   */
40 6
  private function setSettings()
41
  {
42
    // Use faster compression if available
43 6
    if (\Memcached::HAVE_IGBINARY) {
44
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
45
    }
46
47
    // Fix for "PHP: hhvm"
48
    if (
49 6
        defined(\Memcached::OPT_COMPRESSION_TYPE) === true
50 6
        &&
51
        defined(\Memcached::COMPRESSION_FASTLZ) === true
52 6
    ) {
53
      $this->memcached->setOption(\Memcached::OPT_COMPRESSION_TYPE, \Memcached::COMPRESSION_FASTLZ);
54
    }
55
56 6
    $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
57 6
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
58 6
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
59 6
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
60 6
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
61 6
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, true);
62 6
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
63 6
  }
64
65
  /**
66
   * set cache-item by key => value
67
   *
68
   * @param string $key
69
   * @param mixed  $value
70
   *
71
   * @return mixed|void
72
   */
73 2
  public function set($key, $value)
74
  {
75
    // Make sure we are under the proper limit
76
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
78
      throw new \Exception('The passed cache key is over 250 bytes');
79
    }
80
    */
81
82 2
    return $this->memcached->set($key, $value);
83
  }
84
85
  /**
86
   * set cache-item by key => value + ttl
87
   *
88
   * @param string $key
89
   * @param mixed  $value
90
   * @param int    $ttl
91
   *
92
   * @return boolean
93
   */
94 1
  public function setExpired($key, $value, $ttl)
95
  {
96 1
    if ($ttl > 2592000) {
97
      $ttl = 2592000;
98
    }
99
100 1
    return $this->memcached->set($key, $value, $ttl);
101
  }
102
103
  /**
104
   * remove cached-item by key
105
   *
106
   * @param string $key
107
   *
108
   * @return boolean
109
   */
110
  public function remove($key)
111
  {
112
    return $this->memcached->delete($key);
113
  }
114
115
  /**
116
   * check if cached-item exists
117
   *
118
   * @param string $key
119
   *
120
   * @return bool
121
   */
122 1
  public function exists($key)
123
  {
124 1
    return $this->get($key) !== false;
125
  }
126
127
  /**
128
   * get cached-item by key
129
   *
130
   * @param String $key
131
   *
132
   * @return mixed
133
   */
134 4 View Code Duplication
  public function get($key)
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...
135
  {
136 4
    static $memcachedCache;
137
138 4
    if (isset($memcachedCache[$key])) {
139 1
      return $memcachedCache[$key];
140
    } else {
141 3
      $return = $this->memcached->get($key);
142 3
      $memcachedCache[$key] = $return;
143 3
      return $return;
144
    }
145
  }
146
147
  /**
148
   * check if cache is installed
149
   *
150
   * @return boolean
151
   */
152 6
  public function installed()
153
  {
154 6
    return $this->installed;
155
  }
156
157
}
158