Passed
Push — trunk ( f99c1c...cd7f4f )
by Christian
13:22 queued 13s
created

RedisStub::sAdd()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 3
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
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
    public function sAdd($key, $value, ...$other_values)
128
    {
129
        $current = $this->get($key);
130
131
        if ($current === false) {
132
            $current = [];
133
        }
134
135
        if (!\is_array($current)) {
136
            throw new \RedisException('sAdd can be only called on a set');
137
        }
138
139
        $current = array_merge($current, [$value], $other_values);
140
        $current = array_unique($current);
141
142
        sort($current);
143
144
        $this->data[$key] = ['value' => $current, 'expire' => $current];
145
146
        return true;
147
    }
148
149
    /**
150
     * @param string $key
151
     *
152
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Test\Stub\Redis\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
153
     */
154
    public function sMembers($key)
155
    {
156
        /** @var list<string>|false|string $value */
157
        $value = $this->get($key);
158
159
        if ($value === false) {
0 ignored issues
show
introduced by
The condition $value === false is always false.
Loading history...
160
            return [];
161
        }
162
163
        if (!\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
164
            throw new \RedisException('sMembers can be only called on a set');
165
        }
166
167
        return $value;
168
    }
169
170
    /**
171
     * @return RedisMultiWrapper
172
     *
173
     * @phpstan-ignore-next-line
174
     */
175
    public function multi($mode = \Redis::MULTI)
176
    {
177
        return new RedisMultiWrapper($this);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function ttl($key)
184
    {
185
        if (\array_key_exists($key, $this->data)) {
186
            $value = $this->data[$key];
187
188
            // If the expiry is 0, the key will never expire
189
            if ($value['expire'] === 0) {
190
                return -1;
191
            }
192
193
            return $value['expire'] - time();
194
        }
195
196
        return false;
197
    }
198
}
199