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

h4kuna / critical-cache / 36120366469

25 Sep 2026 09:46AM UTC coverage: 95.195% (+0.2%) from 95.0%
36120366469

push

github

h4kuna
chore(ci): enforce static analysis and test on PHP 8.5

416 of 437 relevant lines covered (95.19%)

0.95 hits per line

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

94.74
/src/Caching/ValidityAware/TimeRangeEncoder.php
1
<?php declare(strict_types = 1);
2

3
namespace h4kuna\CriticalCache\Caching\ValidityAware;
4

5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use h4kuna\CriticalCache\Exceptions\LogicException;
8
use Psr\Clock\ClockInterface;
9

10
/**
11
 * f = from
12
 * t = to
13
 * v = value
14
 *
15
 * @phpstan-type TypeSave array{f: string, t: string, v: string}
16
 */
17
final readonly class TimeRangeEncoder
18
{
19

20
        public function __construct(
1 ✔
21
                private ClockInterface $clock,
22
        )
23
        {
24
        }
1 ✔
25

26
        /**
27
         * @param TypeSave|null $data
28
         */
29
        public function decode(?array $data): TimeRangeItem
1 ✔
30
        {
31
                if ($data === null) {
1 ✔
32
                        return new TimeRangeItem(from: null, to: null, value: null, clock: $this->clock);
1 ✔
33
                }
34

35
                return new TimeRangeItem(self::toDate($data['f']), self::toDate($data['t']), $data['v'], $this->clock);
1 ✔
36
        }
37

38
        /**
39
         * @return TypeSave
40
         */
41
        public function encode(
1 ✔
42
                ?DateTimeInterface $from,
43
                DateTimeInterface $to,
44
                string $value,
45
        ): array
46
        {
47
                return [
48
                        't' => self::toString($to),
1 ✔
49
                        'v' => $value,
1 ✔
50
                        'f' => $from === null ? '' : self::toString($from),
1 ✔
51
                ];
52
        }
53

54
        private static function toString(DateTimeInterface $dateTime): string
1 ✔
55
        {
56
                return $dateTime->format(DateTimeInterface::RFC3339_EXTENDED);
1 ✔
57
        }
58

59
        private static function toDate(string $dateTime): ?DateTimeImmutable
1 ✔
60
        {
61
                if ($dateTime === '') {
1 ✔
62
                        return null;
1 ✔
63
                }
64
                $date = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $dateTime);
1 ✔
65

66
                if ($date === false) {
1 ✔
67
                        throw new LogicException('Broken date format in cache.');
×
68
                }
69

70
                return $date;
1 ✔
71
        }
72

73
}
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