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

MemcacheDriver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 23.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 23
loc 97
c 0
b 0
f 0
rs 10
wmc 11
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A connect() 13 13 2
A has() 0 4 1
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 brothers.
13
 */
14
class MemcacheDriver extends AbstractDriver
15
{
16
    /**
17
     * @var \Memcache
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 \Memcache();
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
            $server = $server + $this->defaultServer;
37
38
            $this->driver->addServer(
39
                $server['host'],
40
                $server['port'],
41
                $server['persistent'],
42
                $server['weight']
43
            );
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function has(string $name): bool
51
    {
52
        return $this->driver->get($name) !== false;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function get(string $name)
59
    {
60
        return $this->driver->get($name);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @throws \MemcachedException
67
     */
68
    public function set(string $name, $data, $ttl = null)
69
    {
70
        return $this->driver->set($name, $data, 0, $this->lifetime($ttl, 0));
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function delete(string $name)
77
    {
78
        $this->driver->delete($name);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 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...
85
    {
86
        if (!$this->has($name)) {
87
            $this->set($name, $delta);
88
89
            return $delta;
90
        }
91
92
        return $this->driver->increment($name, $delta);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function dec(string $name, int $delta = 1): int
99
    {
100
        return $this->driver->decrement($name, $delta);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function clear()
107
    {
108
        $this->driver->flush();
109
    }
110
}
111