Skip to content

Commit ff6b80a

Browse files
committed
refactor: remove $config in Response constructor
1 parent 658e1a5 commit ff6b80a

24 files changed

+184
-237
lines changed

system/Config/Services.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public static function csp(?CSPConfig $config = null, bool $getShared = true)
198198
* The CURL Request class acts as a simple HTTP client for interacting
199199
* with other servers, typically through APIs.
200200
*
201+
* @todo v4.8.0 Remove $config parameter since unused
202+
*
201203
* @return CURLRequest
202204
*/
203205
public static function curlrequest(array $options = [], ?ResponseInterface $response = null, ?App $config = null, bool $getShared = true)
@@ -207,7 +209,7 @@ public static function curlrequest(array $options = [], ?ResponseInterface $resp
207209
}
208210

209211
$config ??= config(App::class);
210-
$response ??= new Response($config);
212+
$response ??= new Response();
211213

212214
return new CURLRequest(
213215
$config,
@@ -576,32 +578,33 @@ public static function incomingrequest(?App $config = null, bool $getShared = tr
576578
/**
577579
* The Response class models an HTTP response.
578580
*
581+
* @todo v4.8.0 Remove $config parameter since unused
582+
*
579583
* @return ResponseInterface
580584
*/
581585
public static function response(?App $config = null, bool $getShared = true)
582586
{
583587
if ($getShared) {
584-
return static::getSharedInstance('response', $config);
588+
return static::getSharedInstance('response');
585589
}
586590

587-
$config ??= config(App::class);
588-
589-
return new Response($config);
591+
return new Response();
590592
}
591593

592594
/**
593595
* The Redirect class provides nice way of working with redirects.
594596
*
597+
* @todo v4.8.0 Remove $config parameter since unused
598+
*
595599
* @return RedirectResponse
596600
*/
597601
public static function redirectresponse(?App $config = null, bool $getShared = true)
598602
{
599603
if ($getShared) {
600-
return static::getSharedInstance('redirectresponse', $config);
604+
return static::getSharedInstance('redirectresponse');
601605
}
602606

603-
$config ??= config(App::class);
604-
$response = new RedirectResponse($config);
607+
$response = new RedirectResponse();
605608
$response->setProtocolVersion(AppServices::get('request')->getProtocolVersion());
606609

607610
return $response;

system/HTTP/CURLRequest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ class CURLRequest extends OutgoingRequest
115115
* - timeout
116116
* - any other request options to use as defaults.
117117
*
118+
* @todo v4.8.0 Remove $config parameter since unused
119+
*
118120
* @param array<string, mixed> $options
121+
*
122+
* @phpstan-ignore-next-line constructor.unusedParameter
119123
*/
120124
public function __construct(App $config, URI $uri, ?ResponseInterface $response = null, array $options = [])
121125
{
@@ -125,7 +129,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
125129

126130
parent::__construct(Method::GET, $uri);
127131

128-
$this->responseOrig = $response ?? new Response($config);
132+
$this->responseOrig = $response ?? new Response();
129133
// Remove the default Content-Type header.
130134
$this->responseOrig->removeHeader('Content-Type');
131135

system/HTTP/DownloadResponse.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Exceptions\DownloadException;
1717
use CodeIgniter\Files\File;
18-
use Config\App;
1918
use Config\Mimes;
2019

2120
/**
@@ -64,12 +63,9 @@ class DownloadResponse extends Response implements NonBufferedResponseInterface
6463
*/
6564
protected $statusCode = 200;
6665

67-
/**
68-
* Constructor.
69-
*/
7066
public function __construct(string $filename, bool $setMime)
7167
{
72-
parent::__construct(config(App::class));
68+
parent::__construct();
7369

7470
$this->filename = $filename;
7571
$this->setMime = $setMime;

system/HTTP/Response.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\Cookie\Cookie;
1717
use CodeIgniter\Cookie\CookieStore;
1818
use CodeIgniter\HTTP\Exceptions\HTTPException;
19-
use Config\App;
2019
use Config\Cookie as CookieConfig;
2120

2221
/**
@@ -141,16 +140,7 @@ class Response extends Message implements ResponseInterface
141140
*/
142141
protected $pretend = false;
143142

144-
/**
145-
* Constructor
146-
*
147-
* @param App $config
148-
*
149-
* @todo Recommend removing reliance on config injection
150-
*
151-
* @deprecated 4.5.0 The param $config is no longer used.
152-
*/
153-
public function __construct($config) // @phpstan-ignore-line
143+
public function __construct()
154144
{
155145
// Default to a non-caching page.
156146
// Also ensures that a Cache-control header exists.

system/HTTP/SSEResponse.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use Closure;
17-
use Config\App;
1817
use JsonException;
1918

2019
/**
@@ -31,7 +30,7 @@ class SSEResponse extends Response implements NonBufferedResponseInterface
3130
*/
3231
public function __construct(private readonly Closure $callback)
3332
{
34-
parent::__construct(config(App::class));
33+
parent::__construct();
3534
}
3635

3736
/**

tests/system/API/ResponseTraitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function createRequestAndResponse(string $routePath = '', array $userHea
112112
null,
113113
new UserAgent(),
114114
);
115-
$this->response = new MockResponse($config);
115+
$this->response = new MockResponse();
116116
}
117117

118118
$headers = array_merge(['Accept' => 'text/html'], $userHeaders);
@@ -625,7 +625,7 @@ public function testFormatByRequestNegotiateIfFormatIsNotJsonOrXML(): void
625625
$this->createCookieConfig();
626626

627627
$request = new MockIncomingRequest($config, new SiteURI($config), null, new UserAgent());
628-
$response = new MockResponse($config);
628+
$response = new MockResponse();
629629

630630
$controller = new class ($request, $response) {
631631
use ResponseTrait;

tests/system/Cache/ResponseCacheTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testCachePageIncomingRequest(): void
8787
{
8888
$pageCache = $this->createResponseCache();
8989

90-
$response = new Response(new App());
90+
$response = new Response();
9191
$response->setHeader('ETag', 'abcd1234');
9292
$response->setBody('The response body.');
9393

@@ -97,37 +97,37 @@ public function testCachePageIncomingRequest(): void
9797
));
9898

9999
// Check cache with a request with the same URI path.
100-
$cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App()));
100+
$cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response());
101101
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
102102
$this->assertSame('The response body.', $cachedResponse->getBody());
103103
$this->assertSame('abcd1234', $cachedResponse->getHeaderLine('ETag'));
104104

105105
// Check cache with a request with the same URI path and different query string.
106106
$cachedResponse = $pageCache->get(
107107
$this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']),
108-
new Response(new App()),
108+
new Response(),
109109
);
110110
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
111111
$this->assertSame('The response body.', $cachedResponse->getBody());
112112
$this->assertSame('abcd1234', $cachedResponse->getHeaderLine('ETag'));
113113

114114
// Check cache with another request with the different URI path.
115-
$cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response(new App()));
115+
$cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response());
116116
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
117117
}
118118

119119
public function testCachePageIncomingRequestWithStatus(): void
120120
{
121121
$pageCache = $this->createResponseCache();
122122

123-
$response = new Response(new App());
123+
$response = new Response();
124124
$response->setStatusCode(432, 'Foo Bar');
125125
$response->setBody('The response body.');
126126

127127
$this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response));
128128

129129
// Check cached response status
130-
$cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App()));
130+
$cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response());
131131
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
132132
$this->assertSame(432, $cachedResponse->getStatusCode());
133133
$this->assertSame('Foo Bar', $cachedResponse->getReasonPhrase());
@@ -143,7 +143,7 @@ public function testCachePageIncomingRequestWithCacheQueryString(): void
143143

144144
$request = $this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']);
145145

146-
$response = new Response(new App());
146+
$response = new Response();
147147
$response->setHeader('ETag', 'abcd1234');
148148
$response->setBody('The response body.');
149149

@@ -152,7 +152,7 @@ public function testCachePageIncomingRequestWithCacheQueryString(): void
152152
// Check cache with a request with the same URI path and same query string.
153153
$cachedResponse = $pageCache->get(
154154
$this->createIncomingRequest('foo/bar', ['foo' => 'bar', 'bar' => 'baz']),
155-
new Response(new App()),
155+
new Response(),
156156
);
157157
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
158158
$this->assertSame('The response body.', $cachedResponse->getBody());
@@ -161,28 +161,28 @@ public function testCachePageIncomingRequestWithCacheQueryString(): void
161161
// Check cache with a request with the same URI path and different query string.
162162
$cachedResponse = $pageCache->get(
163163
$this->createIncomingRequest('foo/bar', ['xfoo' => 'bar', 'bar' => 'baz']),
164-
new Response(new App()),
164+
new Response(),
165165
);
166166
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
167167

168168
// Check cache with another request with the different URI path.
169-
$cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response(new App()));
169+
$cachedResponse = $pageCache->get($this->createIncomingRequest('another'), new Response());
170170
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
171171
}
172172

173173
public function testCachePageIncomingRequestWithHttpMethods(): void
174174
{
175175
$pageCache = $this->createResponseCache();
176176

177-
$response = new Response(new App());
177+
$response = new Response();
178178
$response->setBody('The response body.');
179179

180180
$this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response));
181181

182182
// Check cache with a request with the same URI path and different HTTP method
183183
$cachedResponse = $pageCache->get(
184184
$this->createIncomingRequest('foo/bar')->withMethod('POST'),
185-
new Response(new App()),
185+
new Response(),
186186
);
187187
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
188188
}
@@ -191,18 +191,18 @@ public function testCachePageCLIRequest(): void
191191
{
192192
$pageCache = $this->createResponseCache();
193193

194-
$response = new Response(new App());
194+
$response = new Response();
195195
$response->setBody('The response body.');
196196

197197
$this->assertTrue($pageCache->make($this->createCLIRequest(['foo', 'bar']), $response));
198198

199199
// Check cache with a request with the same params.
200-
$cachedResponse = $pageCache->get($this->createCLIRequest(['foo', 'bar']), new Response(new App()));
200+
$cachedResponse = $pageCache->get($this->createCLIRequest(['foo', 'bar']), new Response());
201201
$this->assertInstanceOf(ResponseInterface::class, $cachedResponse);
202202
$this->assertSame('The response body.', $cachedResponse->getBody());
203203

204204
// Check cache with another request with the different params.
205-
$cachedResponse = $pageCache->get($this->createCLIRequest(['baz']), new Response(new App()));
205+
$cachedResponse = $pageCache->get($this->createCLIRequest(['baz']), new Response());
206206
$this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse);
207207
}
208208

@@ -217,7 +217,7 @@ public function testUnserializeError(): void
217217

218218
$request = $this->createIncomingRequest('foo/bar');
219219

220-
$response = new Response(new App());
220+
$response = new Response();
221221
$response->setHeader('ETag', 'abcd1234');
222222
$response->setBody('The response body.');
223223

@@ -229,7 +229,7 @@ public function testUnserializeError(): void
229229
$mockCache->save($cacheKey, 'Invalid data');
230230

231231
// Check cache with a request with the same URI path.
232-
$pageCache->get($request, new Response(new App()));
232+
$pageCache->get($request, new Response());
233233
}
234234

235235
public function testInvalidCacheError(): void
@@ -243,7 +243,7 @@ public function testInvalidCacheError(): void
243243

244244
$request = $this->createIncomingRequest('foo/bar');
245245

246-
$response = new Response(new App());
246+
$response = new Response();
247247
$response->setHeader('ETag', 'abcd1234');
248248
$response->setBody('The response body.');
249249

@@ -255,6 +255,6 @@ public function testInvalidCacheError(): void
255255
$mockCache->save($cacheKey, serialize(['a' => '1']));
256256

257257
// Check cache with a request with the same URI path.
258-
$pageCache->get($request, new Response(new App()));
258+
$pageCache->get($request, new Response());
259259
}
260260
}

tests/system/CommonFunctionsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public function testOldInput(): void
448448
'zibble' => 'fritz',
449449
]);
450450

451-
$response = new RedirectResponse(new App());
451+
$response = new RedirectResponse();
452452
$response->withInput();
453453

454454
$this->assertSame('bar', old('foo')); // regular parameter
@@ -482,7 +482,7 @@ public function testOldInputSerializeData(): void
482482
'zibble' => serialize('fritz'),
483483
]);
484484

485-
$response = new RedirectResponse(new App());
485+
$response = new RedirectResponse();
486486
$response->withInput();
487487

488488
// serialized parameters are only HTML-escaped.
@@ -522,7 +522,7 @@ public function testOldInputArray(): void
522522
$superglobals->setGetArray([]);
523523
$superglobals->setPostArray(['location' => $locations]);
524524

525-
$response = new RedirectResponse(new App());
525+
$response = new RedirectResponse();
526526
$response->withInput();
527527

528528
$this->assertSame($locations, old('location'));

0 commit comments

Comments
 (0)