Skip to content

Commit 7149fa5

Browse files
committed
refactor: use HTTP\Method constants
1 parent a394ac0 commit 7149fa5

File tree

7 files changed

+16
-13
lines changed

7 files changed

+16
-13
lines changed

system/CodeIgniter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use CodeIgniter\HTTP\DownloadResponse;
2222
use CodeIgniter\HTTP\Exceptions\RedirectException;
2323
use CodeIgniter\HTTP\IncomingRequest;
24+
use CodeIgniter\HTTP\Method;
2425
use CodeIgniter\HTTP\RedirectResponse;
2526
use CodeIgniter\HTTP\Request;
2627
use CodeIgniter\HTTP\ResponsableInterface;
@@ -1027,7 +1028,7 @@ public function storePreviousURL($uri)
10271028
public function spoofRequestMethod()
10281029
{
10291030
// Only works with POSTED forms
1030-
if ($this->request->getMethod() !== 'POST') {
1031+
if ($this->request->getMethod() !== Method::POST) {
10311032
return;
10321033
}
10331034

@@ -1038,7 +1039,7 @@ public function spoofRequestMethod()
10381039
}
10391040

10401041
// Only allows PUT, PATCH, DELETE
1041-
if (in_array($method, ['PUT', 'PATCH', 'DELETE'], true)) {
1042+
if (in_array($method, [Method::PUT, Method::PATCH, Method::DELETE], true)) {
10421043
$this->request = $this->request->setMethod($method);
10431044
}
10441045
}

system/HTTP/CURLRequest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
112112
throw HTTPException::forMissingCurl(); // @codeCoverageIgnore
113113
}
114114

115-
parent::__construct('GET', $uri);
115+
parent::__construct(Method::GET, $uri);
116116

117117
$this->responseOrig = $response ?? new Response(config(App::class));
118118
$this->baseURI = $uri->useRawQueryString();
@@ -177,7 +177,7 @@ protected function resetOptions()
177177
*/
178178
public function get(string $url, array $options = []): ResponseInterface
179179
{
180-
return $this->request('GET', $url, $options);
180+
return $this->request(Method::GET, $url, $options);
181181
}
182182

183183
/**
@@ -217,15 +217,15 @@ public function patch(string $url, array $options = []): ResponseInterface
217217
*/
218218
public function post(string $url, array $options = []): ResponseInterface
219219
{
220-
return $this->request('POST', $url, $options);
220+
return $this->request(Method::POST, $url, $options);
221221
}
222222

223223
/**
224224
* Convenience method for sending a PUT request.
225225
*/
226226
public function put(string $url, array $options = []): ResponseInterface
227227
{
228-
return $this->request('PUT', $url, $options);
228+
return $this->request(Method::PUT, $url, $options);
229229
}
230230

231231
/**
@@ -445,7 +445,7 @@ protected function applyMethod(string $method, array $curlOptions): array
445445
return $this->applyBody($curlOptions);
446446
}
447447

448-
if ($method === 'PUT' || $method === 'POST') {
448+
if ($method === Method::PUT || $method === Method::POST) {
449449
// See http://tools.ietf.org/html/rfc7230#section-3.3.2
450450
if ($this->header('content-length') === null && ! isset($this->config['multipart'])) {
451451
$this->setHeader('Content-Length', '0');

system/HTTP/IncomingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public function is(string $type): bool
404404
{
405405
$valueUpper = strtoupper($type);
406406

407-
$httpMethods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS'];
407+
$httpMethods = Method::all();
408408

409409
if (in_array($valueUpper, $httpMethods, true)) {
410410
return $this->getMethod() === $valueUpper;

system/HTTP/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Request extends OutgoingRequest implements RequestInterface
4141
public function __construct($config = null) // @phpstan-ignore-line
4242
{
4343
if (empty($this->method)) {
44-
$this->method = $this->getServer('REQUEST_METHOD') ?? 'GET';
44+
$this->method = $this->getServer('REQUEST_METHOD') ?? Method::GET;
4545
}
4646

4747
if (empty($this->uri)) {

system/HTTP/ResponseTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
442442
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
443443
&& $this->getProtocolVersion() >= 1.1
444444
) {
445-
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
445+
if ($_SERVER['REQUEST_METHOD'] === Method::GET) {
446446
$code = 302;
447-
} elseif (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'DELETE'], true)) {
447+
} elseif (in_array($_SERVER['REQUEST_METHOD'], [Method::POST, Method::PUT, Method::DELETE], true)) {
448448
// reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
449449
$code = 303;
450450
} else {

system/Security/Security.php

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

1414
use CodeIgniter\Cookie\Cookie;
1515
use CodeIgniter\HTTP\IncomingRequest;
16+
use CodeIgniter\HTTP\Method;
1617
use CodeIgniter\HTTP\Request;
1718
use CodeIgniter\HTTP\RequestInterface;
1819
use CodeIgniter\I18n\Time;
@@ -281,7 +282,7 @@ public function verify(RequestInterface $request)
281282
{
282283
// Protects POST, PUT, DELETE, PATCH
283284
$method = $request->getMethod();
284-
$methodsToProtect = ['POST', 'PUT', 'DELETE', 'PATCH'];
285+
$methodsToProtect = [Method::POST, Method::PUT, Method::DELETE, Method::PATCH];
285286
if (! in_array($method, $methodsToProtect, true)) {
286287
return $this;
287288
}

system/Validation/Validation.php

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

1414
use Closure;
1515
use CodeIgniter\HTTP\IncomingRequest;
16+
use CodeIgniter\HTTP\Method;
1617
use CodeIgniter\HTTP\RequestInterface;
1718
use CodeIgniter\Validation\Exceptions\ValidationException;
1819
use CodeIgniter\View\RendererInterface;
@@ -501,7 +502,7 @@ public function withRequest(RequestInterface $request): ValidationInterface
501502
return $this;
502503
}
503504

504-
if (in_array($request->getMethod(), ['PUT', 'PATCH', 'DELETE'], true)
505+
if (in_array($request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE], true)
505506
&& strpos($request->getHeaderLine('Content-Type'), 'multipart/form-data') === false
506507
) {
507508
$this->data = $request->getRawInput();

0 commit comments

Comments
 (0)