• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ducks-project / encoding-repair / 21391437519

27 Jan 2026 09:18AM UTC coverage: 92.427% (-6.5%) from 98.975%
21391437519

push

github

donaldinou
feat : add Optimized PSR-16 implementation

64 of 108 new or added lines in 4 files covered. (59.26%)

537 of 581 relevant lines covered (92.43%)

22.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.5
/Cache/ArrayCache.php
1
<?php
2

3
/**
4
 * Part of EncodingRepair package.
5
 *
6
 * (c) Adrien Loyant <donald_duck@team-df.org>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Ducks\Component\EncodingRepair\Cache;
15

16
use Psr\SimpleCache\CacheInterface;
17

18
/**
19
 * Simple array-based PSR-16 cache implementation.
20
 *
21
 * @final
22
 *
23
 * @psalm-api
24
 */
25
final class ArrayCache implements CacheInterface
26
{
27
    /**
28
     * @var array<string, array{value: mixed, expiry: int|null}>
29
     */
30
    private array $storage = [];
31

32
    /**
33
     * @inheritDoc
34
     *
35
     * @param string $key
36
     * @param mixed $default
37
     *
38
     * @return mixed
39
     */
40
    public function get($key, $default = null)
3✔
41
    {
42
        if (!isset($this->storage[$key])) {
3✔
43
            return $default;
3✔
44
        }
45

46
        $item = $this->storage[$key];
2✔
47

48
        if (null !== $item['expiry'] && $item['expiry'] < \time()) {
2✔
NEW
49
            unset($this->storage[$key]);
×
50

NEW
51
            return $default;
×
52
        }
53

54
        return $item['value'];
2✔
55
    }
56

57
    /**
58
     * @inheritDoc
59
     *
60
     * @param string $key
61
     * @param mixed $value
62
     * @param null|int|\DateInterval $ttl
63
     */
64
    public function set($key, $value, $ttl = null): bool
3✔
65
    {
66
        $expiry = $this->calculateExpiry($ttl);
3✔
67
        $this->storage[$key] = ['value' => $value, 'expiry' => $expiry];
3✔
68

69
        return true;
3✔
70
    }
71

72
    /**
73
     * @inheritDoc
74
     */
NEW
75
    public function delete($key): bool
×
76
    {
NEW
77
        unset($this->storage[$key]);
×
78

NEW
79
        return true;
×
80
    }
81

82
    /**
83
     * @inheritDoc
84
     */
85
    public function clear(): bool
1✔
86
    {
87
        $this->storage = [];
1✔
88

89
        return true;
1✔
90
    }
91

92
    /**
93
     * @inheritDoc
94
     *
95
     * @param iterable<mixed,mixed> $keys A list of keys that can obtained in a single operation.
96
     * @param mixed $default Default value to return for keys that do not exist.
97
     *
98
     * @return array<array-key, mixed> A list of key => value pairs.
99
     * Cache keys that do not exist or are stale will have $default as value.
100
     */
NEW
101
    public function getMultiple($keys, $default = null)
×
102
    {
103
        /** @var array<string, mixed> $result */
NEW
104
        $result = [];
×
105

106
        /** @var string $key */
NEW
107
        foreach ($keys as $key) {
×
108
            /** @var object|resource|iterable<mixed,mixed>|string|float|int|bool|null $value */
NEW
109
            $value = $this->get($key, $default);
×
110

NEW
111
            $result[$key] = $value;
×
112
        }
113

NEW
114
        return $result;
×
115
    }
116

117
    /**
118
     * @inheritDoc
119
     *
120
     * @param iterable<mixed,mixed> $values A list of key => value pairs for a multiple-set operation.
121
     * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
122
     *                                    the driver supports TTL then the library may set a default value
123
     *                                    for it or let the driver take care of that.
124
     */
NEW
125
    public function setMultiple($values, $ttl = null): bool
×
126
    {
127
        /**
128
         * @var mixed $key
129
         * @var mixed $value
130
         */
NEW
131
        foreach ($values as $key => $value) {
×
NEW
132
            $this->set((string) $key, $value, $ttl);
×
133
        }
134

NEW
135
        return true;
×
136
    }
137

138
    /**
139
     * @inheritDoc
140
     *
141
     * @param iterable<mixed,mixed> $keys A list of string-based keys to be deleted.
142
     */
NEW
143
    public function deleteMultiple($keys): bool
×
144
    {
145
        /** @var string $key */
NEW
146
        foreach ($keys as $key) {
×
NEW
147
            $this->delete($key);
×
148
        }
149

NEW
150
        return true;
×
151
    }
152

153
    /**
154
     * @inheritDoc
155
     */
NEW
156
    public function has($key): bool
×
157
    {
NEW
158
        return null !== $this->get($key);
×
159
    }
160

161
    /**
162
     * Calculate expiry timestamp from TTL.
163
     *
164
     * @param null|int|\DateInterval $ttl TTL value
165
     *
166
     * @return int|null Expiry timestamp or null for no expiry
167
     */
168
    private function calculateExpiry($ttl): ?int
3✔
169
    {
170
        if (null === $ttl) {
3✔
NEW
171
            return null;
×
172
        }
173

174
        if ($ttl instanceof \DateInterval) {
3✔
NEW
175
            return (new \DateTime())->add($ttl)->getTimestamp();
×
176
        }
177

178
        return \time() + $ttl;
3✔
179
    }
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc