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

AdapterMemcache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 6.77 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
c 1
b 1
f 0
lcom 1
cbo 1
dl 9
loc 133
ccs 25
cts 35
cp 0.7143
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A installed() 0 4 1
A remove() 0 4 1
A removeAll() 0 4 1
A __construct() 0 6 2
A setMemcache() 0 4 1
A exists() 0 4 1
A get() 0 4 1
A set() 9 9 2
A setExpired() 0 8 2
A getCompressedFlag() 0 4 2
A isCompressed() 0 4 1
A setCompressed() 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
use voku\cache\Exception\InvalidArgumentException;
6
7
/**
8
 * AdapterMemcache: Memcache-adapter
9
 *
10
 * @package voku\cache
11
 */
12
class AdapterMemcache implements iAdapter
13
{
14
  /**
15
   * @var bool
16
   */
17
  public $installed = false;
18
19
  /**
20
   * @var \Memcache
21
   */
22
  private $memcache;
23
24
  /**
25
   * @var boolean
26
   */
27
  private $compressed = false;
28
29
  /**
30
   * __construct
31
   *
32
   * @param \Memcache|null $memcache
33
   */
34 8
  public function __construct($memcache = null)
35
  {
36 8
    if ($memcache instanceof \Memcache) {
0 ignored issues
show
Bug introduced by
The class Memcache 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...
37 8
      $this->setMemcache($memcache);
38 8
    }
39 8
  }
40
41
  /**
42
   * @param \Memcache $memcache
43
   */
44 8
  public function setMemcache(\Memcache $memcache) {
45 8
    $this->memcache = $memcache;
46 8
    $this->installed = true;
47 8
  }
48
49
  /**
50
   * @inheritdoc
51
   */
52 1
  public function exists($key)
53
  {
54 1
    return $this->get($key) !== false;
55
  }
56
57
  /**
58
   * @inheritdoc
59
   */
60 3
  public function get($key)
61
  {
62 3
    return $this->memcache->get($key);
63
  }
64
65
  /**
66
   * @inheritdoc
67
   */
68 8
  public function installed()
69
  {
70 8
    return $this->installed;
71
  }
72
73
  /**
74
   * @inheritdoc
75
   */
76
  public function remove($key)
77
  {
78
    return $this->memcache->delete($key);
79
  }
80
81
  /**
82
   * @inheritdoc
83
   */
84
  public function removeAll()
85
  {
86
    return $this->memcache->flush();
87
  }
88
89
  /**
90
   * @inheritdoc
91
   */
92 2 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...
93
  {
94
    // Make sure we are under the proper limit
95 2
    if (strlen($key) > 250) {
96
      throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true));
97
    }
98
99 2
    return $this->memcache->set($key, $value, $this->getCompressedFlag());
100
  }
101
102
  /**
103
   * @inheritdoc
104
   */
105 1
  public function setExpired($key, $value, $ttl)
106
  {
107 1
    if ($ttl > 2592000) {
108
      $ttl = 2592000;
109
    }
110
111 1
    return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
112
  }
113
114
  /**
115
   * Get the compressed-flag from MemCache.
116
   *
117
   * @return int 2 || 0
118
   */
119 3
  private function getCompressedFlag()
120
  {
121 3
    return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0;
122
  }
123
124
  /**
125
   * Check if compression from MemCache is active.
126
   *
127
   * @return boolean
128
   */
129 3
  public function isCompressed()
130
  {
131 3
    return $this->compressed;
132
  }
133
134
  /**
135
   * Activate the compression from MemCache.
136
   *
137
   * @param mixed $value will be converted to boolean
138
   */
139
  public function setCompressed($value)
140
  {
141
    $this->compressed = (bool)$value;
142
  }
143
144
}
145