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

daycry / twig / 41063239604

30 Oct 2026 01:25PM UTC coverage: 57.818% (-3.4%) from 61.255%
41063239604

push

github

daycry
refactor: support standalone Twig with an included CodeIgniter 4 adapter

530 of 615 new or added lines in 19 files covered. (86.18%)

45 existing lines in 6 files now uncovered.

991 of 1714 relevant lines covered (57.82%)

12.56 hits per line

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

14.29
/src/Support/PersistenceDecoder.php
1
<?php
2

3
namespace Daycry\Twig\Support;
4

5
/**
6
 * Tolerant JSON decoder for persisted Twig state (compile index, warmup summary,
7
 * invalidations, discovery snapshots).
8
 *
9
 * Why this exists:
10
 *  - Cached payloads can be tampered with, truncated, or written by an older
11
 *    schema version — blindly trusting `json_decode` and accessing keys leads to
12
 *    fatal type errors in production.
13
 *  - Each call site previously did its own (often forgotten) `is_array` checks.
14
 */
15
final class PersistenceDecoder
16
{
17
    /**
18
     * Decode a JSON payload into an associative array. Returns `null` when the
19
     * payload is missing, empty, malformed, or not an object/array at the root.
20
     *
21
     * @return array<array-key,mixed>|null
22
     */
23
    public static function decode(?string $json): ?array
24
    {
25
        if ($json === null || $json === '') {
77 ✔
26
            return null;
73 ✔
27
        }
28
        $data = json_decode($json, true);
14 ✔
29

30
        return is_array($data) ? $data : null;
14 ✔
31
    }
32

33
    /**
34
     * Decode and validate against a minimal schema describing required key types.
35
     * Schema entries are PHP type names (`'int'|'string'|'bool'|'array'|'float'`)
36
     * or `'?<type>'` for optional. Unknown keys are passed through unchanged.
37
     * If a required key is missing or has the wrong type, `null` is returned.
38
     *
39
     * Example:
40
     *   $data = PersistenceDecoder::decodeWithSchema($raw, [
41
     *       'cumulative' => 'int',
42
     *       'last'       => '?array',
43
     *   ]);
44
     *
45
     * @param array<string,string> $schema
46
     *
47
     * @return array<array-key,mixed>|null
48
     */
49
    public static function decodeWithSchema(?string $json, array $schema): ?array
50
    {
UNCOV
51
        $data = self::decode($json);
×
UNCOV
52
        if ($data === null) {
×
53
            return null;
×
54
        }
55

UNCOV
56
        foreach ($schema as $key => $type) {
×
UNCOV
57
            $optional = false;
×
UNCOV
58
            if (str_starts_with($type, '?')) {
×
UNCOV
59
                $optional = true;
×
UNCOV
60
                $type     = substr($type, 1);
×
61
            }
UNCOV
62
            if (! array_key_exists($key, $data)) {
×
UNCOV
63
                if (! $optional) {
×
UNCOV
64
                    return null;
×
65
                }
66

UNCOV
67
                continue;
×
68
            }
UNCOV
69
            if (! self::matchesType($data[$key], $type)) {
×
UNCOV
70
                return null;
×
71
            }
72
        }
73

UNCOV
74
        return $data;
×
75
    }
76

77
    private static function matchesType(mixed $value, string $type): bool
78
    {
UNCOV
79
        return match ($type) {
×
UNCOV
80
            'int'    => is_int($value),
×
UNCOV
81
            'float'  => is_float($value) || is_int($value),
×
UNCOV
82
            'string' => is_string($value),
×
UNCOV
83
            'bool'   => is_bool($value),
×
UNCOV
84
            'array'  => is_array($value),
×
85
            'null'   => $value === null,
×
UNCOV
86
            default  => false,
×
UNCOV
87
        };
×
88
    }
89
}
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