Skip to content

Commit

Permalink
refactor: use HTTP\Method constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 19, 2023
1 parent ba7c488 commit 2c44922
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
5 changes: 3 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\ResponsableInterface;
Expand Down Expand Up @@ -1027,7 +1028,7 @@ public function storePreviousURL($uri)
public function spoofRequestMethod()
{
// Only works with POSTED forms
if ($this->request->getMethod() !== 'POST') {
if ($this->request->getMethod() !== Method::POST) {
return;
}

Expand All @@ -1038,7 +1039,7 @@ public function spoofRequestMethod()
}

// Only allows PUT, PATCH, DELETE
if (in_array($method, ['PUT', 'PATCH', 'DELETE'], true)) {
if (in_array($method, [Method::PUT, Method::PATCH, Method::DELETE], true)) {
$this->request = $this->request->setMethod($method);
}
}
Expand Down
10 changes: 5 additions & 5 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
throw HTTPException::forMissingCurl(); // @codeCoverageIgnore
}

parent::__construct('GET', $uri);
parent::__construct(Method::GET, $uri);

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

/**
Expand Down Expand Up @@ -217,15 +217,15 @@ public function patch(string $url, array $options = []): ResponseInterface
*/
public function post(string $url, array $options = []): ResponseInterface
{
return $this->request('POST', $url, $options);
return $this->request(Method::POST, $url, $options);
}

/**
* Convenience method for sending a PUT request.
*/
public function put(string $url, array $options = []): ResponseInterface
{
return $this->request('PUT', $url, $options);
return $this->request(Method::PUT, $url, $options);
}

/**
Expand Down Expand Up @@ -445,7 +445,7 @@ protected function applyMethod(string $method, array $curlOptions): array
return $this->applyBody($curlOptions);
}

if ($method === 'PUT' || $method === 'POST') {
if ($method === Method::PUT || $method === Method::POST) {
// See http://tools.ietf.org/html/rfc7230#section-3.3.2
if ($this->header('content-length') === null && ! isset($this->config['multipart'])) {
$this->setHeader('Content-Length', '0');
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public function is(string $type): bool
{
$valueUpper = strtoupper($type);

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

if (in_array($valueUpper, $httpMethods, true)) {
return $this->getMethod() === $valueUpper;
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Request extends OutgoingRequest implements RequestInterface
public function __construct($config = null) // @phpstan-ignore-line
{
if (empty($this->method)) {
$this->method = $this->getServer('REQUEST_METHOD') ?? 'GET';
$this->method = $this->getServer('REQUEST_METHOD') ?? Method::GET;
}

if (empty($this->uri)) {
Expand Down
4 changes: 2 additions & 2 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
&& $this->getProtocolVersion() >= 1.1
) {
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if ($_SERVER['REQUEST_METHOD'] === Method::GET) {
$code = 302;
} elseif (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'DELETE'], true)) {
} elseif (in_array($_SERVER['REQUEST_METHOD'], [Method::POST, Method::PUT, Method::DELETE], true)) {
// reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
$code = 303;
} else {
Expand Down
3 changes: 2 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Cookie\Cookie;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\I18n\Time;
Expand Down Expand Up @@ -281,7 +282,7 @@ public function verify(RequestInterface $request)
{
// Protects POST, PUT, DELETE, PATCH
$method = $request->getMethod();
$methodsToProtect = ['POST', 'PUT', 'DELETE', 'PATCH'];
$methodsToProtect = [Method::POST, Method::PUT, Method::DELETE, Method::PATCH];
if (! in_array($method, $methodsToProtect, true)) {
return $this;
}
Expand Down
3 changes: 2 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Closure;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use CodeIgniter\View\RendererInterface;
Expand Down Expand Up @@ -501,7 +502,7 @@ public function withRequest(RequestInterface $request): ValidationInterface
return $this;
}

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

0 comments on commit 2c44922

Please sign in to comment.