Strings   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 293
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 1

19 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 4 1
A decr() 0 4 1
A decrBy() 0 4 1
A get() 0 4 1
A getRange() 0 4 1
A getSet() 0 4 1
A incr() 0 4 1
A incrBy() 0 4 1
A incrByFloat() 0 4 1
A mGet() 0 4 1
A getMultiple() 0 4 1
A mSet() 0 8 2
A mSetNX() 0 8 2
A set() 0 8 2
A setEx() 0 4 1
A pSetEx() 0 4 1
A setNx() 0 4 1
A setRange() 0 4 1
A strLen() 0 4 1
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\NotAssociativeArrayException;
6
7
trait Strings
8
{
9
    /**
10
     * Append specified string to the string stored in specified key.
11
     * See: https://redis.io/commands/append.
12
     *
13
     * @param  string $key
14
     * @param  string $value
15
     *
16
     * @return int              Size of the value after the append.
17
     */
18
    public function append(string $key, string $value): int
19
    {
20
        return $this->redis->append($key, $value);
0 ignored issues
show
Bug introduced by
The property redis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    /**
24
     * Decrement the number stored at key by one.
25
     * See: https://redis.io/commands/decr.
26
     *
27
     * @param  string $key
28
     *
29
     * @return int          the new value
30
     */
31
    public function decr(string $key): int
32
    {
33
        return $this->redis->decr($key);
34
    }
35
36
    /**
37
     * Decrement the number stored at key by the given decrement.
38
     * See: https://redis.io/commands/decrby.
39
     *
40
     * @param  string $key
41
     * @param  int    $decrement
42
     *
43
     * @return int              The new value
44
     */
45
    public function decrBy(string $key, int $decrement): int
46
    {
47
        return $this->redis->decrBy($key, $decrement);
48
    }
49
50
    /**
51
     * Get the value related to the specified key.
52
     * See: https://redis.io/commands/get.
53
     *
54
     * @param  string $key
55
     *
56
     * @return mixed|string|bool    If key didn't exist, FALSE is returned.
57
     *                              Otherwise, the value related to this key
58
     *                              is returned.
59
     */
60
    public function get(string $key)
61
    {
62
        return $this->redis->get($key);
63
    }
64
65
    /**
66
     * Return a substring of a larger string.
67
     * See: https://redis.io/commands/getrange.
68
     *
69
     * @param  string $key
70
     * @param  int    $start
71
     * @param  int    $end
72
     *
73
     * @return string       the substring
74
     */
75
    public function getRange(string $key, int $start, int $end): string
76
    {
77
        return $this->redis->getRange($key, $start, $end);
78
    }
79
80
    /**
81
     * Atomically sets key to value and returns the old value stored at key.
82
     * Returns an error when key exists but does not hold a string value.
83
     * See: https://redis.io/commands/getset.
84
     *
85
     * @param  string $key
86
     * @param  string $value
87
     *
88
     * @return string       Bulk string reply: the old value stored at key,
89
     *                      or nil when key did not exist.
90
     */
91
    public function getSet(string $key, string $value): string
92
    {
93
        return $this->redis->getSet($key, $value);
94
    }
95
96
    /**
97
     * Increment the number stored at key by one.
98
     * See: https://redis.io/commands/incr.
99
     *
100
     * @param  string $key
101
     *
102
     * @return int         The new value.
103
     */
104
    public function incr(string $key): int
105
    {
106
        return $this->redis->incr($key);
107
    }
108
109
    /**
110
     * Increment the number stored at key by the given increment.
111
     * See: https://redis.io/commands/incrby.
112
     *
113
     * @param  string $key
114
     * @param  int    $increment
115
     *
116
     * @return int                  The new value
117
     */
118
    public function incrBy(string $key, int $increment): int
119
    {
120
        return $this->redis->incrBy($key, $increment);
121
    }
122
123
    /**
124
     * Increment the number stored at key by the given increment.
125
     * See: https://redis.io/commands/incrbyfloat.
126
     *
127
     * @param  string $key
128
     * @param  float  $increment
129
     *
130
     * @return float                The new value.
131
     */
132
    public function incrByFloat(string $key, float $increment): float
133
    {
134
        return $this->redis->incrByFloat($key, $increment);
135
    }
136
137
    /**
138
     * Get the values of all the specified keys. If one or more keys don't
139
     * exist, the array will contain FALSE at the position of the key.
140
     * See: https://redis.io/commands/mget.
141
     *
142
     * @param  array  $keys
143
     *
144
     * @return array        Array reply: list of values at the specified keys.
145
     */
146
    public function mGet(array $keys): array
147
    {
148
        return $this->redis->mGet($keys);
149
    }
150
151
    /**
152
     * Get the values of all the specified keys. If one or more keys don't
153
     * exist, the array will contain FALSE at the position of the key.
154
     * See: https://redis.io/commands/mget.
155
     *
156
     * @param  array  $keys
157
     *
158
     * @return array        Array reply: list of values at the specified keys.
159
     */
160
    public function getMultiple(array $keys): array
161
    {
162
        return $this->redis->mGet($keys);
163
    }
164
165
    /**
166
     * Sets multiple key-value pairs in one atomic command.
167
     * See: https://redis.io/commands/mset.
168
     *
169
     * @param  array  $pairs
170
     *
171
     * @return bool         TRUE in case of success, FALSE in case of failure.
172
     */
173
    public function mSet(array $pairs): bool
174
    {
175
        if (!$this->is_associative($pairs)) {
0 ignored issues
show
Bug introduced by
It seems like is_associative() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
176
            throw new NotAssociativeArrayException('The array provided is not associative.', 1);
177
        }
178
179
        return $this->redis->mSet($pairs);
180
    }
181
182
    /**
183
     * Sets multiple key-value pairs in one atomic command. MSETNX only returns
184
     * TRUE if all the keys were set (see SETNX).
185
     * See: https://redis.io/commands/msetnx.
186
     *
187
     * @param  array  $pairs
188
     *
189
     * @return bool         TRUE in case of success, FALSE in case of failure.
190
     */
191
    public function mSetNX(array $pairs): bool
192
    {
193
        if (!$this->is_associative($pairs)) {
0 ignored issues
show
Bug introduced by
It seems like is_associative() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
194
            throw new NotAssociativeArrayException('The array provided is not associative.', 1);
195
        }
196
197
        return $this->redis->mSetNX($pairs);
198
    }
199
200
    /**
201
     * Set the string value in argument as value of the key. If you're using
202
     * Redis >= 2.6.12, you can pass extended options as explained below.
203
     * See: https://redis.io/commands/set.
204
     *
205
     * @param string $key
206
     * @param mixed $value
207
     * @param mixed $args   Timeout or Options Array (optional). If you pass
208
     *                      an integer, phpredis will redirect to SETEX, and
209
     *                      will try to use Redis >= 2.6.12 extended options
210
     *                      if you  pass an array with valid values.
211
     *
212
     * @return bool TRUE if the command is successful.
213
     */
214
    public function set(string $key, $value, ...$args): bool
215
    {
216
        if (empty($args)) {
217
            return $this->redis->set($key, $value);
218
        }
219
220
        return $this->redis->set($key, $value, $args[0]);
221
    }
222
223
    /**
224
     * Set the string value in argument as value of the key, with a time to
225
     * live. PSETEX uses a TTL in milliseconds.
226
     * See: https://redis.io/commands/setex.
227
     *
228
     * @param string $key       Key
229
     * @param int    $ttl       Time To Live (seconds)
230
     * @param string $value     Value
231
     *
232
     * @return bool           TRUE if the command is successful.
233
     */
234
    public function setEx(string $key, int $ttl, string $value): bool
235
    {
236
        return $this->redis->setEx($key, $ttl, $value);
237
    }
238
239
    /**
240
     * Set the string value in argument as value of the key, with a time to
241
     * live. PSETEX uses a TTL in milliseconds.
242
     * See: https://redis.io/commands/setex.
243
     *
244
     * @param string $key       Key
245
     * @param int    $ttl       Time To Live (milliseconds)
246
     * @param string $value     Value
247
     *
248
     * @return bool           TRUE if the command is successful.
249
     */
250
    public function pSetEx(string $key, int $ttl, string $value): bool
251
    {
252
        return $this->redis->pSetEx($key, $ttl, $value);
253
    }
254
255
    /**
256
     * Set the string value in argument as value of the key if the key doesn't
257
     * already exist in the database.
258
     * See: https://redis.io/commands/setnx.
259
     *
260
     * @param string $key
261
     * @param string $value
262
     *
263
     * @return bool         TRUE in case of success, FALSE in case of failure.
264
     */
265
    public function setNx(string $key, string $value): bool
266
    {
267
        return $this->redis->setNx($key, $value);
268
    }
269
270
    /**
271
     * Changes a substring of a larger string.
272
     * See: https://redis.io/commands/setrange.
273
     *
274
     * @param string $key
275
     * @param int    $offset
276
     * @param string $value
277
     *
278
     * @return int          the length of the string after it was modified.
279
     */
280
    public function setRange(string $key, int $offset, string $value): int
281
    {
282
        return $this->redis->setRange($key, $offset, $value);
283
    }
284
285
    /**
286
     * Returns the length of the string value stored at key. An error is
287
     * returned when key holds a non-string value.
288
     * See: https://redis.io/commands/strlen.
289
     *
290
     * @param  string $key
291
     *
292
     * @return int          The length of the string at key, or 0 when key does
293
     *                      not exist.
294
     */
295
    public function strLen(string $key): int
296
    {
297
        return $this->redis->strLen($key);
298
    }
299
}
300