• 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.86
/Cache/InternalArrayCache.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
 * Internal LRU cache without TTL for optimal performance.
20
 *
21
 * Lightweight PSR-16 implementation optimized for CachedDetector.
22
 * No TTL calculation, no expiry checks, pure O(1) operations.
23
 *
24
 * @final
25
 *
26
 * @psalm-api
27
 */
28
final class InternalArrayCache implements CacheInterface
29
{
30
    /**
31
     * @var array<string, mixed>
32
     */
33
    private array $storage = [];
34

35
    /**
36
     * @var int
37
     */
38
    private int $maxSize;
39

40
    /**
41
     * @param int $maxSize Maximum cache entries
42
     */
43
    public function __construct(int $maxSize = 1000)
106✔
44
    {
45
        $this->maxSize = $maxSize;
106✔
46
    }
47

48
    /**
49
     * @inheritDoc
50
     */
51
    public function get($key, $default = null)
8✔
52
    {
53
        return $this->storage[$key] ?? $default;
8✔
54
    }
55

56
    /**
57
     * @inheritDoc
58
     */
59
    public function set($key, $value, $ttl = null): bool
7✔
60
    {
61
        if (\count($this->storage) >= $this->maxSize) {
7✔
NEW
62
            \array_shift($this->storage);
×
63
        }
64
        $this->storage[$key] = $value;
7✔
65

66
        return true;
7✔
67
    }
68

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

NEW
76
        return true;
×
77
    }
78

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

86
        return true;
1✔
87
    }
88

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

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

NEW
107
            $result[$key] = $value;
×
108
        }
109

NEW
110
        return $result;
×
111
    }
112

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

NEW
131
        return true;
×
132
    }
133

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

NEW
146
        return true;
×
147
    }
148

149
    /**
150
     * @inheritDoc
151
     */
NEW
152
    public function has($key): bool
×
153
    {
NEW
154
        return isset($this->storage[$key]);
×
155
    }
156

157
    /**
158
     * Get cache size.
159
     *
160
     * @return int Current number of entries
161
     */
162
    public function getSize(): int
4✔
163
    {
164
        return \count($this->storage);
4✔
165
    }
166

167
    /**
168
     * Get max size.
169
     *
170
     * @return int Maximum number of entries
171
     */
172
    public function getMaxSize(): int
4✔
173
    {
174
        return $this->maxSize;
4✔
175
    }
176
}
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