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

ApiSetHashTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 213
Duplicated Lines 10.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 23
loc 213
ccs 79
cts 79
cp 1
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
dispatch() 0 1 ?
A hDel() 0 8 1
A hGet() 0 7 1
A hGetAll() 0 20 3
A hIncrBy() 0 7 1
A hIncrByFloat() 0 7 1
A hKeys() 7 7 1
A hLen() 7 7 1
A hMGet() 0 8 1
A hMSet() 0 16 3
A hSet() 0 7 1
A hSetNx() 0 7 1
A hStrLen() 0 7 1
A hVals() 7 7 1
A hScan() 0 9 1
A hExists() 0 7 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 ApiSetHashTrait
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 hDel($key, ...$fields)
22
    {
23 1
        $command = Enum::HDEL;
24 1
        $args = [$key];
25 1
        $args = array_merge($args, $fields);
26
27 1
        return $this->dispatch(Builder::build($command, $args));
28
    }
29
30
    /**
31
     * @override
32
     * @inheritDoc
33
     */
34 1
    public function hGet($key, $field)
35
    {
36 1
        $command = Enum::HGET;
37 1
        $args = [$key, $field];
38
39 1
        return $this->dispatch(Builder::build($command, $args));
40
    }
41
42
    /**
43
     * @override
44
     * @inheritDoc
45
     */
46 1
    public function hGetAll($key)
47
    {
48 1
        $command = Enum::HGETALL;
49 1
        $args = [$key];
50
51 1
        return $this->dispatch(Builder::build($command, $args))->then(function ($value) {
52 1
            if (!empty($value)) {
53 1
                $tmp = [];
54 1
                $size = count($value);
55 1
                for ($i=0; $i<$size; $i+=2) {
56 1
                    $field = $value[$i];
57 1
                    $val = $value[$i+1];
58 1
                    $tmp[$field] = $val;
59
                }
60 1
                $value = $tmp;
61
            }
62
63 1
            return $value;
64 1
        });
65
    }
66
67
    /**
68
     * @override
69
     * @inheritDoc
70
     */
71 1
    public function hIncrBy($key, $field, $increment)
72
    {
73 1
        $command = Enum::HINCRBY;
74 1
        $args = [$key, $field, $increment];
75
76 1
        return $this->dispatch(Builder::build($command, $args));
77
    }
78
79
    /**
80
     * @override
81
     * @inheritDoc
82
     */
83 1
    public function hIncrByFloat($key, $field, $increment)
84
    {
85 1
        $command = Enum::HINCRBYFLOAT;
86 1
        $args = [$key, $field, $increment];
87
88 1
        return $this->dispatch(Builder::build($command, $args));
89
    }
90
91
    /**
92
     * @override
93
     * @inheritDoc
94
     */
95 1 View Code Duplication
    public function hKeys($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...
96
    {
97 1
        $command = Enum::HKEYS;
98 1
        $args = [$key];
99
100 1
        return $this->dispatch(Builder::build($command, $args));
101
    }
102
103
    /**
104
     * @override
105
     * @inheritDoc
106
     */
107 1 View Code Duplication
    public function hLen($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...
108
    {
109 1
        $command = Enum::HLEN;
110 1
        $args = [$key];
111
112 1
        return $this->dispatch(Builder::build($command, $args));
113
    }
114
115
    /**
116
     * @override
117
     * @inheritDoc
118
     */
119 1
    public function hMGet($key, ...$fields)
120
    {
121 1
        $command = Enum::HMGET;
122 1
        $args = [$key];
123 1
        $args = array_merge($args, $fields);
124
125 1
        return $this->dispatch(Builder::build($command, $args));
126
    }
127
128
    /**
129
     * @override
130
     * @inheritDoc
131
     */
132 7
    public function hMSet($key, array $fvMap)
133
    {
134
        //TODO: replace param $fvMap to ...$fvs,cauz hash map not allow duplicate key
135 7
        $command = Enum::HMSET;
136 7
        $args = [$key];
137 7
        if (!empty($fvMap)) {
138 7
            foreach ($fvMap as $field => $value) {
139 7
                $tmp[] = $field;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tmp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tmp = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
140 7
                $tmp[] = $value;
141
            }
142 7
            $fvMap = $tmp;
0 ignored issues
show
Bug introduced by
The variable $tmp does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
143
        }
144 7
        $args = array_merge($args, $fvMap);
145
146 7
        return $this->dispatch(Builder::build($command, $args));
147
    }
148
149
    /**
150
     * @override
151
     * @inheritDoc
152
     */
153 8
    public function hSet($key, $field, $value)
154
    {
155 8
        $command = Enum::HSET;
156 8
        $args = [$key, $field, $value];
157
158 8
        return $this->dispatch(Builder::build($command, $args));
159
    }
160
161
    /**
162
     * @override
163
     * @inheritDoc
164
     */
165 1
    public function hSetNx($key, $filed, $value)
166
    {
167 1
        $command = Enum::HSETNX;
168 1
        $args = [$key, $filed, $value];
169
170 1
        return $this->dispatch(Builder::build($command, $args));
171
    }
172
173
    /**
174
     * @override
175
     * @inheritDoc
176
     */
177 1
    public function hStrLen($key, $field)
178
    {
179 1
        $command = Enum::HSTRLEN;
180 1
        $args = [$key, $field];
181
182 1
        return $this->dispatch(Builder::build($command, $args));
183
    }
184
185
    /**
186
     * @override
187
     * @inheritDoc
188
     */
189 1 View Code Duplication
    public function hVals($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...
190
    {
191 1
        $command = Enum::HVALS;
192 1
        $args = [$key];
193
194 1
        return $this->dispatch(Builder::build($command, $args));
195
    }
196
197
    /**
198
     * @override
199
     * @inheritDoc
200
     */
201 1
    public function hScan($key, $cursor, array $options = [])
202
    {
203
        // TODO: Implement hScan() method.
204 1
        $command = Enum::HSCAN;
205 1
        $args = [$key, $cursor];
206 1
        $args = array_merge($args, $options);
207
208 1
        return $this->dispatch(Builder::build($command, $args));
209
    }
210
211
    /**
212
     * @inheritDoc
213
     */
214 1
    public function hExists($key, $field)
215
    {
216 1
        $command = Enum::HEXISTS;
217 1
        $args = [$key, $field];
218
219 1
        return $this->dispatch(Builder::build($command, $args));
220
    }
221
}
222