Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

MemcachedDriver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 24
loc 102
c 0
b 0
f 0
rs 10
wmc 12
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A connect() 14 14 2
A has() 0 8 2
A get() 0 4 1
A set() 0 4 1
A delete() 0 4 1
A inc() 10 10 2
A dec() 0 4 1
A clear() 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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Cache\Stores\Memcache;
10
11
/**
12
 * Two sisters.
13
 */
14
class MemcachedDriver extends AbstractDriver
15
{
16
    /**
17
     * @var \Memcached
18
     */
19
    protected $driver = null;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function __construct(array $servers)
25
    {
26
        $this->servers = $servers;
27
        $this->driver = new \Memcached();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    public function connect()
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...
34
    {
35
        foreach ($this->servers as $server) {
36
37
            //Merging default options
38
            $server = $server + $this->defaultServer;
39
40
            $this->driver->addServer(
41
                $server['host'],
42
                $server['port'],
43
                $server['weight']
44
            );
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function has(string $name): bool
52
    {
53
        if ($this->driver->get($name) === false) {
54
            return $this->driver->getResultCode() != \Memcached::RES_NOTFOUND;
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get(string $name)
64
    {
65
        return $this->driver->get($name);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws \MemcachedException
72
     */
73
    public function set(string $name, $data, $ttl = null)
74
    {
75
        return $this->driver->set($name, $data, $this->lifetime($ttl));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function delete(string $name)
82
    {
83
        $this->driver->delete($name);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 View Code Duplication
    public function inc(string $name, int $delta = 1): int
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
        if (!$this->has($name)) {
92
            $this->set($name, $delta);
93
94
            return $delta;
95
        }
96
97
        return $this->driver->increment($name, $delta);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function dec(string $name, int $delta = 1): int
104
    {
105
        return $this->driver->decrement($name, $delta);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function clear()
112
    {
113
        $this->driver->flush();
114
    }
115
}
116