Skip to content

Commit 85639d3

Browse files
michalsnneznaika0
andcommitted
set array in the constructor and fluent api
Co-authored-by: neznaika0 <ozornick.ks@gmail.com>
1 parent e30d499 commit 85639d3

File tree

12 files changed

+157
-53
lines changed

12 files changed

+157
-53
lines changed

system/Pager/Pager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ protected function calculateCurrentPage(string $group)
434434
} else {
435435
$pageSelector = $this->groups[$group]['pageSelector'];
436436

437-
$page = (int) (service('superglobals')->get($pageSelector, 1));
437+
$page = (int) (service('superglobals')->get($pageSelector, '1'));
438438

439439
$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
440440
}

system/Superglobals.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public function __construct(
5555
private ?array $files = null,
5656
private ?array $request = null,
5757
) {
58-
$this->server ??= $_SERVER;
59-
$this->get ??= $_GET;
60-
$this->post ??= $_POST;
61-
$this->cookie ??= $_COOKIE;
62-
$this->files ??= $_FILES;
63-
$this->request ??= $_REQUEST;
58+
$this->setServerArray($server ?? $_SERVER);
59+
$this->setGetArray($get ?? $_GET);
60+
$this->setPostArray($post ?? $_POST);
61+
$this->setCookieArray($cookie ?? $_COOKIE);
62+
$this->setFilesArray($files ?? $_FILES);
63+
$this->setRequestArray($request ?? $_REQUEST);
6464
}
6565

6666
/**
@@ -80,18 +80,22 @@ public function server(string $key, mixed $default = null): array|float|int|stri
8080
*
8181
* @param server_items $value
8282
*/
83-
public function setServer(string $key, array|float|int|string $value): void
83+
public function setServer(string $key, array|float|int|string $value): self
8484
{
8585
$this->server[$key] = $value;
8686
$_SERVER[$key] = $value;
87+
88+
return $this;
8789
}
8890

8991
/**
9092
* Remove a key from $_SERVER.
9193
*/
92-
public function unsetServer(string $key): void
94+
public function unsetServer(string $key): self
9395
{
9496
unset($this->server[$key], $_SERVER[$key]);
97+
98+
return $this;
9599
}
96100

97101
/**
@@ -109,10 +113,12 @@ public function getServerArray(): array
109113
*
110114
* @param array<string, server_items> $array
111115
*/
112-
public function setServerArray(array $array): void
116+
public function setServerArray(array $array): self
113117
{
114118
$this->server = $array;
115119
$_SERVER = $array;
120+
121+
return $this;
116122
}
117123

118124
/**
@@ -132,18 +138,22 @@ public function get(string $key, mixed $default = null): array|string|null
132138
*
133139
* @param get_items $value
134140
*/
135-
public function setGet(string $key, array|string $value): void
141+
public function setGet(string $key, array|string $value): self
136142
{
137143
$this->get[$key] = $value;
138144
$_GET[$key] = $value;
145+
146+
return $this;
139147
}
140148

141149
/**
142150
* Remove a key from $_GET.
143151
*/
144-
public function unsetGet(string $key): void
152+
public function unsetGet(string $key): self
145153
{
146154
unset($this->get[$key], $_GET[$key]);
155+
156+
return $this;
147157
}
148158

149159
/**
@@ -161,10 +171,12 @@ public function getGetArray(): array
161171
*
162172
* @param array<string, get_items> $array
163173
*/
164-
public function setGetArray(array $array): void
174+
public function setGetArray(array $array): self
165175
{
166176
$this->get = $array;
167177
$_GET = $array;
178+
179+
return $this;
168180
}
169181

170182
/**
@@ -184,18 +196,22 @@ public function post(string $key, mixed $default = null): array|string|null
184196
*
185197
* @param post_items $value
186198
*/
187-
public function setPost(string $key, array|string $value): void
199+
public function setPost(string $key, array|string $value): self
188200
{
189201
$this->post[$key] = $value;
190202
$_POST[$key] = $value;
203+
204+
return $this;
191205
}
192206

193207
/**
194208
* Remove a key from $_POST.
195209
*/
196-
public function unsetPost(string $key): void
210+
public function unsetPost(string $key): self
197211
{
198212
unset($this->post[$key], $_POST[$key]);
213+
214+
return $this;
199215
}
200216

201217
/**
@@ -213,10 +229,12 @@ public function getPostArray(): array
213229
*
214230
* @param array<string, post_items> $array
215231
*/
216-
public function setPostArray(array $array): void
232+
public function setPostArray(array $array): self
217233
{
218234
$this->post = $array;
219235
$_POST = $array;
236+
237+
return $this;
220238
}
221239

222240
/**
@@ -236,18 +254,22 @@ public function cookie(string $key, mixed $default = null): array|string|null
236254
*
237255
* @param cookie_items $value
238256
*/
239-
public function setCookie(string $key, array|string $value): void
257+
public function setCookie(string $key, array|string $value): self
240258
{
241259
$this->cookie[$key] = $value;
242260
$_COOKIE[$key] = $value;
261+
262+
return $this;
243263
}
244264

245265
/**
246266
* Remove a key from $_COOKIE.
247267
*/
248-
public function unsetCookie(string $key): void
268+
public function unsetCookie(string $key): self
249269
{
250270
unset($this->cookie[$key], $_COOKIE[$key]);
271+
272+
return $this;
251273
}
252274

253275
/**
@@ -265,10 +287,12 @@ public function getCookieArray(): array
265287
*
266288
* @param array<string, cookie_items> $array
267289
*/
268-
public function setCookieArray(array $array): void
290+
public function setCookieArray(array $array): self
269291
{
270292
$this->cookie = $array;
271293
$_COOKIE = $array;
294+
295+
return $this;
272296
}
273297

274298
/**
@@ -288,18 +312,22 @@ public function request(string $key, mixed $default = null): array|string|null
288312
*
289313
* @param request_items $value
290314
*/
291-
public function setRequest(string $key, array|string $value): void
315+
public function setRequest(string $key, array|string $value): self
292316
{
293317
$this->request[$key] = $value;
294318
$_REQUEST[$key] = $value;
319+
320+
return $this;
295321
}
296322

297323
/**
298324
* Remove a key from $_REQUEST.
299325
*/
300-
public function unsetRequest(string $key): void
326+
public function unsetRequest(string $key): self
301327
{
302328
unset($this->request[$key], $_REQUEST[$key]);
329+
330+
return $this;
303331
}
304332

305333
/**
@@ -317,10 +345,12 @@ public function getRequestArray(): array
317345
*
318346
* @param array<string, request_items> $array
319347
*/
320-
public function setRequestArray(array $array): void
348+
public function setRequestArray(array $array): self
321349
{
322350
$this->request = $array;
323351
$_REQUEST = $array;
352+
353+
return $this;
324354
}
325355

326356
/**
@@ -338,10 +368,12 @@ public function getFilesArray(): array
338368
*
339369
* @param files_items $array
340370
*/
341-
public function setFilesArray(array $array): void
371+
public function setFilesArray(array $array): self
342372
{
343373
$this->files = $array;
344374
$_FILES = $array;
375+
376+
return $this;
345377
}
346378

347379
/**

tests/system/Filters/InvalidCharsTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ protected function setUp(): void
3939
{
4040
parent::setUp();
4141

42-
$_GET = [];
43-
$_POST = [];
44-
$_COOKIE = [];
45-
46-
Services::injectMock('superglobals', new Superglobals());
42+
Services::injectMock('superglobals', new Superglobals(null, [], [], []));
4743

4844
$this->request = $this->createRequest();
4945
$this->invalidChars = new InvalidChars();

tests/system/HTTP/CLIRequestTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ protected function setUp(): void
3636
{
3737
parent::setUp();
3838

39-
$_POST = [];
40-
$_GET = [];
41-
4239
Services::injectMock('superglobals', new Superglobals(['argv' => []], [], [], [], []));
4340

4441
$this->request = new CLIRequest(new App());

tests/system/HTTP/MessageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
use CodeIgniter\HTTP\Exceptions\HTTPException;
1919
use CodeIgniter\Superglobals;
2020
use CodeIgniter\Test\CIUnitTestCase;
21+
use PHPUnit\Framework\Attributes\BackupGlobals;
2122
use PHPUnit\Framework\Attributes\DataProvider;
2223
use PHPUnit\Framework\Attributes\Group;
2324

2425
/**
2526
* @internal
2627
*/
28+
#[BackupGlobals(true)]
2729
#[Group('Others')]
2830
final class MessageTest extends CIUnitTestCase
2931
{
@@ -259,7 +261,6 @@ public function testPopulateHeadersWithoutContentType(): void
259261
$this->assertNull($this->message->header('content-type'));
260262

261263
putenv("CONTENT_TYPE={$originalEnv}");
262-
$superglobals->setServerArray($original); // restore so code coverage doesn't break
263264
}
264265

265266
public function testPopulateHeadersWithoutHTTP(): void

tests/system/HTTP/RequestTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ protected function setUp(): void
3434
{
3535
parent::setUp();
3636

37-
$_POST = [];
38-
$_GET = [];
39-
40-
Services::injectMock('superglobals', new Superglobals());
37+
Services::injectMock('superglobals', new Superglobals(null, [], []));
4138

4239
$this->request = new Request(new App());
4340
}

tests/system/HTTP/SiteURIFactoryTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ protected function setUp(): void
3232
{
3333
parent::setUp();
3434

35-
$_GET = $_SERVER = [];
36-
37-
Services::injectMock('superglobals', new Superglobals());
35+
Services::injectMock('superglobals', new Superglobals([], []));
3836
}
3937

4038
private function createSiteURIFactory(?App $config = null, ?Superglobals $superglobals = null): SiteURIFactory

tests/system/Pager/PagerTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ protected function setUp(): void
4141
{
4242
parent::setUp();
4343

44-
$_SERVER = $_GET = $_POST = $_COOKIE = $_FILES = $_REQUEST = [];
45-
46-
Services::injectMock('superglobals', new Superglobals());
44+
Services::injectMock('superglobals', new Superglobals([], [], [], [], [], []));
4745

4846
$this->createPager('/');
4947
}

tests/system/Security/SecurityCSRFCookieRandomizeTokenTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ protected function setUp(): void
4848
{
4949
parent::setUp();
5050

51-
$_COOKIE = [];
52-
Services::injectMock('superglobals', new Superglobals());
51+
Services::injectMock('superglobals', new Superglobals(null, null, null, []));
5352

5453
$this->config = new SecurityConfig();
5554
$this->config->csrfProtection = Security::CSRF_PROTECTION_COOKIE;

tests/system/Security/SecurityTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ protected function setUp(): void
4040
{
4141
parent::setUp();
4242

43-
$_COOKIE = [];
44-
4543
$this->resetServices();
4644

47-
Services::injectMock('superglobals', new Superglobals());
45+
Services::injectMock('superglobals', new Superglobals(null, null, null, []));
4846
}
4947

5048
private static function createMockSecurity(SecurityConfig $config = new SecurityConfig()): MockSecurity

0 commit comments

Comments
 (0)