Skip to content

Commit dafeec6

Browse files
committed
wip: refactor HTTP deprecations
1 parent 1172c8c commit dafeec6

21 files changed

+115
-604
lines changed

system/CodeIgniter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ public function spoofRequestMethod()
822822

823823
// Only allows PUT, PATCH, DELETE
824824
if (in_array($method, [Method::PUT, Method::PATCH, Method::DELETE], true)) {
825-
$this->request = $this->request->setMethod($method);
825+
$this->request = $this->request->withMethod($method);
826826
}
827827
}
828828

system/Commands/Utilities/Routes/FilterCollector.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public function get(string $method, string $uri): array
5151
return ['before' => [], 'after' => []];
5252
}
5353

54-
$request = service('incomingrequest', null, false);
55-
$request->setMethod($method);
54+
$request = single_service('incomingrequest', null)->withMethod($method);
5655

5756
$router = $this->createRouter($request);
5857
$filters = $this->createFilters($request);
@@ -77,8 +76,7 @@ public function getClasses(string $method, string $uri): array
7776
return ['before' => [], 'after' => []];
7877
}
7978

80-
$request = service('incomingrequest', null, false);
81-
$request->setMethod($method);
79+
$request = single_service('incomingrequest', null)->withMethod($method);
8280

8381
$router = $this->createRouter($request);
8482
$filters = $this->createFilters($request);
@@ -95,8 +93,7 @@ public function getClasses(string $method, string $uri): array
9593
*/
9694
public function getRequiredFilters(): array
9795
{
98-
$request = service('incomingrequest', null, false);
99-
$request->setMethod(Method::GET);
96+
$request = single_service('incomingrequest', null)->withMethod(Method::GET);
10097

10198
$router = $this->createRouter($request);
10299
$filters = $this->createFilters($request);
@@ -113,8 +110,7 @@ public function getRequiredFilters(): array
113110
*/
114111
public function getRequiredFilterClasses(): array
115112
{
116-
$request = service('incomingrequest', null, false);
117-
$request->setMethod(Method::GET);
113+
$request = single_service('incomingrequest', null)->withMethod(Method::GET);
118114

119115
$router = $this->createRouter($request);
120116
$filters = $this->createFilters($request);

system/HTTP/CURLRequest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16+
use Closure;
1617
use CodeIgniter\Exceptions\InvalidArgumentException;
1718
use CodeIgniter\HTTP\Exceptions\HTTPException;
1819
use Config\App;
@@ -313,7 +314,11 @@ public function setJSON($data)
313314
protected function parseOptions(array $options)
314315
{
315316
if (array_key_exists('baseURI', $options)) {
316-
$this->baseURI = $this->baseURI->setURI($options['baseURI']);
317+
$this->baseURI = Closure::bind(
318+
static fn (URI $uri): URI => $uri->setUri($options['baseURI']),
319+
null,
320+
URI::class,
321+
)($this->baseURI);
317322
unset($options['baseURI']);
318323
}
319324

system/HTTP/Message.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,6 @@ public function getBody()
6060
return $this->body;
6161
}
6262

63-
/**
64-
* Returns an array containing all headers.
65-
*
66-
* @return array<string, Header> An array of the request headers
67-
*
68-
* @deprecated Use Message::headers() to make room for PSR-7
69-
*
70-
* @TODO Incompatible return value with PSR-7
71-
*
72-
* @codeCoverageIgnore
73-
*/
74-
public function getHeaders(): array
75-
{
76-
return $this->headers();
77-
}
78-
79-
/**
80-
* Returns a single header object. If multiple headers with the same
81-
* name exist, then will return an array of header objects.
82-
*
83-
* @return array|Header|null
84-
*
85-
* @deprecated Use Message::header() to make room for PSR-7
86-
*
87-
* @TODO Incompatible return value with PSR-7
88-
*
89-
* @codeCoverageIgnore
90-
*/
91-
public function getHeader(string $name)
92-
{
93-
return $this->header($name);
94-
}
95-
9663
/**
9764
* Determines whether a header exists.
9865
*/

system/HTTP/OutgoingRequest.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ public function getMethod(): string
7777
return $this->method;
7878
}
7979

80-
/**
81-
* Sets the request method. Used when spoofing the request.
82-
*
83-
* @return $this
84-
*
85-
* @deprecated Use withMethod() instead for immutability
86-
*/
87-
public function setMethod(string $method)
88-
{
89-
$this->method = $method;
90-
91-
return $this;
92-
}
93-
9480
/**
9581
* Returns an instance with the specified method.
9682
*
@@ -100,7 +86,8 @@ public function setMethod(string $method)
10086
*/
10187
public function withMethod($method)
10288
{
103-
$request = clone $this;
89+
$request = clone $this;
90+
10491
$request->method = $method;
10592

10693
return $request;

system/HTTP/Request.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,6 @@ public function __construct($config = null)
4242
}
4343
}
4444

45-
/**
46-
* Sets the request method. Used when spoofing the request.
47-
*
48-
* @return $this
49-
*
50-
* @deprecated 4.0.5 Use withMethod() instead for immutability
51-
*
52-
* @codeCoverageIgnore
53-
*/
54-
public function setMethod(string $method)
55-
{
56-
$this->method = $method;
57-
58-
return $this;
59-
}
60-
6145
/**
6246
* Returns an instance with the specified method.
6347
*

system/HTTP/RequestTrait.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,6 @@ public function getServer($index = null, $filter = null, $flags = null)
206206
return $this->fetchGlobal('server', $index, $filter, $flags);
207207
}
208208

209-
/**
210-
* Fetch an item from the $_ENV array.
211-
*
212-
* @param array|string|null $index Index for item to be fetched from $_ENV
213-
* @param int|null $filter A filter name to be applied
214-
* @param array|int|null $flags
215-
*
216-
* @return mixed
217-
*
218-
* @deprecated 4.4.4 This method does not work from the beginning. Use `env()`.
219-
*/
220-
public function getEnv($index = null, $filter = null, $flags = null)
221-
{
222-
// @phpstan-ignore-next-line
223-
return $this->fetchGlobal('env', $index, $filter, $flags);
224-
}
225-
226209
/**
227210
* Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
228211
*

system/HTTP/SiteURI.php

Lines changed: 13 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16-
use CodeIgniter\Exceptions\BadMethodCallException;
1716
use CodeIgniter\Exceptions\ConfigException;
1817
use CodeIgniter\HTTP\Exceptions\HTTPException;
1918
use Config\App;
@@ -43,37 +42,6 @@ class SiteURI extends URI
4342
*/
4443
private readonly string $indexPage;
4544

46-
/**
47-
* List of URI segments in baseURL and indexPage.
48-
*
49-
* If the URI is "http://localhost:8888/ci431/public/index.php/test?a=b",
50-
* and the baseURL is "http://localhost:8888/ci431/public/", then:
51-
* $baseSegments = [
52-
* 0 => 'ci431',
53-
* 1 => 'public',
54-
* 2 => 'index.php',
55-
* ];
56-
*/
57-
private array $baseSegments;
58-
59-
/**
60-
* List of URI segments after indexPage.
61-
*
62-
* The word "URI Segments" originally means only the URI path part relative
63-
* to the baseURL.
64-
*
65-
* If the URI is "http://localhost:8888/ci431/public/index.php/test?a=b",
66-
* and the baseURL is "http://localhost:8888/ci431/public/", then:
67-
* $segments = [
68-
* 0 => 'test',
69-
* ];
70-
*
71-
* @var array<int, string>
72-
*
73-
* @deprecated This property will be private.
74-
*/
75-
protected $segments;
76-
7745
/**
7846
* URI path relative to baseURL.
7947
*
@@ -147,16 +115,14 @@ private function determineBaseURL(
147115

148116
$uri = new URI($baseURL);
149117

150-
// Update scheme
151118
if ($scheme !== null && $scheme !== '') {
152-
$uri->setScheme($scheme);
119+
$uri = $uri->withScheme($scheme);
153120
} elseif ($configApp->forceGlobalSecureRequests) {
154-
$uri->setScheme('https');
121+
$uri = $uri->withScheme('https');
155122
}
156123

157-
// Update host
158124
if ($host !== null) {
159-
$uri->setHost($host);
125+
$uri = $uri->setHost($host);
160126
}
161127

162128
return $uri;
@@ -211,34 +177,12 @@ private function normalizeBaseURL(App $configApp): string
211177
private function setBasePath(): void
212178
{
213179
$this->basePathWithoutIndexPage = $this->baseURL->getPath();
214-
215-
$this->baseSegments = $this->convertToSegments($this->basePathWithoutIndexPage);
216-
217-
if ($this->indexPage !== '') {
218-
$this->baseSegments[] = $this->indexPage;
219-
}
220-
}
221-
222-
/**
223-
* @deprecated
224-
*/
225-
public function setBaseURL(string $baseURL): void
226-
{
227-
throw new BadMethodCallException('Cannot use this method.');
228-
}
229-
230-
/**
231-
* @deprecated
232-
*/
233-
public function setURI(?string $uri = null)
234-
{
235-
throw new BadMethodCallException('Cannot use this method.');
236180
}
237181

238182
/**
239183
* Returns the baseURL.
240184
*
241-
* @interal
185+
* @internal
242186
*/
243187
public function getBaseURL(): string
244188
{
@@ -307,39 +251,18 @@ private function convertToSegments(string $path): array
307251
return ($tempPath === '') ? [] : explode('/', $tempPath);
308252
}
309253

310-
/**
311-
* Sets the path portion of the URI based on segments.
312-
*
313-
* @return $this
314-
*
315-
* @deprecated This method will be private.
316-
*/
317-
public function refreshPath()
318-
{
319-
$allSegments = array_merge($this->baseSegments, $this->segments);
320-
$this->path = '/' . $this->filterPath(implode('/', $allSegments));
321-
322-
if ($this->routePath === '/' && $this->path !== '/') {
323-
$this->path .= '/';
324-
}
325-
326-
$this->routePath = $this->filterPath(implode('/', $this->segments));
327-
328-
return $this;
329-
}
330-
331254
/**
332255
* Saves our parts from a parse_url() call.
333256
*
334257
* @param array{
335-
* host?: string,
336-
* user?: string,
337-
* path?: string,
338-
* query?: string,
339-
* fragment?: string,
340-
* scheme?: string,
341-
* port?: int,
342-
* pass?: string,
258+
* host?: string,
259+
* user?: string,
260+
* path?: string,
261+
* query?: string,
262+
* fragment?: string,
263+
* scheme?: string,
264+
* port?: int,
265+
* pass?: string,
343266
* } $parts
344267
*/
345268
protected function applyParts(array $parts): void
@@ -364,11 +287,7 @@ protected function applyParts(array $parts): void
364287
$this->fragment = $parts['fragment'];
365288
}
366289

367-
if (isset($parts['scheme'])) {
368-
$this->setScheme(rtrim($parts['scheme'], ':/'));
369-
} else {
370-
$this->setScheme('http');
371-
}
290+
$this->scheme = $this->withScheme(rtrim($parts['scheme'] ?? 'http', ':/'))->getScheme();
372291

373292
if (isset($parts['port'])) {
374293
// Valid port numbers are enforced by earlier parse_url or setPort()

0 commit comments

Comments
 (0)