Skip to content

Commit 519e2a3

Browse files
committed
types rework for superglobals class
1 parent 770afff commit 519e2a3

File tree

4 files changed

+63
-196
lines changed

4 files changed

+63
-196
lines changed

system/Superglobals.php

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,79 +25,56 @@
2525
* - $_GET, $_POST, $_REQUEST can contain nested arrays from query params like ?foo[bar]=value
2626
* - $_COOKIE typically contains strings but can have arrays with cookie[key] notation
2727
*
28+
* @phpstan-type server_items array<array-key, mixed>|float|int|string
29+
* @phpstan-type get_items array<array-key, mixed>|string
30+
* @phpstan-type post_items array<array-key, mixed>|string
31+
* @phpstan-type cookie_items array<array-key, mixed>|string
32+
* @phpstan-type files_items array<array-key, mixed>
33+
* @phpstan-type request_items array<array-key, mixed>|string
34+
*
2835
* @internal
2936
* @see \CodeIgniter\SuperglobalsTest
3037
*/
3138
final class Superglobals
3239
{
3340
/**
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
41+
* @param array<string, server_items>|null $server
42+
* @param array<string, get_items>|null $get
43+
* @param array<string, post_items>|null $post
44+
* @param array<string, cookie_items>|null $cookie
45+
* @param array<string, files_items>|null $files
46+
* @param array<string, request_items>|null $request
7047
*/
7148
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,
49+
private ?array $server = null,
50+
private ?array $get = null,
51+
private ?array $post = null,
52+
private ?array $cookie = null,
53+
private ?array $files = null,
54+
private ?array $request = null,
7855
) {
79-
$this->server = $server ?? $_SERVER;
80-
$this->get = $get ?? $_GET;
81-
$this->post = $post ?? $_POST;
82-
$this->cookie = $cookie ?? $_COOKIE;
83-
$this->files = $files ?? $_FILES;
84-
$this->request = $request ?? $_REQUEST;
56+
$this->server ??= $_SERVER;
57+
$this->get ??= $_GET;
58+
$this->post ??= $_POST;
59+
$this->cookie ??= $_COOKIE;
60+
$this->files ??= $_FILES;
61+
$this->request ??= $_REQUEST;
8562
}
8663

8764
/**
8865
* Get a value from $_SERVER.
8966
*
90-
* @return array<array-key, mixed>|float|int|string|null
67+
* @return server_items|null
9168
*/
92-
public function server(string $key): array|float|int|string|null
69+
public function server(string $key, mixed $default = null): array|float|int|string|null
9370
{
94-
return $this->server[$key] ?? null;
71+
return $this->server[$key] ?? $default;
9572
}
9673

9774
/**
9875
* Set a value in $_SERVER.
9976
*
100-
* @param array<array-key, mixed>|float|int|string $value
77+
* @param server_items $value
10178
*/
10279
public function setServer(string $key, array|float|int|string $value): void
10380
{
@@ -116,7 +93,7 @@ public function unsetServer(string $key): void
11693
/**
11794
* Get all $_SERVER values.
11895
*
119-
* @return array<string, array|float|int|string>
96+
* @return array<string, server_items>
12097
*/
12198
public function getServerArray(): array
12299
{
@@ -126,7 +103,7 @@ public function getServerArray(): array
126103
/**
127104
* Set the entire $_SERVER array.
128105
*
129-
* @param array<string, array|float|int|string> $array
106+
* @param array<string, server_items> $array
130107
*/
131108
public function setServerArray(array $array): void
132109
{
@@ -137,17 +114,17 @@ public function setServerArray(array $array): void
137114
/**
138115
* Get a value from $_GET.
139116
*
140-
* @return array<array-key, mixed>|string|null
117+
* @return get_items|null
141118
*/
142-
public function get(string $key): array|string|null
119+
public function get(string $key, mixed $default = null): array|string|null
143120
{
144-
return $this->get[$key] ?? null;
121+
return $this->get[$key] ?? $default;
145122
}
146123

147124
/**
148125
* Set a value in $_GET.
149126
*
150-
* @param array<array-key, mixed>|string $value
127+
* @param get_items $value
151128
*/
152129
public function setGet(string $key, array|string $value): void
153130
{
@@ -166,7 +143,7 @@ public function unsetGet(string $key): void
166143
/**
167144
* Get all $_GET values.
168145
*
169-
* @return array<string, array|string>
146+
* @return array<string, get_items>
170147
*/
171148
public function getGetArray(): array
172149
{
@@ -176,7 +153,7 @@ public function getGetArray(): array
176153
/**
177154
* Set the entire $_GET array.
178155
*
179-
* @param array<string, array|string> $array
156+
* @param array<string, get_items> $array
180157
*/
181158
public function setGetArray(array $array): void
182159
{
@@ -187,17 +164,17 @@ public function setGetArray(array $array): void
187164
/**
188165
* Get a value from $_POST.
189166
*
190-
* @return array<array-key, mixed>|string|null
167+
* @return post_items|null
191168
*/
192-
public function post(string $key): array|string|null
169+
public function post(string $key, mixed $default = null): array|string|null
193170
{
194-
return $this->post[$key] ?? null;
171+
return $this->post[$key] ?? $default;
195172
}
196173

197174
/**
198175
* Set a value in $_POST.
199176
*
200-
* @param array<array-key, mixed>|string $value
177+
* @param post_items $value
201178
*/
202179
public function setPost(string $key, array|string $value): void
203180
{
@@ -216,7 +193,7 @@ public function unsetPost(string $key): void
216193
/**
217194
* Get all $_POST values.
218195
*
219-
* @return array<string, array|string>
196+
* @return array<string, post_items>
220197
*/
221198
public function getPostArray(): array
222199
{
@@ -226,7 +203,7 @@ public function getPostArray(): array
226203
/**
227204
* Set the entire $_POST array.
228205
*
229-
* @param array<string, array|string> $array
206+
* @param array<string, post_items> $array
230207
*/
231208
public function setPostArray(array $array): void
232209
{
@@ -237,17 +214,17 @@ public function setPostArray(array $array): void
237214
/**
238215
* Get a value from $_COOKIE.
239216
*
240-
* @return array<array-key, mixed>|string|null
217+
* @return cookie_items|null
241218
*/
242-
public function cookie(string $key): array|string|null
219+
public function cookie(string $key, mixed $default = null): array|string|null
243220
{
244-
return $this->cookie[$key] ?? null;
221+
return $this->cookie[$key] ?? $default;
245222
}
246223

247224
/**
248225
* Set a value in $_COOKIE.
249226
*
250-
* @param array<array-key, mixed>|string $value
227+
* @param cookie_items $value
251228
*/
252229
public function setCookie(string $key, array|string $value): void
253230
{
@@ -266,7 +243,7 @@ public function unsetCookie(string $key): void
266243
/**
267244
* Get all $_COOKIE values.
268245
*
269-
* @return array<string, array|string>
246+
* @return array<string, cookie_items>
270247
*/
271248
public function getCookieArray(): array
272249
{
@@ -276,7 +253,7 @@ public function getCookieArray(): array
276253
/**
277254
* Set the entire $_COOKIE array.
278255
*
279-
* @param array<string, array|string> $array
256+
* @param array<string, cookie_items> $array
280257
*/
281258
public function setCookieArray(array $array): void
282259
{
@@ -287,17 +264,17 @@ public function setCookieArray(array $array): void
287264
/**
288265
* Get a value from $_REQUEST.
289266
*
290-
* @return array<array-key, mixed>|string|null
267+
* @return request_items|null
291268
*/
292-
public function request(string $key): array|string|null
269+
public function request(string $key, mixed $default = null): array|string|null
293270
{
294-
return $this->request[$key] ?? null;
271+
return $this->request[$key] ?? $default;
295272
}
296273

297274
/**
298275
* Set a value in $_REQUEST.
299276
*
300-
* @param array<array-key, mixed>|string $value
277+
* @param request_items $value
301278
*/
302279
public function setRequest(string $key, array|string $value): void
303280
{
@@ -316,7 +293,7 @@ public function unsetRequest(string $key): void
316293
/**
317294
* Get all $_REQUEST values.
318295
*
319-
* @return array<string, array|string>
296+
* @return array<string, request_items>
320297
*/
321298
public function getRequestArray(): array
322299
{
@@ -326,7 +303,7 @@ public function getRequestArray(): array
326303
/**
327304
* Set the entire $_REQUEST array.
328305
*
329-
* @param array<string, array|string> $array
306+
* @param array<string, request_items> $array
330307
*/
331308
public function setRequestArray(array $array): void
332309
{
@@ -337,7 +314,7 @@ public function setRequestArray(array $array): void
337314
/**
338315
* Get all $_FILES values.
339316
*
340-
* @return array<string, array<string, mixed>>
317+
* @return files_items
341318
*/
342319
public function getFilesArray(): array
343320
{
@@ -347,7 +324,7 @@ public function getFilesArray(): array
347324
/**
348325
* Set the entire $_FILES array.
349326
*
350-
* @param array<string, array<string, mixed>> $array
327+
* @param files_items $array
351328
*/
352329
public function setFilesArray(array $array): void
353330
{
@@ -360,7 +337,7 @@ public function setFilesArray(array $array): void
360337
*
361338
* @param string $name The superglobal name (server, get, post, cookie, files, request)
362339
*
363-
* @return array<string, array|float|int|string>
340+
* @return array<string, server_items>
364341
*/
365342
public function getGlobalArray(string $name): array
366343
{
@@ -378,8 +355,8 @@ public function getGlobalArray(string $name): array
378355
/**
379356
* Set a superglobal array by name.
380357
*
381-
* @param string $name The superglobal name (server, get, post, cookie, files, request)
382-
* @param array<string, array|float|int|string> $array The array to set
358+
* @param string $name The superglobal name (server, get, post, cookie, files, request)
359+
* @param array<string, server_items> $array The array to set
383360
*/
384361
public function setGlobalArray(string $name, array $array): void
385362
{

utils/phpstan-baseline/argument.type.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ parameters:
123123
path: ../../tests/system/HTTP/HeaderTest.php
124124

125125
-
126-
message: '#^Parameter \#1 \$array of method CodeIgniter\\Superglobals\:\:setServerArray\(\) expects array\<string, array\|float\|int\|string\>, array\<string, string\|null\> given\.$#'
126+
message: '#^Parameter \#1 \$array of method CodeIgniter\\Superglobals\:\:setServerArray\(\) expects array\<string, array\<mixed\>\|float\|int\|string\>, array\<string, string\|null\> given\.$#'
127127
count: 1
128128
path: ../../tests/system/HTTP/MessageTest.php
129129

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2221 errors
1+
# total 2199 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)