Passed
Push — trunk ( 2f5c46...ab639d )
by Christian
13:43 queued 13s
created

RedisStub::del()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Stub\Redis;
4
5
class RedisStub extends \Redis
6
{
7
    /**
8
     * @var array<string, mixed>
9
     */
10
    private array $data = [];
11
12
    public function __construct()
13
    {
14
    }
15
16
    /**
17
     * @param mixed $context
18
     */
19
    public function connect($host, $port = 6379, $timeout = 0, $persistent_id = null, $retry_interval = 0, $read_timeout = 0, $context = null)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

19
    public function connect($host, $port = 6379, $timeout = 0, $persistent_id = null, $retry_interval = 0, $read_timeout = 0, /** @scrutinizer ignore-unused */ $context = null)

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

Loading history...
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function isConnected()
28
    {
29
        return true;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function get($key)
36
    {
37
        if (\array_key_exists($key, $this->data)) {
38
            $value = $this->data[$key];
39
40
            if ($value['expire'] !== 0 && $value['expire'] < time()) {
41
                unset($this->data[$key]);
42
43
                return false;
44
            }
45
46
            return $value['value'];
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * @param string $key
54
     * @param string $value
55
     * @param int|array{'EX'?: int, 'ex'?: int} $options
56
     */
57
    public function set($key, $value, $options = 0)
58
    {
59
        $expire = 0;
60
61
        if (\is_array($options)) {
62
            if (isset($options['ex'])) {
63
                $expire = time() + $options['ex'];
64
            }
65
66
            if (isset($options['EX'])) {
67
                $expire = time() + $options['EX'];
68
            }
69
        } elseif (\is_int($options)) {
0 ignored issues
show
introduced by
The condition is_int($options) is always true.
Loading history...
70
            $expire = time() + $options;
71
        }
72
73
        $this->data[$key] = ['value' => $value, 'expire' => $expire];
74
75
        return true;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function setex($key, $expire, $value)
82
    {
83
        return $this->set($key, $value, $expire);
84
    }
85
86
    /**
87
     * @param string $key1
88
     * @param string ...$otherKeys
89
     *
90
     * @return int
91
     */
92
    public function del($key1, ...$otherKeys)
93
    {
94
        $deletions = 0;
95
96
        $otherKeys[] = $key1;
97
98
        foreach ($otherKeys as $key) {
99
            if (\array_key_exists($key, $this->data)) {
100
                unset($this->data[$key]);
101
                ++$deletions;
102
            }
103
        }
104
105
        return $deletions;
106
    }
107
108
    public function exists($key, ...$other_keys)
109
    {
110
        if ($other_keys === []) {
111
            return \array_key_exists($key, $this->data);
112
        }
113
114
        $keys = array_merge([$key], $other_keys);
115
116
        $found = 0;
117
118
        foreach ($keys as $keyLoop) {
119
            if (\array_key_exists($keyLoop, $this->data)) {
120
                ++$found;
121
            }
122
        }
123
124
        return $found;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function ttl($key)
131
    {
132
        if (\array_key_exists($key, $this->data)) {
133
            $value = $this->data[$key];
134
135
            // If the expiry is 0, the key will never expire
136
            if ($value['expire'] === 0) {
137
                return -1;
138
            }
139
140
            return $value['expire'] - time();
141
        }
142
143
        return false;
144
    }
145
}
146