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

ApiSetSortedTrait   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 339
Duplicated Lines 25.66 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.29%

Importance

Changes 0
Metric Value
dl 87
loc 339
c 0
b 0
f 0
wmc 41
lcom 1
cbo 1
ccs 118
cts 140
cp 0.8429
rs 8.2769

22 Methods

Rating   Name   Duplication   Size   Complexity  
dispatch() 0 1 ?
A zAdd() 7 7 1
A zCard() 7 7 1
A zCount() 7 7 1
A zIncrBy() 0 7 1
A zInterStore() 0 7 1
A zLexCount() 7 7 1
A zRange() 0 18 3
A zRangeByLex() 0 8 1
A zRevRangeByLex() 0 8 1
C zRangeByScore() 28 28 8
A zRank() 0 7 1
A zRem() 0 8 1
A zRemRangeByLex() 0 8 1
A zRemRangeByRank() 0 7 1
A zRemRangeByScore() 0 8 1
B zRevRange() 0 28 5
C zRevRangeByScore() 28 28 8
A zRevRank() 0 7 1
A zScore() 0 7 1
A zScan() 0 8 1
A zUnionScore() 0 7 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ApiSetSortedTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ApiSetSortedTrait, and based on these observations, apply Extract Interface, too.

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 ApiSetSortedTrait
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 16 View Code Duplication
    public function zAdd($key, array $options = [], ...$scoreMembers)
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...
22
    {
23 16
        $command = Enum::ZADD;
24 16
        $args = array_merge([$key], $options, $scoreMembers);
25
26 16
        return $this->dispatch(Builder::build($command, $args));
27
    }
28
29
    /**
30
     * @override
31
     * @inheritDoc
32
     */
33 1 View Code Duplication
    public function zCard($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...
34
    {
35 1
        $command = Enum::ZCARD;
36 1
        $args = [$key];
37
38 1
        return $this->dispatch(Builder::build($command, $args));
39
    }
40
41
    /**
42
     * @override
43
     * @inheritDoc
44
     */
45 1 View Code Duplication
    public function zCount($key, $min, $max)
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...
46
    {
47 1
        $command = Enum::ZCOUNT;
48 1
        $args = [$key, $min, $max];
49
50 1
        return $this->dispatch(Builder::build($command, $args));
51
    }
52
53
    /**
54
     * @override
55
     * @inheritDoc
56
     */
57 1
    public function zIncrBy($key, $increment, $member)
58
    {
59 1
        $command = Enum::ZINCRBY;
60 1
        $args = [$key, $increment, $member];
61
62 1
        return $this->dispatch(Builder::build($command, $args));
63
    }
64
65
    /**
66
     * @override
67
     * @inheritDoc
68
     */
69
    public function zInterStore($dst, $numKeys)
70
    {
71
        $command = Enum::ZINTERSTORE;
72
        $args = [$dst, $numKeys];
73
74
        return $this->dispatch(Builder::build($command, $args));
75
    }
76
77
    /**
78
     * @override
79
     * @inheritDoc
80
     */
81 1 View Code Duplication
    public function zLexCount($key, $min, $max)
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...
82
    {
83 1
        $command = Enum::ZLEXCOUNT;
84 1
        $args = [$key, $min, $max];
85
86 1
        return $this->dispatch(Builder::build($command, $args));
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93 1
    public function zRange($key, $star = 0, $stop = -1, $withScores = false)
94
    {
95 1
        $command = Enum::ZRANGE;
96 1
        $args = [$key, $star, $stop];
97 1
        if ($withScores) {
98 1
            $args[] = 'WITHSCORES';
99
            return $this->dispatch(Builder::build($command, $args))->then(function ($value) {
100 1
                $len = count($value);
101 1
                $ret = [];
102 1
                for ($i=0; $i<$len; $i+=2) {
103 1
                    $ret[$value[$i]] = $value[$i+1];
104
                }
105 1
                return $ret;
106 1
            });
107
        }
108
109
        return $this->dispatch(Builder::build($command, $args));
110
    }
111
112
    /**
113
     * @override
114
     * @inheritDoc
115
     */
116 1
    public function zRangeByLex($key, $min, $max, array $options = [])
117
    {
118 1
        $command = Enum::ZRANGEBYLEX;
119 1
        $args = [$key, $min, $max];
120 1
        $args = array_merge($args, $options);
121
122 1
        return $this->dispatch(Builder::build($command, $args));
123
    }
124
125
    /**
126
     * @override
127
     * @inheritDoc
128
     */
129 1
    public function zRevRangeByLex($key, $max, $min, array $options = [])
130
    {
131 1
        $command = Enum::ZREVRANGEBYLEX;
132 1
        $args = [$key, $max,$min];
133 1
        $args = array_merge($args,$options);
134
135 1
        return $this->dispatch(Builder::build($command, $args));
136
    }
137
138
    /**
139
     * @override
140
     * @inheritDoc
141
     */
142 1 View Code Duplication
    public function zRangeByScore($key, $min, $max, $withScores = false, $offset = 0, $count = 0)
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...
143
    {
144 1
        $command = Enum::ZRANGEBYSCORE;
145 1
        $args = [$key, $min, $max];
146 1
        if ($withScores === true) {
147 1
            $args[] = 'WITHSCORES';
148
        }
149 1
        if ($offset != 0 || $count != 0) {
150 1
            $args[] = 'LIMIT';
151 1
            $args[] = $offset;
152 1
            $args[] = $count;
153
        }
154 1
        $promise = $this->dispatch(Builder::build($command, $args));
155
156
        return $withScores ? $promise->then(function ($value) {
157 1
            $len = is_array($value) ? count($value) : 0;
158 1
            if ($len > 0) {
159 1
                $ret = [];
160 1
                for ($i=0; $i<$len; $i+=2) {
161 1
                    $ret[$value[$i]] = $value[$i+1];
162
                }
163
164 1
                return $ret;
165
            }
166
167
            return $value;
168 1
        } ) : $promise;
169
    }
170
171
    /**
172
     * @override
173
     * @inheritDoc
174
     */
175 1
    public function zRank($key, $member)
176
    {
177 1
        $command = Enum::ZRANK;
178 1
        $args = [$key,$member];
179
180 1
        return $this->dispatch(Builder::build($command, $args));
181
    }
182
183
    /**
184
     * @override
185
     * @inheritDoc
186
     */
187 1
    public function zRem($key, ...$members)
188
    {
189 1
        $command = Enum::ZREM;
190 1
        $args = [$key];
191 1
        $args = array_merge($args, $members);
192
193 1
        return $this->dispatch(Builder::build($command, $args));
194
    }
195
196
    /**
197
     * @override
198
     * @inheritDoc
199
     */
200 1
    public function zRemRangeByLex($key, $min, $max, array $options = [])
201
    {
202 1
        $command = Enum::ZREMRANGEBYLEX;
203 1
        $args = [$key, $min, $max];
204 1
        $args = array_merge($args, $options);
205
206 1
        return $this->dispatch(Builder::build($command, $args));
207
    }
208
209
    /**
210
     * @override
211
     * @inheritDoc
212
     */
213 1
    public function zRemRangeByRank($key, $start, $stop)
214
    {
215 1
        $command = Enum::ZREMRANGEBYRANK;
216 1
        $args = [$key, $start,$stop];
217
218 1
        return $this->dispatch(Builder::build($command, $args));
219
    }
220
221
    /**
222
     * @override
223
     * @inheritDoc
224
     */
225 1
    public function zRemRangeByScore($key, $min, $max, array $options = [])
226
    {
227 1
        $command = Enum::ZREMRANGEBYSCORE;
228 1
        $args = [$key, $min, $max];
229 1
        $args = array_merge($args, $options);
230
231 1
        return $this->dispatch(Builder::build($command, $args));
232
    }
233
234
    /**
235
     * @override
236
     * @inheritDoc
237
     */
238 1
    public function zRevRange($key, $start, $stop, $withScores = false)
239
    {
240 1
        $command = Enum::ZREVRANGE;
241 1
        $args = [$key, $start, $stop];
242
243 1
        if ($withScores === true) {
244 1
            $args[] = 'WITHSCORES';
245
            
246 1
            return $this->dispatch(Builder::build($command, $args))
247
            ->then(function ($value) {
248 1
                $len = is_array($value) ? count($value) : 0;
249 1
                if ($len > 0) {
250 1
                    $ret = [];
251 1
                    for ($i=0; $i<$len; $i+=2) {
252 1
                        $member = $value[$i];
253 1
                        $score = $value[$i+1];
254 1
                        $ret[$member] = $score;
255
                    }
256
257 1
                    return $ret;
258
                }
259
260
                return $value; 
261 1
            });
262
        } 
263
264
        return $promise = $this->dispatch(Builder::build($command, $args));
0 ignored issues
show
Unused Code introduced by
$promise is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
265
    }
266
267
    /**
268
     * @override
269
     * @inheritDoc
270
     */
271 1 View Code Duplication
    public function zRevRangeByScore($key, $max, $min, $withScores = false, $offset = 0, $count = 0)
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...
272
    {
273 1
        $command = Enum::ZREVRANGEBYSCORE;
274 1
        $args = [$key, $max, $min];
275 1
        if ($withScores === true) {
276 1
            $args[] = 'WITHSCORES';
277
        }
278 1
        if ($offset != 0 || $count != 0) {
279 1
            $args[] = 'LIMIT';
280 1
            $args[] = $offset;
281 1
            $args[] = $count;
282
        }
283 1
        $promise = $this->dispatch(Builder::build($command, $args));
284
285 1
        return $withScores ? $promise->then(function ($value) {
286 1
            $len = is_array($value) ? count($value) : 0;
287 1
            if ($len > 0) {
288 1
                $ret = [];
289 1
                for ($i=0; $i<$len; $i+=2) {
290 1
                    $ret[$value[$i]] = $value[$i+1];
291
                }
292
293 1
                return $ret;
294
            }
295
296
            return $value;
297 1
        } ) : $promise;
298
    }
299
300
    /**
301
     * @override
302
     * @inheritDoc
303
     */
304 1
    public function zRevRank($key, $member)
305
    {
306 1
        $command = Enum::ZREVRANK;
307 1
        $args = [$key,$member];
308
309 1
        return $this->dispatch(Builder::build($command, $args));
310
    }
311
312
    /**
313
     * @override
314
     * @inheritDoc
315
     */
316
    public function zScore($key, $member)
317
    {
318
        $command = Enum::ZSCORE;
319
        $args = [$key,$member];
320
321
        return $this->dispatch(Builder::build($command, $args));
322
    }
323
324
    /**
325
     * @override
326
     * @inheritDoc
327
     */
328
    public function zScan($key, $cursor, array $options = [])
329
    {
330
        $command = Enum::ZSCAN;
331
        $args = [$key , $cursor];
332
        $args = array_merge($args, $options);
333
334
        return $this->dispatch(Builder::build($command, $args));
335
    }
336
337
    /**
338
     * @inheritDoc
339
     */
340
    public function zUnionScore($dst, $numKeys)
341
    {
342
        $command = Enum::ZUNIIONSCORE;
343
        $args = [$dst, $numKeys];
344
345
        return $this->dispatch(Builder::build($command, $args));
346
    }
347
}