Passed
Push — trunk ( 711512...d883c7 )
by Christian
31:59 queued 18:57
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 Method

Rating   Name   Duplication   Size   Complexity  
A RedisStub::doTtl() 0 14 3
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Stub\Redis;
4
5
class RedisStub extends \Redis
6
{
7
    use RedisCompatibility;
8
9
    /**
10
     * @var array<string, mixed>
11
     */
12
    private array $data = [];
13
14
    public function __construct()
15
    {
16
    }
17
18
    private function doGet(string $key): mixed
0 ignored issues
show
Unused Code introduced by
The method doGet() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
19
    {
20
        if (\array_key_exists($key, $this->data)) {
21
            $value = $this->data[$key];
22
23
            if ($value['expire'] !== 0 && $value['expire'] < time()) {
24
                unset($this->data[$key]);
25
26
                return false;
27
            }
28
29
            return $value['value'];
30
        }
31
32
        return false;
33
    }
34
35
    private function doSet(string $key, mixed $value, mixed $options = null): bool
0 ignored issues
show
Unused Code introduced by
The method doSet() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
36
    {
37
        $expire = 0;
38
39
        if (\is_array($options)) {
40
            if (isset($options['ex'])) {
41
                $expire = time() + $options['ex'];
42
            }
43
44
            if (isset($options['EX'])) {
45
                $expire = time() + $options['EX'];
46
            }
47
        } elseif (\is_int($options)) {
48
            $expire = time() + $options;
49
        }
50
51
        $this->data[$key] = ['value' => $value, 'expire' => $expire];
52
53
        return true;
54
    }
55
56
    private function doDel(string $key, string ...$other_keys): int
0 ignored issues
show
Unused Code introduced by
The method doDel() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
57
    {
58
        $deletions = 0;
59
60
        $other_keys[] = $key;
61
62
        foreach ($other_keys as $otherKey) {
63
            if (\array_key_exists($otherKey, $this->data)) {
64
                unset($this->data[$otherKey]);
65
                ++$deletions;
66
            }
67
        }
68
69
        return $deletions;
70
    }
71
72
    private function doExists(mixed $key, mixed ...$other_keys): int|bool
0 ignored issues
show
Unused Code introduced by
The method doExists() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
73
    {
74
        if ($other_keys === []) {
75
            return \array_key_exists($key, $this->data);
76
        }
77
78
        $keys = array_merge([$key], $other_keys);
79
80
        $found = 0;
81
82
        foreach ($keys as $keyLoop) {
83
            if (\array_key_exists($keyLoop, $this->data)) {
84
                ++$found;
85
            }
86
        }
87
88
        return $found;
89
    }
90
91
    private function doSAdd(string $key, mixed $value, mixed ...$other_values): int
0 ignored issues
show
Unused Code introduced by
The method doSAdd() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
92
    {
93
        $current = $this->get($key);
94
95
        if ($current === false) {
96
            $current = [];
97
        }
98
99
        if (!\is_array($current)) {
100
            throw new \RedisException('sAdd can be only called on a set');
101
        }
102
103
        $current = array_merge($current, [$value], $other_values);
104
        $current = array_unique($current);
105
106
        sort($current);
107
108
        $this->data[$key] = ['value' => $current, 'expire' => $current];
109
110
        return 1;
111
    }
112
113
    /**
114
     * @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...
115
     */
116
    private function doSMembers(string $key): array
0 ignored issues
show
Unused Code introduced by
The method doSMembers() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
117
    {
118
        /** @var list<string>|false|string $value */
119
        $value = $this->get($key);
120
121
        if ($value === false) {
0 ignored issues
show
introduced by
The condition $value === false is always false.
Loading history...
122
            return [];
123
        }
124
125
        if (!\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
126
            throw new \RedisException('sMembers can be only called on a set');
127
        }
128
129
        return $value;
130
    }
131
132
    private function doTtl(string $key): int|false
0 ignored issues
show
Unused Code introduced by
The method doTtl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
133
    {
134
        if (\array_key_exists($key, $this->data)) {
135
            $value = $this->data[$key];
136
137
            // If the expiry is 0, the key will never expire
138
            if ($value['expire'] === 0) {
139
                return -1;
140
            }
141
142
            return $value['expire'] - time();
143
        }
144
145
        return false;
146
    }
147
}
148