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

codeigniter4 / CodeIgniter4 / 20653314318

02 Jan 2026 07:36AM UTC coverage: 85.527% (+0.03%) from 85.499%
20653314318

Pull #9858

github

web-flow
Merge 539cb56f0 into ff20d8106
Pull Request #9858: feat: complete `Superglobals` implementation

119 of 121 new or added lines in 18 files covered. (98.35%)

1 existing line in 1 file now uncovered.

21865 of 25565 relevant lines covered (85.53%)

204.71 hits per line

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

98.48
/system/Superglobals.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter;
15

16
use CodeIgniter\Exceptions\InvalidArgumentException;
17

18
/**
19
 * Superglobals manipulation.
20
 *
21
 * Provides a clean API for accessing and manipulating PHP superglobals
22
 * with support for testing and backward compatibility.
23
 *
24
 * Note on return types:
25
 * - $_SERVER can contain int (argc, REQUEST_TIME) or float (REQUEST_TIME_FLOAT)
26
 * - $_SERVER['argv'] is an array
27
 * - $_GET, $_POST, $_REQUEST can contain nested arrays from query params like ?foo[bar]=value
28
 * - $_COOKIE typically contains strings but can have arrays with cookie[key] notation
29
 *
30
 * @phpstan-type server_items  array<array-key, mixed>|float|int|string
31
 * @phpstan-type get_items     array<array-key, mixed>|string
32
 * @phpstan-type post_items    array<array-key, mixed>|string
33
 * @phpstan-type cookie_items  array<array-key, mixed>|string
34
 * @phpstan-type files_items   array<array-key, mixed>
35
 * @phpstan-type request_items array<array-key, mixed>|string
36
 *
37
 * @internal
38
 * @see \CodeIgniter\SuperglobalsTest
39
 */
40
final class Superglobals
41
{
42
    /**
43
     * @param array<string, server_items>|null  $server
44
     * @param array<string, get_items>|null     $get
45
     * @param array<string, post_items>|null    $post
46
     * @param array<string, cookie_items>|null  $cookie
47
     * @param array<string, files_items>|null   $files
48
     * @param array<string, request_items>|null $request
49
     */
50
    public function __construct(
51
        private ?array $server = null,
52
        private ?array $get = null,
53
        private ?array $post = null,
54
        private ?array $cookie = null,
55
        private ?array $files = null,
56
        private ?array $request = null,
57
    ) {
58
        $this->server ??= $_SERVER;
2,325✔
59
        $this->get ??= $_GET;
2,325✔
60
        $this->post ??= $_POST;
2,325✔
61
        $this->cookie ??= $_COOKIE;
2,325✔
62
        $this->files ??= $_FILES;
2,325✔
63
        $this->request ??= $_REQUEST;
2,325✔
64
    }
65

66
    /**
67
     * Get a value from $_SERVER.
68
     *
69
     * @param server_items|null $default
70
     *
71
     * @return server_items|null
72
     */
73
    public function server(string $key, mixed $default = null): array|float|int|string|null
74
    {
75
        return $this->server[$key] ?? $default;
1,722✔
76
    }
77

78
    /**
79
     * Set a value in $_SERVER.
80
     *
81
     * @param server_items $value
82
     */
83
    public function setServer(string $key, array|float|int|string $value): void
84
    {
85
        $this->server[$key] = $value;
904✔
86
        $_SERVER[$key]      = $value;
904✔
87
    }
88

89
    /**
90
     * Remove a key from $_SERVER.
91
     */
92
    public function unsetServer(string $key): void
93
    {
94
        unset($this->server[$key], $_SERVER[$key]);
24✔
95
    }
96

97
    /**
98
     * Get all $_SERVER values.
99
     *
100
     * @return array<string, server_items>
101
     */
102
    public function getServerArray(): array
103
    {
104
        return $this->server;
1,614✔
105
    }
106

107
    /**
108
     * Set the entire $_SERVER array.
109
     *
110
     * @param array<string, server_items> $array
111
     */
112
    public function setServerArray(array $array): void
113
    {
114
        $this->server = $array;
209✔
115
        $_SERVER      = $array;
209✔
116
    }
117

118
    /**
119
     * Get a value from $_GET.
120
     *
121
     * @param get_items|null $default
122
     *
123
     * @return get_items|null
124
     */
125
    public function get(string $key, mixed $default = null): array|string|null
126
    {
127
        return $this->get[$key] ?? $default;
52✔
128
    }
129

130
    /**
131
     * Set a value in $_GET.
132
     *
133
     * @param get_items $value
134
     */
135
    public function setGet(string $key, array|string $value): void
136
    {
137
        $this->get[$key] = $value;
42✔
138
        $_GET[$key]      = $value;
42✔
139
    }
140

141
    /**
142
     * Remove a key from $_GET.
143
     */
144
    public function unsetGet(string $key): void
145
    {
146
        unset($this->get[$key], $_GET[$key]);
1✔
147
    }
148

149
    /**
150
     * Get all $_GET values.
151
     *
152
     * @return array<string, get_items>
153
     */
154
    public function getGetArray(): array
155
    {
156
        return $this->get;
324✔
157
    }
158

159
    /**
160
     * Set the entire $_GET array.
161
     *
162
     * @param array<string, get_items> $array
163
     */
164
    public function setGetArray(array $array): void
165
    {
166
        $this->get = $array;
654✔
167
        $_GET      = $array;
654✔
168
    }
169

170
    /**
171
     * Get a value from $_POST.
172
     *
173
     * @param post_items|null $default
174
     *
175
     * @return post_items|null
176
     */
177
    public function post(string $key, mixed $default = null): array|string|null
178
    {
179
        return $this->post[$key] ?? $default;
35✔
180
    }
181

182
    /**
183
     * Set a value in $_POST.
184
     *
185
     * @param post_items $value
186
     */
187
    public function setPost(string $key, array|string $value): void
188
    {
189
        $this->post[$key] = $value;
57✔
190
        $_POST[$key]      = $value;
57✔
191
    }
192

193
    /**
194
     * Remove a key from $_POST.
195
     */
196
    public function unsetPost(string $key): void
197
    {
198
        unset($this->post[$key], $_POST[$key]);
15✔
199
    }
200

201
    /**
202
     * Get all $_POST values.
203
     *
204
     * @return array<string, post_items>
205
     */
206
    public function getPostArray(): array
207
    {
208
        return $this->post;
277✔
209
    }
210

211
    /**
212
     * Set the entire $_POST array.
213
     *
214
     * @param array<string, post_items> $array
215
     */
216
    public function setPostArray(array $array): void
217
    {
218
        $this->post = $array;
72✔
219
        $_POST      = $array;
72✔
220
    }
221

222
    /**
223
     * Get a value from $_COOKIE.
224
     *
225
     * @param cookie_items|null $default
226
     *
227
     * @return cookie_items|null
228
     */
229
    public function cookie(string $key, mixed $default = null): array|string|null
230
    {
231
        return $this->cookie[$key] ?? $default;
6✔
232
    }
233

234
    /**
235
     * Set a value in $_COOKIE.
236
     *
237
     * @param cookie_items $value
238
     */
239
    public function setCookie(string $key, array|string $value): void
240
    {
241
        $this->cookie[$key] = $value;
26✔
242
        $_COOKIE[$key]      = $value;
26✔
243
    }
244

245
    /**
246
     * Remove a key from $_COOKIE.
247
     */
248
    public function unsetCookie(string $key): void
249
    {
250
        unset($this->cookie[$key], $_COOKIE[$key]);
1✔
251
    }
252

253
    /**
254
     * Get all $_COOKIE values.
255
     *
256
     * @return array<string, cookie_items>
257
     */
258
    public function getCookieArray(): array
259
    {
260
        return $this->cookie;
1✔
261
    }
262

263
    /**
264
     * Set the entire $_COOKIE array.
265
     *
266
     * @param array<string, cookie_items> $array
267
     */
268
    public function setCookieArray(array $array): void
269
    {
270
        $this->cookie = $array;
12✔
271
        $_COOKIE      = $array;
12✔
272
    }
273

274
    /**
275
     * Get a value from $_REQUEST.
276
     *
277
     * @param request_items|null $default
278
     *
279
     * @return request_items|null
280
     */
281
    public function request(string $key, mixed $default = null): array|string|null
282
    {
283
        return $this->request[$key] ?? $default;
6✔
284
    }
285

286
    /**
287
     * Set a value in $_REQUEST.
288
     *
289
     * @param request_items $value
290
     */
291
    public function setRequest(string $key, array|string $value): void
292
    {
293
        $this->request[$key] = $value;
6✔
294
        $_REQUEST[$key]      = $value;
6✔
295
    }
296

297
    /**
298
     * Remove a key from $_REQUEST.
299
     */
300
    public function unsetRequest(string $key): void
301
    {
302
        unset($this->request[$key], $_REQUEST[$key]);
1✔
303
    }
304

305
    /**
306
     * Get all $_REQUEST values.
307
     *
308
     * @return array<string, request_items>
309
     */
310
    public function getRequestArray(): array
311
    {
312
        return $this->request;
1✔
313
    }
314

315
    /**
316
     * Set the entire $_REQUEST array.
317
     *
318
     * @param array<string, request_items> $array
319
     */
320
    public function setRequestArray(array $array): void
321
    {
322
        $this->request = $array;
39✔
323
        $_REQUEST      = $array;
39✔
324
    }
325

326
    /**
327
     * Get all $_FILES values.
328
     *
329
     * @return files_items
330
     */
331
    public function getFilesArray(): array
332
    {
333
        return $this->files;
93✔
334
    }
335

336
    /**
337
     * Set the entire $_FILES array.
338
     *
339
     * @param files_items $array
340
     */
341
    public function setFilesArray(array $array): void
342
    {
343
        $this->files = $array;
95✔
344
        $_FILES      = $array;
95✔
345
    }
346

347
    /**
348
     * Get a superglobal array by name.
349
     *
350
     * @param string $name The superglobal name (server, get, post, cookie, files, request)
351
     *
352
     * @return array<string, server_items>
353
     *
354
     * @throws InvalidArgumentException If the superglobal name is invalid
355
     */
356
    public function getGlobalArray(string $name): array
357
    {
358
        return match ($name) {
1,645✔
359
            'server'  => $this->server,
1,636✔
360
            'get'     => $this->get,
43✔
361
            'post'    => $this->post,
92✔
362
            'cookie'  => $this->cookie,
89✔
363
            'files'   => $this->files,
1✔
364
            'request' => $this->request,
18✔
365
            default   => throw new InvalidArgumentException(
1,645✔
366
                "Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.",
1,645✔
367
            ),
1,645✔
368
        };
1,645✔
369
    }
370

371
    /**
372
     * Set a superglobal array by name.
373
     *
374
     * @param string                      $name  The superglobal name (server, get, post, cookie, files, request)
375
     * @param array<string, server_items> $array The array to set
376
     *
377
     * @throws InvalidArgumentException If the superglobal name is invalid
378
     */
379
    public function setGlobalArray(string $name, array $array): void
380
    {
381
        match ($name) {
112✔
382
            'server'  => $this->setServerArray($array),
1✔
383
            'get'     => $this->setGetArray($array),
63✔
384
            'post'    => $this->setPostArray($array),
56✔
NEW
385
            'cookie'  => $this->setCookieArray($array),
×
386
            'files'   => $this->setFilesArray($array),
1✔
387
            'request' => $this->setRequestArray($array),
32✔
388
            default   => throw new InvalidArgumentException(
1✔
389
                "Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.",
1✔
390
            ),
1✔
391
        };
112✔
392
    }
393
}
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