Completed
Push — master ( 9e125a...9a1e57 )
by Kamil
02:25
created

ApiSetHashTrait::hGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
nc 1
cc 1
eloc 4
nop 2
crap 2
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
    public function hDel($key, ...$fields)
22
    {
23
        $command = Enum::HDEL;
24
        $args = [$key];
25
        $args = array_merge($args, $fields);
26
27
        return $this->dispatch(Builder::build($command, $args));
28
    }
29
30
    /**
31
     * @override
32
     * @inheritDoc
33
     */
34
    public function hGet($key, $field)
35
    {
36
        $command = Enum::HGET;
37
        $args = [$key, $field];
38
39
        return $this->dispatch(Builder::build($command, $args));
40
    }
41
42
    /**
43
     * @override
44
     * @inheritDoc
45
     */
46
    public function hGetAll($key)
47
    {
48
        $command = Enum::HGETALL;
49
        $args = [$key];
50
51
        return $this->dispatch(Builder::build($command, $args))->then(function ($value) {
52
            if (!empty($value)) {
53
                $tmp = [];
54
                $size = count($value);
55
                for ($i=0; $i<$size; $i+=2) {
56
                    $field = $value[$i];
57
                    $val = $value[$i+1];
58
                    $tmp[$field] = $val;
59
                }
60
                $value = $tmp;
61
            }
62
63
            return $value;
64
        });
65
    }
66
67
    /**
68
     * @override
69
     * @inheritDoc
70
     */
71
    public function hIncrBy($key, $field, $increment)
72
    {
73
        $command = Enum::HINCRBY;
74
        $args = [$key, $field, $increment];
75
76
        return $this->dispatch(Builder::build($command, $args));
77
    }
78
79
    /**
80
     * @override
81
     * @inheritDoc
82
     */
83
    public function hIncrByFloat($key, $field, $increment)
84
    {
85
        $command = Enum::HINCRBYFLOAT;
86
        $args = [$key, $field, $increment];
87
88
        return $this->dispatch(Builder::build($command, $args));
89
    }
90
91
    /**
92
     * @override
93
     * @inheritDoc
94
     */
95 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
        $command = Enum::HKEYS;
98
        $args = [$key];
99
100
        return $this->dispatch(Builder::build($command, $args));
101
    }
102
103
    /**
104
     * @override
105
     * @inheritDoc
106
     */
107 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
        $command = Enum::HLEN;
110
        $args = [$key];
111
112
        return $this->dispatch(Builder::build($command, $args));
113
    }
114
115
    /**
116
     * @override
117
     * @inheritDoc
118
     */
119
    public function hMGet($key, ...$fields)
120
    {
121
        $command = Enum::HMGET;
122
        $args = [$key];
123
        $args = array_merge($args, $fields);
124
125
        return $this->dispatch(Builder::build($command, $args));
126
    }
127
128
    /**
129
     * @override
130
     * @inheritDoc
131
     */
132
    public function hMSet($key, array $fvMap)
133
    {
134
        $command = Enum::HMSET;
135
        $args = [$key];
136
        if (!empty($fvMap)) {
137
            foreach ($fvMap as $field => $value) {
138
                $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...
139
                $tmp[] = $value;
140
            }
141
            $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...
142
        }
143
        $args = array_merge($args, $fvMap);
144
145
        return $this->dispatch(Builder::build($command, $args));
146
    }
147
148
    /**
149
     * @override
150
     * @inheritDoc
151
     */
152
    public function hSet($key, $field, $value)
153
    {
154
        $command = Enum::HSET;
155
        $args = [$key, $field, $value];
156
157
        return $this->dispatch(Builder::build($command, $args));
158
    }
159
160
    /**
161
     * @override
162
     * @inheritDoc
163
     */
164
    public function hSetNx($key, $filed, $value)
165
    {
166
        $command = Enum::HSETNX;
167
        $args = [$key, $filed, $value];
168
169
        return $this->dispatch(Builder::build($command, $args));
170
    }
171
172
    /**
173
     * @override
174
     * @inheritDoc
175
     */
176
    public function hStrLen($key, $field)
177
    {
178
        $command = Enum::HSTRLEN;
179
        $args = [$key, $field];
180
181
        return $this->dispatch(Builder::build($command, $args));
182
    }
183
184
    /**
185
     * @override
186
     * @inheritDoc
187
     */
188 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...
189
    {
190
        $command = Enum::HVALS;
191
        $args = [$key];
192
193
        return $this->dispatch(Builder::build($command, $args));
194
    }
195
196
    /**
197
     * @override
198
     * @inheritDoc
199
     */
200
    public function hScan($key, $cursor, array $options = [])
201
    {
202
        // TODO: Implement hScan() method.
203
        $command = Enum::HSCAN;
204
        $args = [$key, $cursor];
205
        $args = array_merge($args, $options);
206
207
        return $this->dispatch(Builder::build($command, $args));
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function hExists($key, $field)
214
    {
215
        // TODO: Implement hExists() method.
216
        $command = Enum::HEXISTS;
217
        $args = [$key, $field];
218
219
        return $this->dispatch(Builder::build($command, $args));
220
    }
221
}
222