Completed
Push — master ( cd674c...612bb3 )
by Kamil
12s
created

ApiCoreTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 5.03 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.85%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 8
loc 159
ccs 41
cts 52
cp 0.7885
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
dispatch() 0 1 ?
A bgSave() 0 6 1
A bgRewriteAoF() 0 6 1
A sync() 0 6 1
A time() 0 6 1
A monitor() 0 6 1
A flushAll() 0 6 1
A flushDb() 0 6 1
C info() 0 34 8
A slaveOf() 7 7 1
A slowLog() 0 7 1
A save() 0 6 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 Dazzle\Redis\Command\Compose;
4
5
use Dazzle\Redis\Command\Builder;
6
use Dazzle\Redis\Command\Enum;
7
use Dazzle\Redis\Driver\Request;
8
9
trait ApiCoreTrait
10
{
11
    /**
12
     * @param Request $request
13
     * @return mixed
14
     */
15
    abstract function dispatch(Request $request);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
17
    /**
18
     * @override
19
     * @inheritDoc
20
     */
21 1
    public function bgRewriteAoF()
22
    {
23 1
        $command = Enum::BGREWRITEAOF;
24
25 1
        return $this->dispatch(Builder::build($command));
26
    }
27
28
    /**
29
     * @override
30
     * @inheritDoc
31
     */
32
    public function bgSave()
33
    {
34
        $command = Enum::BGSAVE;
35
36
        return $this->dispatch(Builder::build($command));
37
    }
38
39
    /**
40
     * @override
41
     * @inheritDoc
42
     */
43
    public function sync()
44
    {
45
        $command = Enum::SYNC;
46
47
        return $this->dispatch(Builder::build($command));
48
    }
49
50
    /**
51
     * @override
52
     * @inheritDoc
53
     */
54 1
    public function time()
55
    {
56 1
        $command = Enum::TIME;
57
58 1
        return $this->dispatch(Builder::build($command));
59
    }
60
61
    /**
62
     * @override
63
     * @inheritDoc
64
     */
65 1
    public function monitor()
66
    {
67 1
        $command = Enum::MONITOR;
68
69 1
        return $this->dispatch(Builder::build($command));
70
    }
71
72
    /**
73
     * @override
74
     * @inheritDoc
75
     */
76 1
    public function flushAll()
77
    {
78 1
        $command = Enum::FLUSHALL;
79
80 1
        return $this->dispatch(Builder::build($command));
81
    }
82
83
    /**
84
     * @override
85
     * @inheritDoc
86
     */
87 137
    public function flushDb()
88
    {
89 137
        $command = Enum::FLUSHDB;
90
91 137
        return $this->dispatch(Builder::build($command));
92
    }
93
94
    /**
95
     * @override
96
     * @inheritDoc
97
     */
98 137
    public function info($section = [])
99
    {
100 137
        $command = Enum::INFO;
101
102 137
        return $this->dispatch(Builder::build($command, $section))->then(function ($value) {
103 137
            if ($value) {
104 137
                $ret = explode("\r\n", $value);
105 137
                $handled = [];
106 137
                $lastKey = '';
107
108 137
                foreach ($ret as $_ => $v)
109
                {
110 137
                    if (($pos = strpos($v, '#')) !== false)
111
                    {
112 137
                        $lastKey = strtolower(substr($v,$pos+2));
113 137
                        $handled[$lastKey] = [];
114 137
                        continue;
115
                    }
116 137
                    if ($v === '') {
117 137
                        continue;
118
                    }
119 137
                    if (($statMap = explode(':', $v)) && $statMap[0] && $statMap[1])
120
                    {
121 137
                        list($name, $stat) = explode(':', $v);
122 137
                        $handled[$lastKey][$name] = $stat;
123
                    }
124
                }
125
126 137
                return $handled;
127
            }
128
129
            return $value;
130 137
        });
131
    }
132
133
    /**
134
     * @override
135
     * @inheritDoc
136
     */
137 1 View Code Duplication
    public function slaveOf($host, $port)
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...
138
    {
139 1
        $command = Enum::SLAVEOF;
140 1
        $args = [$host, $port];
141
142 1
        return $this->dispatch(Builder::build($command, $args));
143
    }
144
145
    /**
146
     * @override
147
     * @inheritDoc
148
     */
149
    public function slowLog($subCommand, array $args = [])
150
    {
151
        $command = Enum::SLOWLOG;
152
        $args = array_merge([$subCommand],$args);
153
154
        return $this->dispatch(Builder::build($command, $args));
155
    }
156
157
    /**
158
     * @override
159
     * @inheritDoc
160
     */
161 1
    public function save()
162
    {
163 1
        $command = Enum::SAVE;
164
165 1
        return $this->dispatch(Builder::build($command));
166
    }
167
}
168