Completed
Push — master ( b586f8...cd674c )
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 0%

Importance

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

22 Methods

Rating   Name   Duplication   Size   Complexity  
dispatch() 0 1 ?
A zInterStore() 0 7 1
A zScore() 0 7 1
A zScan() 0 8 1
A zUnionScore() 0 7 1
A zAdd() 7 7 1
A zCard() 7 7 1
A zCount() 7 7 1
A zIncrBy() 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

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 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
        $command = Enum::ZADD;
24
        $args = array_merge([$key], $options, $scoreMembers);
25
26
        return $this->dispatch(Builder::build($command, $args));
27
    }
28
29
    /**
30
     * @override
31
     * @inheritDoc
32
     */
33 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
        $command = Enum::ZCARD;
36
        $args = [$key];
37
38
        return $this->dispatch(Builder::build($command, $args));
39
    }
40
41
    /**
42
     * @override
43
     * @inheritDoc
44
     */
45 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
        $command = Enum::ZCOUNT;
48
        $args = [$key, $min, $max];
49
50
        return $this->dispatch(Builder::build($command, $args));
51
    }
52
53
    /**
54
     * @override
55
     * @inheritDoc
56
     */
57
    public function zIncrBy($key, $increment, $member)
58
    {
59
        $command = Enum::ZINCRBY;
60
        $args = [$key, $increment, $member];
61
62
        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 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
        $command = Enum::ZLEXCOUNT;
84
        $args = [$key, $min, $max];
85
86
        return $this->dispatch(Builder::build($command, $args));
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93
    public function zRange($key, $star = 0, $stop = -1, $withScores = false)
94
    {
95
        $command = Enum::ZRANGE;
96
        $args = [$key, $star, $stop];
97
        if ($withScores) {
98
            $args[] = 'WITHSCORES';
99
            return $this->dispatch(Builder::build($command, $args))->then(function ($value) {
100
                $len = count($value);
101
                $ret = [];
102
                for ($i=0; $i<$len; $i+=2) {
103
                    $ret[$value[$i]] = $value[$i+1];
104
                }
105
                return $ret;
106
            });
107
        }
108
109
        return $this->dispatch(Builder::build($command, $args));
110
    }
111
112
    /**
113
     * @override
114
     * @inheritDoc
115
     */
116
    public function zRangeByLex($key, $min, $max, array $options = [])
117
    {
118
        $command = Enum::ZRANGEBYLEX;
119
        $args = [$key, $min, $max];
120
        $args = array_merge($args, $options);
121
122
        return $this->dispatch(Builder::build($command, $args));
123
    }
124
125
    /**
126
     * @override
127
     * @inheritDoc
128
     */
129
    public function zRevRangeByLex($key, $max, $min, array $options = [])
130
    {
131
        $command = Enum::ZREVRANGEBYLEX;
132
        $args = [$key, $max,$min];
133
        $args = array_merge($args,$options);
134
135
        return $this->dispatch(Builder::build($command, $args));
136
    }
137
138
    /**
139
     * @override
140
     * @inheritDoc
141
     */
142 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
        $command = Enum::ZRANGEBYSCORE;
145
        $args = [$key, $min, $max];
146
        if ($withScores === true) {
147
            $args[] = 'WITHSCORES';
148
        }
149
        if ($offset != 0 || $count != 0) {
150
            $args[] = 'LIMIT';
151
            $args[] = $offset;
152
            $args[] = $count;
153
        }
154
        $promise = $this->dispatch(Builder::build($command, $args));
155
156
        return $withScores ? $promise->then(function ($value) {
157
            $len = is_array($value) ? count($value) : 0;
158
            if ($len > 0) {
159
                $ret = [];
160
                for ($i=0; $i<$len; $i+=2) {
161
                    $ret[$value[$i]] = $value[$i+1];
162
                }
163
164
                return $ret;
165
            }
166
167
            return $value;
168
        } ) : $promise;
169
    }
170
171
    /**
172
     * @override
173
     * @inheritDoc
174
     */
175
    public function zRank($key, $member)
176
    {
177
        $command = Enum::ZRANK;
178
        $args = [$key,$member];
179
180
        return $this->dispatch(Builder::build($command, $args));
181
    }
182
183
    /**
184
     * @override
185
     * @inheritDoc
186
     */
187
    public function zRem($key, ...$members)
188
    {
189
        $command = Enum::ZREM;
190
        $args = [$key];
191
        $args = array_merge($args, $members);
192
193
        return $this->dispatch(Builder::build($command, $args));
194
    }
195
196
    /**
197
     * @override
198
     * @inheritDoc
199
     */
200
    public function zRemRangeByLex($key, $min, $max, array $options = [])
201
    {
202
        $command = Enum::ZREMRANGEBYLEX;
203
        $args = [$key, $min, $max];
204
        $args = array_merge($args, $options);
205
206
        return $this->dispatch(Builder::build($command, $args));
207
    }
208
209
    /**
210
     * @override
211
     * @inheritDoc
212
     */
213
    public function zRemRangeByRank($key, $start, $stop)
214
    {
215
        $command = Enum::ZREMRANGEBYRANK;
216
        $args = [$key, $start,$stop];
217
218
        return $this->dispatch(Builder::build($command, $args));
219
    }
220
221
    /**
222
     * @override
223
     * @inheritDoc
224
     */
225
    public function zRemRangeByScore($key, $min, $max, array $options = [])
226
    {
227
        $command = Enum::ZREMRANGEBYSCORE;
228
        $args = [$key, $min, $max];
229
        $args = array_merge($args, $options);
230
231
        return $this->dispatch(Builder::build($command, $args));
232
    }
233
234
    /**
235
     * @override
236
     * @inheritDoc
237
     */
238
    public function zRevRange($key, $start, $stop, $withScores = false)
239
    {
240
        $command = Enum::ZREVRANGE;
241
        $args = [$key, $start, $stop];
242
243
        if ($withScores === true) {
244
            $args[] = 'WITHSCORES';
245
            
246
            return $this->dispatch(Builder::build($command, $args))
247
            ->then(function ($value) {
248
                $len = is_array($value) ? count($value) : 0;
249
                if ($len > 0) {
250
                    $ret = [];
251
                    for ($i=0; $i<$len; $i+=2) {
252
                        $member = $value[$i];
253
                        $score = $value[$i+1];
254
                        $ret[$member] = $score;
255
                    }
256
257
                    return $ret;
258
                }
259
260
                return $value; 
261
            });
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 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
        $command = Enum::ZREVRANGEBYSCORE;
274
        $args = [$key, $max, $min];
275
        if ($withScores === true) {
276
            $args[] = 'WITHSCORES';
277
        }
278
        if ($offset != 0 || $count != 0) {
279
            $args[] = 'LIMIT';
280
            $args[] = $offset;
281
            $args[] = $count;
282
        }
283
        $promise = $this->dispatch(Builder::build($command, $args));
284
285
        return $withScores ? $promise->then(function ($value) {
286
            $len = is_array($value) ? count($value) : 0;
287
            if ($len > 0) {
288
                $ret = [];
289
                for ($i=0; $i<$len; $i+=2) {
290
                    $ret[$value[$i]] = $value[$i+1];
291
                }
292
293
                return $ret;
294
            }
295
296
            return $value;
297
        } ) : $promise;
298
    }
299
300
    /**
301
     * @override
302
     * @inheritDoc
303
     */
304
    public function zRevRank($key, $member)
305
    {
306
        $command = Enum::ZREVRANK;
307
        $args = [$key,$member];
308
309
        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
}