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

codeigniter4 / settings / 17488284278

05 Sep 2025 08:41AM UTC coverage: 85.088% (-0.4%) from 85.526%
17488284278

push

github

web-flow
update workflow for PHPUnit (#151)

194 of 228 relevant lines covered (85.09%)

18.6 hits per line

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

51.02
/src/Handlers/BaseHandler.php
1
<?php
2

3
namespace CodeIgniter\Settings\Handlers;
4

5
use RuntimeException;
6

7
abstract class BaseHandler
8
{
9
    /**
10
     * Checks whether this handler has a value set.
11
     */
12
    abstract public function has(string $class, string $property, ?string $context = null): bool;
13

14
    /**
15
     * Returns a single value from the handler, if stored.
16
     *
17
     * @return mixed
18
     */
19
    abstract public function get(string $class, string $property, ?string $context = null);
20

21
    /**
22
     * If the Handler supports saving values, it
23
     * MUST override this method to provide that functionality.
24
     * Not all Handlers will support writing values.
25
     * Must throw RuntimeException for any failures.
26
     *
27
     * @param mixed $value
28
     *
29
     * @return void
30
     *
31
     * @throws RuntimeException
32
     */
33
    public function set(string $class, string $property, $value = null, ?string $context = null)
34
    {
35
        throw new RuntimeException('Set method not implemented for current Settings handler.');
×
36
    }
37

38
    /**
39
     * If the Handler supports forgetting values, it
40
     * MUST override this method to provide that functionality.
41
     * Not all Handlers will support writing values.
42
     * Must throw RuntimeException for any failures.
43
     *
44
     * @return void
45
     *
46
     * @throws RuntimeException
47
     */
48
    public function forget(string $class, string $property, ?string $context = null)
49
    {
50
        throw new RuntimeException('Forget method not implemented for current Settings handler.');
×
51
    }
52

53
    /**
54
     * All handlers MUST support flushing all values.
55
     *
56
     * @return void
57
     *
58
     * @throws RuntimeException
59
     */
60
    public function flush()
61
    {
62
        throw new RuntimeException('Flush method not implemented for current Settings handler.');
×
63
    }
64

65
    /**
66
     * Takes care of converting some item types so they can be safely
67
     * stored and re-hydrated into the config files.
68
     *
69
     * @param mixed $value
70
     *
71
     * @return mixed|string
72
     */
73
    protected function prepareValue($value)
74
    {
75
        if (is_bool($value)) {
38✔
76
            return (int) $value;
8✔
77
        }
78

79
        if (is_array($value) || is_object($value)) {
32✔
80
            return serialize($value);
4✔
81
        }
82

83
        return $value;
28✔
84
    }
85

86
    /**
87
     * Handles some special case conversions that
88
     * data might have been saved as, such as booleans
89
     * and serialized data.
90
     *
91
     * @param mixed $value
92
     *
93
     * @return bool|mixed
94
     */
95
    protected function parseValue($value, string $type)
96
    {
97
        // Serialized?
98
        if ($this->isSerialized($value)) {
32✔
99
            $value = unserialize($value);
4✔
100
        }
101

102
        settype($value, $type);
32✔
103

104
        return $value;
32✔
105
    }
106

107
    /**
108
     * Checks to see if an object is serialized and correctly formatted.
109
     *
110
     * Taken from Wordpress core functions.
111
     *
112
     * @param mixed $data
113
     * @param bool  $strict Whether to be strict about the end of the string.
114
     */
115
    protected function isSerialized($data, $strict = true): bool
116
    {
117
        // If it isn't a string, it isn't serialized.
118
        if (! is_string($data)) {
32✔
119
            return false;
12✔
120
        }
121
        $data = trim($data);
20✔
122
        if ('N;' === $data) {
20✔
123
            return true;
×
124
        }
125
        if (strlen($data) < 4) {
20✔
126
            return false;
12✔
127
        }
128
        if (':' !== $data[1]) {
8✔
129
            return false;
4✔
130
        }
131
        if ($strict) {
4✔
132
            $lastc = substr($data, -1);
4✔
133
            if (';' !== $lastc && '}' !== $lastc) {
4✔
134
                return false;
×
135
            }
136
        } else {
137
            $semicolon = strpos($data, ';');
×
138
            $brace     = strpos($data, '}');
×
139
            // Either ; or } must exist.
140
            if (false === $semicolon && false === $brace) {
×
141
                return false;
×
142
            }
143
            // But neither must be in the first X characters.
144
            if (false !== $semicolon && $semicolon < 3) {
×
145
                return false;
×
146
            }
147
            if (false !== $brace && $brace < 4) {
×
148
                return false;
×
149
            }
150
        }
151
        $token = $data[0];
4✔
152

153
        switch ($token) {
154
            case 's':
4✔
155
                if ($strict) {
×
156
                    if ('"' !== substr($data, -2, 1)) {
×
157
                        return false;
×
158
                    }
159
                } elseif (! str_contains($data, '"')) {
×
160
                    return false;
×
161
                }
162

163
                // Or else fall through.
164
                // no break
165
            case 'a':
4✔
166
            case 'O':
2✔
167
                return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
4✔
168

169
            case 'b':
×
170
            case 'i':
×
171
            case 'd':
×
172
                $end = $strict ? '$' : '';
×
173

174
                return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data);
×
175
        }
176

177
        return false;
×
178
    }
179
}
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

© 2025 Coveralls, Inc