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

codeigniter4 / CodeIgniter4 / 20566093033

29 Dec 2025 06:02AM UTC coverage: 84.546% (+0.02%) from 84.522%
20566093033

Pull #9858

github

web-flow
Merge 79091a326 into e2fc5243b
Pull Request #9858: feat: complete `Superglobals` implementation

114 of 116 new or added lines in 19 files covered. (98.28%)

1 existing line in 1 file now uncovered.

21599 of 25547 relevant lines covered (84.55%)

204.13 hits per line

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

98.39
/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
/**
17
 * Superglobals manipulation.
18
 *
19
 * Provides a clean API for accessing and manipulating PHP superglobals
20
 * with support for testing and backward compatibility.
21
 *
22
 * Note on return types:
23
 * - $_SERVER can contain int (argc, REQUEST_TIME) or float (REQUEST_TIME_FLOAT)
24
 * - $_SERVER['argv'] is an array
25
 * - $_GET, $_POST, $_REQUEST can contain nested arrays from query params like ?foo[bar]=value
26
 * - $_COOKIE typically contains strings but can have arrays with cookie[key] notation
27
 *
28
 * @internal
29
 * @see \CodeIgniter\SuperglobalsTest
30
 */
31
final class Superglobals
32
{
33
    /**
34
     * @var array<string, array|float|int|string>
35
     */
36
    private array $server;
37

38
    /**
39
     * @var array<string, array|string>
40
     */
41
    private array $get;
42

43
    /**
44
     * @var array<string, array|string>
45
     */
46
    private array $post;
47

48
    /**
49
     * @var array<string, array|string>
50
     */
51
    private array $cookie;
52

53
    /**
54
     * @var array<string, array|string>
55
     */
56
    private array $request;
57

58
    /**
59
     * @var array<string, array<string, mixed>>
60
     */
61
    private array $files;
62

63
    /**
64
     * @param array<string, array|float|int|string>|null $server
65
     * @param array<string, array|string>|null           $get
66
     * @param array<string, array|string>|null           $post
67
     * @param array<string, array|string>|null           $cookie
68
     * @param array<string, array<string, mixed>>|null   $files
69
     * @param array<string, array|string>|null           $request
70
     */
71
    public function __construct(
72
        ?array $server = null,
73
        ?array $get = null,
74
        ?array $post = null,
75
        ?array $cookie = null,
76
        ?array $files = null,
77
        ?array $request = null,
78
    ) {
79
        $this->server  = $server ?? $_SERVER;
2,323✔
80
        $this->get     = $get ?? $_GET;
2,323✔
81
        $this->post    = $post ?? $_POST;
2,323✔
82
        $this->cookie  = $cookie ?? $_COOKIE;
2,323✔
83
        $this->files   = $files ?? $_FILES;
2,323✔
84
        $this->request = $request ?? $_REQUEST;
2,323✔
85
    }
86

87
    /**
88
     * Get a value from $_SERVER
89
     *
90
     * @return array<array-key, mixed>|float|int|string|null
91
     */
92
    public function server(string $key): array|float|int|string|null
93
    {
94
        return $this->server[$key] ?? null;
1,715✔
95
    }
96

97
    /**
98
     * Set a value in $_SERVER
99
     *
100
     * @param array<array-key, mixed>|float|int|string $value
101
     */
102
    public function setServer(string $key, array|float|int|string $value): void
103
    {
104
        $this->server[$key] = $value;
489✔
105
        $_SERVER[$key]      = $value;
489✔
106
    }
107

108
    /**
109
     * Remove a key from $_SERVER
110
     */
111
    public function unsetServer(string $key): void
112
    {
113
        unset($this->server[$key], $_SERVER[$key]);
24✔
114
    }
115

116
    /**
117
     * Get all $_SERVER values
118
     *
119
     * @return array<string, array|float|int|string>
120
     */
121
    public function getServerArray(): array
122
    {
123
        return $this->server;
1,607✔
124
    }
125

126
    /**
127
     * Set the entire $_SERVER array
128
     *
129
     * @param array<string, array|float|int|string> $array
130
     */
131
    public function setServerArray(array $array): void
132
    {
133
        $this->server = $array;
209✔
134
        $_SERVER      = $array;
209✔
135
    }
136

137
    /**
138
     * Get a value from $_GET
139
     *
140
     * @return array<array-key, mixed>|string|null
141
     */
142
    public function get(string $key): array|string|null
143
    {
144
        return $this->get[$key] ?? null;
52✔
145
    }
146

147
    /**
148
     * Set a value in $_GET
149
     *
150
     * @param array<array-key, mixed>|string $value
151
     */
152
    public function setGet(string $key, array|string $value): void
153
    {
154
        $this->get[$key] = $value;
42✔
155
        $_GET[$key]      = $value;
42✔
156
    }
157

158
    /**
159
     * Remove a key from $_GET
160
     */
161
    public function unsetGet(string $key): void
162
    {
163
        unset($this->get[$key], $_GET[$key]);
1✔
164
    }
165

166
    /**
167
     * Get all $_GET values
168
     *
169
     * @return array<string, array|string>
170
     */
171
    public function getGetArray(): array
172
    {
173
        return $this->get;
324✔
174
    }
175

176
    /**
177
     * Set the entire $_GET array
178
     *
179
     * @param array<string, array|string> $array
180
     */
181
    public function setGetArray(array $array): void
182
    {
183
        $this->get = $array;
200✔
184
        $_GET      = $array;
200✔
185
    }
186

187
    /**
188
     * Get a value from $_POST
189
     *
190
     * @return array<array-key, mixed>|string|null
191
     */
192
    public function post(string $key): array|string|null
193
    {
194
        return $this->post[$key] ?? null;
35✔
195
    }
196

197
    /**
198
     * Set a value in $_POST
199
     *
200
     * @param array<array-key, mixed>|string $value
201
     */
202
    public function setPost(string $key, array|string $value): void
203
    {
204
        $this->post[$key] = $value;
57✔
205
        $_POST[$key]      = $value;
57✔
206
    }
207

208
    /**
209
     * Remove a key from $_POST
210
     */
211
    public function unsetPost(string $key): void
212
    {
213
        unset($this->post[$key], $_POST[$key]);
15✔
214
    }
215

216
    /**
217
     * Get all $_POST values
218
     *
219
     * @return array<string, array|string>
220
     */
221
    public function getPostArray(): array
222
    {
223
        return $this->post;
277✔
224
    }
225

226
    /**
227
     * Set the entire $_POST array
228
     *
229
     * @param array<string, array|string> $array
230
     */
231
    public function setPostArray(array $array): void
232
    {
233
        $this->post = $array;
72✔
234
        $_POST      = $array;
72✔
235
    }
236

237
    /**
238
     * Get a value from $_COOKIE
239
     *
240
     * @return array<array-key, mixed>|string|null
241
     */
242
    public function cookie(string $key): array|string|null
243
    {
244
        return $this->cookie[$key] ?? null;
6✔
245
    }
246

247
    /**
248
     * Set a value in $_COOKIE
249
     *
250
     * @param array<array-key, mixed>|string $value
251
     */
252
    public function setCookie(string $key, array|string $value): void
253
    {
254
        $this->cookie[$key] = $value;
26✔
255
        $_COOKIE[$key]      = $value;
26✔
256
    }
257

258
    /**
259
     * Remove a key from $_COOKIE
260
     */
261
    public function unsetCookie(string $key): void
262
    {
263
        unset($this->cookie[$key], $_COOKIE[$key]);
1✔
264
    }
265

266
    /**
267
     * Get all $_COOKIE values
268
     *
269
     * @return array<string, array|string>
270
     */
271
    public function getCookieArray(): array
272
    {
273
        return $this->cookie;
1✔
274
    }
275

276
    /**
277
     * Set the entire $_COOKIE array
278
     *
279
     * @param array<string, array|string> $array
280
     */
281
    public function setCookieArray(array $array): void
282
    {
283
        $this->cookie = $array;
12✔
284
        $_COOKIE      = $array;
12✔
285
    }
286

287
    /**
288
     * Get a value from $_REQUEST
289
     *
290
     * @return array<array-key, mixed>|string|null
291
     */
292
    public function request(string $key): array|string|null
293
    {
294
        return $this->request[$key] ?? null;
6✔
295
    }
296

297
    /**
298
     * Set a value in $_REQUEST
299
     *
300
     * @param array<array-key, mixed>|string $value
301
     */
302
    public function setRequest(string $key, array|string $value): void
303
    {
304
        $this->request[$key] = $value;
6✔
305
        $_REQUEST[$key]      = $value;
6✔
306
    }
307

308
    /**
309
     * Remove a key from $_REQUEST
310
     */
311
    public function unsetRequest(string $key): void
312
    {
313
        unset($this->request[$key], $_REQUEST[$key]);
1✔
314
    }
315

316
    /**
317
     * Get all $_REQUEST values
318
     *
319
     * @return array<string, array|string>
320
     */
321
    public function getRequestArray(): array
322
    {
323
        return $this->request;
1✔
324
    }
325

326
    /**
327
     * Set the entire $_REQUEST array
328
     *
329
     * @param array<string, array|string> $array
330
     */
331
    public function setRequestArray(array $array): void
332
    {
333
        $this->request = $array;
39✔
334
        $_REQUEST      = $array;
39✔
335
    }
336

337
    /**
338
     * Get all $_FILES values
339
     *
340
     * @return array<string, array<string, mixed>>
341
     */
342
    public function getFilesArray(): array
343
    {
344
        return $this->files;
93✔
345
    }
346

347
    /**
348
     * Set the entire $_FILES array
349
     *
350
     * @param array<string, array<string, mixed>> $array
351
     */
352
    public function setFilesArray(array $array): void
353
    {
354
        $this->files = $array;
95✔
355
        $_FILES      = $array;
95✔
356
    }
357

358
    /**
359
     * Get a superglobal array by name
360
     *
361
     * @param string $name The superglobal name (server, get, post, cookie, request, files)
362
     *
363
     * @return array<string, array|float|int|string>
364
     */
365
    public function getGlobalArray(string $name): array
366
    {
367
        return match ($name) {
1,638✔
368
            'server'  => $this->server,
1,629✔
369
            'get'     => $this->get,
41✔
370
            'post'    => $this->post,
90✔
371
            'cookie'  => $this->cookie,
86✔
372
            'request' => $this->request,
18✔
373
            'files'   => $this->files,
1✔
374
            default   => [],
1,638✔
375
        };
1,638✔
376
    }
377

378
    /**
379
     * Set a superglobal array by name
380
     *
381
     * @param string                                $name  The superglobal name (server, get, post, cookie, request, files)
382
     * @param array<string, array|float|int|string> $array The array to set
383
     */
384
    public function setGlobalArray(string $name, array $array): void
385
    {
386
        match ($name) {
112✔
387
            'server'  => $this->setServerArray($array),
1✔
388
            'get'     => $this->setGetArray($array),
63✔
389
            'post'    => $this->setPostArray($array),
56✔
NEW
390
            'cookie'  => $this->setCookieArray($array),
×
391
            'request' => $this->setRequestArray($array),
32✔
392
            'files'   => $this->setFilesArray($array),
1✔
393
            default   => null,
1✔
394
        };
112✔
395
    }
396
}
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