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

ducks-project / encoding-repair / 21291993490

23 Jan 2026 03:45PM UTC coverage: 97.336% (-2.7%) from 100.0%
21291993490

push

github

donaldinou
fix : tests

1 of 1 new or added line in 1 file covered. (100.0%)

11 existing lines in 2 files now uncovered.

475 of 488 relevant lines covered (97.34%)

29.3 hits per line

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

95.83
/Detector/CachedDetector.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\Detector;
15

16
/**
17
 * Cached detector decorator with LRU-like eviction.
18
 *
19
 * @final
20
 *
21
 * @psalm-api
22
 */
23
final class CachedDetector implements DetectorInterface
24
{
25
    private const MAX_SIZE = 1000;
26

27
    /**
28
     * @var DetectorInterface
29
     */
30
    private DetectorInterface $detector;
31

32
    /**
33
     * @var array<string, string>
34
     */
35
    private array $cache = [];
36

37
    /**
38
     * @param DetectorInterface $detector Wrapped detector
39
     */
40
    public function __construct(DetectorInterface $detector)
122✔
41
    {
42
        $this->detector = $detector;
122✔
43
    }
44

45
    /**
46
     * @inheritDoc
47
     */
48
    public function detect(string $string, ?array $options = null): ?string
10✔
49
    {
50
        /**
51
         * xxh64 does not exits.
52
         *
53
         * @phan-var string|false $key
54
         */
55
        $key = \hash('sha256', $string);
10✔
56

57
        if (false !== $key && isset($this->cache[$key])) {
10✔
58
            return $this->cache[$key];
3✔
59
        }
60

61
        $result = $this->detector->detect($string, $options);
10✔
62

63
        if (null !== $result) {
10✔
64
            if (\count($this->cache) >= self::MAX_SIZE) {
9✔
65
                // LRU eviction: remove oldest entry
UNCOV
66
                \array_shift($this->cache);
×
67
            }
68
            if (false !== $key) {
9✔
69
                $this->cache[$key] = $result;
9✔
70
            }
71
        }
72

73
        return $result;
10✔
74
    }
75

76
    /**
77
     * @inheritDoc
78
     */
79
    public function getPriority(): int
112✔
80
    {
81
        return 200;
112✔
82
    }
83

84
    /**
85
     * @inheritDoc
86
     */
87
    public function isAvailable(): bool
3✔
88
    {
89
        return $this->detector->isAvailable();
3✔
90
    }
91

92
    /**
93
     * Clear cache entries.
94
     *
95
     * @return void
96
     *
97
     * @psalm-api
98
     */
99
    public function clearCache(): void
2✔
100
    {
101
        $this->cache = [];
2✔
102
    }
103

104
    /**
105
     * Get cache statistics.
106
     *
107
     * @return array{size: int, maxSize: int}
108
     *
109
     * @psalm-api
110
     */
111
    public function getCacheStats(): array
6✔
112
    {
113
        return [
6✔
114
            'size' => \count($this->cache),
6✔
115
            'maxSize' => self::MAX_SIZE,
6✔
116
        ];
6✔
117
    }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc