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

codeigniter4 / settings / 6357304054

29 Sep 2023 09:24PM UTC coverage: 87.097%. Remained the same
6357304054

push

github

web-flow
Merge pull request #95 from kenjis/reuse-ga-workflows

chore: reuse github workflows

189 of 217 relevant lines covered (87.1%)

18.41 hits per line

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

54.17
/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
     * Takes care of converting some item types so they can be safely
55
     * stored and re-hydrated into the config files.
56
     *
57
     * @param mixed $value
58
     *
59
     * @return mixed|string
60
     */
61
    protected function prepareValue($value)
62
    {
63
        if (is_bool($value)) {
36✔
64
            return (int) $value;
8✔
65
        }
66

67
        if (is_array($value) || is_object($value)) {
30✔
68
            return serialize($value);
4✔
69
        }
70

71
        return $value;
26✔
72
    }
73

74
    /**
75
     * Handles some special case conversions that
76
     * data might have been saved as, such as booleans
77
     * and serialized data.
78
     *
79
     * @param mixed $value
80
     *
81
     * @return bool|mixed
82
     */
83
    protected function parseValue($value, string $type)
84
    {
85
        // Serialized?
86
        if ($this->isSerialized($value)) {
30✔
87
            $value = unserialize($value);
4✔
88
        }
89

90
        settype($value, $type);
30✔
91

92
        return $value;
30✔
93
    }
94

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

141
        switch ($token) {
142
            case 's':
4✔
143
                if ($strict) {
×
144
                    if ('"' !== substr($data, -2, 1)) {
×
145
                        return false;
×
146
                    }
147
                } elseif (false === strpos($data, '"')) {
×
148
                    return false;
×
149
                }
150

151
                // Or else fall through.
152
                // no break
153
            case 'a':
4✔
154
            case 'O':
2✔
155
                return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
4✔
156

157
            case 'b':
×
158
            case 'i':
×
159
            case 'd':
×
160
                $end = $strict ? '$' : '';
×
161

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

165
        return false;
×
166
    }
167
}
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