Issues (37)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Traits/Keys.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\NotAssociativeArrayException;
6
7
trait Keys
8
{
9
    /**
10
     * Remove specified keys [Blocking].
11
     *
12
     * @param  mixed $keys
13
     *
14
     * @return int Number of keys deleted
15
     */
16
    public function del(...$keys): int
17
    {
18
        return $this->redis->del(...$keys);
0 ignored issues
show
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...
19
    }
20
21
    /**
22
     * Remove specified keys [Non Blocking].
23
     *
24
     * @param  mixed $keys
25
     *
26
     * @return int Number of keys deleted
27
     */
28
    public function delete(...$keys): int
29
    {
30
        return $this->redis->unlink(...$keys);
31
    }
32
33
    /**
34
     * Remove specified keys [NonBlocking].
35
     *
36
     * Note: If you are connecting to Redis server >= 4.0.0 you can remove a
37
     * key with the unlink method in the exact same way you would use del.
38
     * The Redis unlink command is non-blocking and will perform the actual
39
     *  deletion asynchronously.
40
     *
41
     * @param  mixed $keys
42
     *
43
     * @return int Number of keys deleted
44
     */
45
    public function unlink(...$keys): int
46
    {
47
        return $this->redis->unlink(...$keys);
48
    }
49
50
    /**
51
     * Return a serialized version of the value stored at the specified key.
52
     *
53
     * @param  string $key
54
     *
55
     * @return mixed|string|false       The Redis encoded value of the key,
56
     *                                  or FALSE if the key doesn't exist
57
     */
58
    public function dump(string $key)
59
    {
60
        return $this->redis->dump($key);
61
    }
62
63
    /**
64
     * Verify if the specified key exists.
65
     *
66
     * @param  mixed] $keys
0 ignored issues
show
The doc-type mixed] could not be parsed: Expected "|" or "end of type", but got "]" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
67
     *
68
     * @return int
69
     */
70
    public function exists(...$keys): int
71
    {
72
        return $this->redis->exists(...$keys);
73
    }
74
75
    /**
76
     * Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds.
77
     *
78
     * @param  string $key. The key that will disappear.
0 ignored issues
show
There is no parameter named $key.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
     * @param  int    $ttl. The key's remaining Time To Live, in seconds.
0 ignored issues
show
There is no parameter named $ttl.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
80
     *
81
     * @return bool         true in case of success, false in case of failure.
82
     */
83
    public function expire(string $key, int $ttl): bool
84
    {
85
        return $this->redis->expire($key, $ttl);
86
    }
87
88
    /**
89
     * Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds.
90
     *
91
     * @param  string $key. The key that will disappear.
0 ignored issues
show
There is no parameter named $key.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
     * @param  int    $ttl. The key's remaining Time To Live, in seconds.
0 ignored issues
show
There is no parameter named $ttl.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
93
     *
94
     * @return bool         true in case of success, false in case of failure.
95
     */
96
    public function setTimeout(string $key, int $ttl): bool
97
    {
98
        return $this->redis->expire($key, $ttl);
99
    }
100
101
    /**
102
     * Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds.
103
     *
104
     * @param  string $key. The key that will disappear.
0 ignored issues
show
There is no parameter named $key.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105
     * @param  int    $ttl. The key's remaining Time To Live, in seconds.
0 ignored issues
show
There is no parameter named $ttl.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
106
     *
107
     * @return bool         true in case of success, false in case of failure.
108
     */
109
    public function pexpire(string $key, int $ttl): bool
110
    {
111
        return $this->redis->pexpire($key, $ttl);
112
    }
113
114
    /**
115
     * Sets an expiration date (a timestamp) on an item.
116
     *
117
     * @param  string $key  The key that will disappear.
118
     * @param  int    $ttl  Unix timestamp. The key's date of death, in seconds from Epoch time.
119
     *
120
     * @return bool         true in case of success, false in case of failure.
121
     */
122
    public function expireAt(string $key, int $ttl): bool
123
    {
124
        return $this->redis->expireAt($key, $ttl);
125
    }
126
127
    /**
128
     * Sets an expiration date (a timestamp) on an item in milliseconds.
129
     *
130
     * @param  string $key  The key that will disappear.
131
     * @param  int    $ttl  Unix timestamp. The key's date of death, in
132
     *                      milliseconds from Epoch time with.
133
     *
134
     * @return bool         true in case of success, false in case of failure.
135
     */
136
    public function pexpireAt(string $key, int $ttl): bool
137
    {
138
        return $this->redis->pexpireAt($key, $ttl);
139
    }
140
141
    /**
142
     * Returns the keys that match a certain pattern.
143
     *
144
     * @param  string $pattern  Pattern to match, using '*' as a wildcard.
145
     *
146
     * @return array            The keys that match a certain pattern.
147
     */
148
    public function keys(string $pattern): array
149
    {
150
        return $this->redis->keys($pattern);
151
    }
152
153
    /**
154
     * Returns the keys that match a certain pattern.
155
     *
156
     * @param  string $pattern  Pattern to match, using '*' as a wildcard.
157
     *
158
     * @return array            The keys that match a certain pattern.
159
     */
160
    public function getKeys(string $pattern): array
161
    {
162
        return $this->redis->keys($pattern);
163
    }
164
165
    /**
166
     * Scan the keyspace for keys.
167
     *
168
     * @param  int      $iterator
169
     * @param  string   $pattern
170
     * @param  int|int  $count
171
     *
172
     * @return mixed|array|bool     This function will return an array of keys
173
     *                              or FALSE if Redis returned zero keys.
174
     */
175
    public function scan(?int &$iterator = null, string $pattern = '*', int $count = 10)
176
    {
177
        return $this->redis->scan($iterator, $pattern, $count);
178
    }
179
180
    /**
181
     * Migrates a key to a different Redis instance.
182
     * Note:: Redis introduced migrating multiple keys in 3.0.6, so you must
183
     * have at least that version in order to call migrate with an array of
184
     * keys.
185
     * See: https://redis.io/commands/migrate.
186
     *
187
     * @param  string $host         The destination host
188
     * @param  int    $port         The TCP port to connect to.
189
     * @param  array  $keys
190
     * @param  int    $db           The target DB.
191
     * @param  int    $timeout      The maximum amount of time given to this transfer.
192
     * @param  bool   $copy         (optional) Should we send the COPY flag to redis.
193
     * @param  bool   $replace      (optional) Should we send the REPLACE flag to redis.
194
     *
195
     * @return bool                 Simple string reply: The command returns OK
196
     *                              on success, or NOKEY if no keys were found
197
     *                              in the source instance.
198
     */
199
    public function migrate(
200
        string $host,
201
        int $port,
202
        array $keys,
203
        int $db,
204
        int $timeout,
205
        ?bool $copy = false,
0 ignored issues
show
The parameter $copy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
        ?bool $replace = false
0 ignored issues
show
The parameter $replace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
    ): bool {
208
        return $this->redis->migrate($host, $port, $keys, $db, $timeout);
209
    }
210
211
    /**
212
     * Moves a key to a different database.
213
     * See: https://redis.io/commands/move.
214
     *
215
     * @param  string $key  key, the key to move.
216
     * @param  int    $db   dbindex, the database number to move the key to.
217
     *
218
     * @return bool         TRUE in case of success, FALSE in case of failure.
219
     */
220
    public function move(string $key, int $db): bool
221
    {
222
        return $this->redis->move($key, $db);
223
    }
224
225
    /**
226
     * Describes the object pointed to by a key.
227
     * See: https://redis.io/commands/object.
228
     *
229
     * @param  string $subcommand       The information to retrieve.
230
     * @param  string $key              The key to fetch that data from.
231
     *
232
     * @return mixed|string|int|bool    STRING for "encoding", LONG for
233
     *                                  "refcount" and "idletime", FALSE if the
234
     *                                  key doesn't exist.
235
     */
236
    public function object(string $subcommand, string $key)
237
    {
238
        return $this->redis->object($subcommand, $key);
239
    }
240
241
    /**
242
     * Remove the expiration timer from a key.
243
     * See: https://redis.io/commands/persist.
244
     *
245
     * @param  string $key
246
     *
247
     * @return bool         TRUE if a timeout was removed, FALSE if the key
248
     *                      didn’t exist or didn’t have an expiration timer.
249
     */
250
    public function persist(string $key): bool
251
    {
252
        return $this->redis->persist($key);
253
    }
254
255
    /**
256
     * Returns a random key.
257
     * See: https://redis.io/commands/randomkey.
258
     *
259
     * @return string   An existing key in redis.
260
     */
261
    public function randomKey(): string
262
    {
263
        return $this->redis->randomKey();
264
    }
265
266
    /**
267
     * Renames key to newkey. It returns an error when key does not exist.
268
     * If newkey already exists it is overwritten.
269
     * See: https://redis.io/commands/rename.
270
     *
271
     * @param  string $key      Source key, the key to rename.
272
     * @param  string $newKey   Destination key, the new name for the key.
273
     *
274
     * @return bool             TRUE in case of success, FALSE in case of failure.
275
     */
276
    public function rename(string $key, string $newKey): bool
277
    {
278
        return $this->redis->rename($key, $newKey);
279
    }
280
281
    /**
282
     * Renames key to newkey. It returns an error when key does not exist.
283
     * If newkey already exists it is overwritten.
284
     * Note: renameKey is an alias for rename and will be removed in future versions of phpredis.
285
     * See: https://redis.io/commands/rename.
286
     *
287
     * @param  string $key      Source key, the key to rename.
288
     * @param  string $newKey   Destination key, the new name for the key.
289
     *
290
     * @return bool             TRUE in case of success, FALSE in case of failure.
291
     */
292
    public function renameKey(string $key, string $newKey): bool
293
    {
294
        return $this->redis->rename($key, $newKey);
295
    }
296
297
    /**
298
     * Renames key to newkey,. but will not replace a key if the destination
299
     * already exists. This is the same behaviour as setNx.
300
     * See: https://redis.io/commands/renamenx.
301
     *
302
     * @param  string $key      Source key, the key to rename.
303
     * @param  string $newKey   Destination key, the new name for the key.
304
     *
305
     * @return bool             TRUE in case of success, FALSE in case of failure.
306
     */
307
    public function renameNx(string $key, string $newKey): bool
308
    {
309
        return $this->redis->renameNx($key, $newKey);
310
    }
311
312
    /**
313
     * Returns the type of data pointed by a given key.
314
     * See: https://redis.io/commands/type.
315
     *
316
     * @param  string $key
317
     *
318
     * @return mixed        Depending on the type of the data pointed by the key,
319
     *                      this method will return the following value:
320
     *                      string: Redis::REDIS_STRING
321
     *                      set: Redis::REDIS_SET
322
     *                      list: Redis::REDIS_LIST
323
     *                      zset: Redis::REDIS_ZSET
324
     *                      hash: Redis::REDIS_HASH
325
     *                      other: Redis::REDIS_NOT_FOUND
326
     */
327
    public function type(string $key)
328
    {
329
        return $this->redis->type($key);
330
    }
331
332
    /**
333
     * Sort the elements in a list, set or sorted set.
334
     * ONLY WORKS WITH NUMERIC VALUES
335
     * See: https://redis.io/commands/sort.
336
     *
337
     * @param  string $key
338
     * @param  array $options   [key => value, ...] - optional, with the
339
     *                          following keys and values:
340
     *                          'by' => 'some_pattern_*',
341
     *                          'limit' => [0, 1],
342
     *                          'get' => 'some_other_pattern_*' or an array of patterns,
343
     *                          'sort' => 'asc' or 'desc',
344
     *                          'alpha' => TRUE,
345
     *                          'store' => 'external-key'
346
     *
347
     * @return mixed|array|int  An array of values, or a number corresponding
348
     *                          to the number of elements stored if that was used.
349
     */
350
    public function sort(string $key, array $options = [])
351
    {
352
        if (empty($options)) {
353
            return $this->redis->sort($key);
354
        }
355
356
        if (!$this->is_associative($options)) {
0 ignored issues
show
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...
357
            throw new NotAssociativeArrayException('The array provided is not associative.', 1);
358
        }
359
360
        return $this->redis->sort($key, $options);
361
    }
362
363
    /**
364
     * Returns the time to live left for a given key in seconds (ttl).
365
     * See: https://redis.io/commands/ttl.
366
     *
367
     * @param  string $key
368
     *
369
     * @return int          The time to live in seconds. If the key has no ttl,
370
     *                      -1 will be returned, and -2 if the key doesn't exist.
371
     */
372
    public function ttl(string $key): int
373
    {
374
        return $this->redis->ttl($key);
375
    }
376
377
    /**
378
     * Returns the time to live left for a given key in milliseconds (pttl).
379
     * See: https://redis.io/commands/pttl.
380
     *
381
     * @param  string $key
382
     *
383
     * @return int          The time to live in seconds. If the key has no ttl,
384
     *                      -1 will be returned, and -2 if the key doesn't exist.
385
     */
386
    public function pttl(string $key): int
387
    {
388
        return $this->redis->pttl($key);
389
    }
390
391
    /**
392
     * Restore a key from the result of a DUMP operation.
393
     * See: https://redis.io/commands/restore.
394
     *
395
     * @param  string $key   The key name
396
     * @param  int    $ttl   How long the key should live (if zero, no expire will be set on the key)
397
     * @param  string $value String (binary). The Redis encoded key value (from DUMP)
398
     *
399
     * @return mixed
400
     */
401
    public function restore(string $key, int $ttl = 0, string $value = '')
402
    {
403
        return $this->redis->restore($key, $ttl, $value);
404
    }
405
}
406