Skip to content

Commit

Permalink
HttpMethod enum (phpmyadmin#19420)
Browse files Browse the repository at this point in the history
* HttpMethod

Signed-off-by: Kamil Tekiela <[email protected]>

* Rename to \PhpMyAdmin\Http\RequestMethod

Signed-off-by: Kamil Tekiela <[email protected]>

---------

Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela authored Dec 4, 2024
1 parent f28d82f commit 42310b1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 53 deletions.
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19557,12 +19557,6 @@ parameters:
count: 2
path: src/Utils/HttpRequest.php

-
message: '#^Parameter \#3 \$value of function curl_setopt expects non\-empty\-string\|null, string given\.$#'
identifier: argument.type
count: 1
path: src/Utils/HttpRequest.php

-
message: '#^Variable \$http_response_header in isset\(\) always exists and is not nullable\.$#'
identifier: isset.variable
Expand Down
3 changes: 2 additions & 1 deletion src/Error/ErrorReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\Config;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Http\RequestMethod;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Utils\HttpRequest;
Expand Down Expand Up @@ -208,7 +209,7 @@ public function send(array $report): string|bool|null
{
return $this->httpRequest->create(
$this->submissionUrl,
'POST',
RequestMethod::Post,
false,
json_encode($report),
'Content-Type: application/json',
Expand Down
5 changes: 3 additions & 2 deletions src/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use DateTimeZone;
use DirectoryIterator;
use PhpMyAdmin\Http\RequestMethod;
use PhpMyAdmin\Utils\HttpRequest;
use stdClass;

Expand Down Expand Up @@ -410,7 +411,7 @@ private function isRemoteCommit(mixed $commit, bool &$isRemoteCommit, string $ha
}

$link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
$isFound = $httpRequest->create($link, 'GET');
$isFound = $httpRequest->create($link, RequestMethod::Get);
if ($isFound === false) {
$isRemoteCommit = false;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
Expand Down Expand Up @@ -586,7 +587,7 @@ public function checkGitRevision(): array|null
} else {
$httpRequest = new HttpRequest();
$link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
$isFound = $httpRequest->create($link, 'GET', true);
$isFound = $httpRequest->create($link, RequestMethod::Get, true);
if (is_bool($isFound)) {
$isRemoteBranch = $isFound;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = $isFound;
Expand Down
11 changes: 11 additions & 0 deletions src/Http/RequestMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Http;

enum RequestMethod: string
{
case Get = 'GET';
case Post = 'POST';
}
47 changes: 24 additions & 23 deletions src/Utils/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Composer\CaBundle\CaBundle;
use PhpMyAdmin\Config;
use PhpMyAdmin\Http\RequestMethod;

use function base64_encode;
use function curl_exec;
Expand Down Expand Up @@ -127,15 +128,15 @@ private function response(
/**
* Creates HTTP request using curl
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
* @param string $url Url to send the request
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*/
private function curl(
string $url,
string $method,
RequestMethod $method,
bool $returnOnlyStatus = false,
mixed $content = null,
string $header = '',
Expand All @@ -159,15 +160,15 @@ private function curl(

$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_USERAGENT, 'phpMyAdmin');

if ($method !== 'GET') {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
if ($method !== RequestMethod::Get) {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method->value);
}

if ($header !== '') {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_HTTPHEADER, [$header]);
}

if ($method === 'POST') {
if ($method === RequestMethod::Post) {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
}

Expand Down Expand Up @@ -204,22 +205,22 @@ private function curl(
/**
* Creates HTTP request using file_get_contents
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
* @param string $url Url to send the request
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*/
private function fopen(
string $url,
string $method,
RequestMethod $method,
bool $returnOnlyStatus = false,
mixed $content = null,
string $header = '',
): string|bool|null {
$context = [
'http' => [
'method' => $method,
'method' => $method->value,
'request_fulluri' => true,
'timeout' => 10,
'user_agent' => 'phpMyAdmin',
Expand All @@ -231,7 +232,7 @@ private function fopen(
$context['http']['header'] .= "\n" . $header;
}

if ($method === 'POST') {
if ($method === RequestMethod::Post) {
$context['http']['content'] = $content;
}

Expand Down Expand Up @@ -262,15 +263,15 @@ private function fopen(
/**
* Creates HTTP request
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
* @param string $url Url to send the request
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*/
public function create(
string $url,
string $method,
RequestMethod $method,
bool $returnOnlyStatus = false,
mixed $content = null,
string $header = '',
Expand Down
3 changes: 2 additions & 1 deletion src/VersionInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Http\RequestMethod;
use PhpMyAdmin\Utils\HttpRequest;

use function count;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function getLatestVersions(): array|null
$save = true;
$file = 'https://www.phpmyadmin.net/home_page/version.json';
$httpRequest = new HttpRequest();
$response = $httpRequest->create($file, 'GET');
$response = $httpRequest->create($file, RequestMethod::Get);
}

$response = $response ?: '{}';
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Error/ErrorReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Error\Error;
use PhpMyAdmin\Error\ErrorReport;
use PhpMyAdmin\Http\RequestMethod;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\DbiDummy;
Expand Down Expand Up @@ -124,7 +125,7 @@ public function testSend(): void
->method('create')
->with(
$submissionUrl,
'POST',
RequestMethod::Post,
false,
json_encode($report),
'Content-Type: application/json',
Expand Down
51 changes: 32 additions & 19 deletions tests/unit/Utils/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Tests\Utils;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Http\RequestMethod;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Utils\HttpRequest;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -61,15 +62,19 @@ private function checkCurlSslFlagsSupport(): void
* Test for http request using Curl
*
* @param string $url url
* @param string $method method
* @param RequestMethod $method method
* @param bool $returnOnlyStatus return only status
* @param bool|string|null $expected expected result
*/
#[DataProvider('httpRequests')]
#[Group('network')]
#[RequiresPhpExtension('curl')]
public function testCurl(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
{
public function testCurl(
string $url,
RequestMethod $method,
bool $returnOnlyStatus,
bool|string|null $expected,
): void {
$result = $this->callFunction(
$this->httpRequest,
HttpRequest::class,
Expand All @@ -83,7 +88,7 @@ public function testCurl(string $url, string $method, bool $returnOnlyStatus, bo
* Test for http request using Curl with CURLOPT_CAPATH
*
* @param string $url url
* @param string $method method
* @param RequestMethod $method method
* @param bool $returnOnlyStatus return only status
* @param bool|string|null $expected expected result
*/
Expand All @@ -92,7 +97,7 @@ public function testCurl(string $url, string $method, bool $returnOnlyStatus, bo
#[RequiresPhpExtension('curl')]
public function testCurlCAPath(
string $url,
string $method,
RequestMethod $method,
bool $returnOnlyStatus,
bool|string|null $expected,
): void {
Expand All @@ -112,7 +117,7 @@ public function testCurlCAPath(
* Test for http request using Curl with CURLOPT_CAINFO
*
* @param string $url url
* @param string $method method
* @param RequestMethod $method method
* @param bool $returnOnlyStatus return only status
* @param bool|string|null $expected expected result
*/
Expand All @@ -121,7 +126,7 @@ public function testCurlCAPath(
#[RequiresPhpExtension('curl')]
public function testCurlCAInfo(
string $url,
string $method,
RequestMethod $method,
bool $returnOnlyStatus,
bool|string|null $expected,
): void {
Expand All @@ -141,14 +146,18 @@ public function testCurlCAInfo(
* Test for http request using fopen
*
* @param string $url url
* @param string $method method
* @param RequestMethod $method method
* @param bool $returnOnlyStatus return only status
* @param bool|string|null $expected expected result
*/
#[DataProvider('httpRequests')]
#[Group('network')]
public function testFopen(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
{
public function testFopen(
string $url,
RequestMethod $method,
bool $returnOnlyStatus,
bool|string|null $expected,
): void {
if (! ini_get('allow_url_fopen')) {
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
}
Expand All @@ -166,15 +175,19 @@ public function testFopen(string $url, string $method, bool $returnOnlyStatus, b
* Test for http request using generic interface
*
* @param string $url url
* @param string $method method
* @param RequestMethod $method method
* @param bool $returnOnlyStatus return only status
* @param bool|string|null $expected expected result
*/
#[DataProvider('httpRequests')]
#[Group('network')]
#[RequiresPhpExtension('curl')]
public function testCreate(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
{
public function testCreate(
string $url,
RequestMethod $method,
bool $returnOnlyStatus,
bool|string|null $expected,
): void {
if (! ini_get('allow_url_fopen')) {
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
}
Expand Down Expand Up @@ -206,16 +219,16 @@ private function validateHttp(mixed $result, mixed $expected): void
/**
* Data provider for HTTP tests
*
* @return mixed[][]
* @return list<array{string, RequestMethod, bool, bool|string|null}>
*/
public static function httpRequests(): array
{
return [
['https://www.phpmyadmin.net/test/data', 'GET', true, true],
['https://www.phpmyadmin.net/test/data', 'POST', true, null],
['https://nonexisting.phpmyadmin.net/test/data', 'GET', true, null],
['https://www.phpmyadmin.net/test/data', 'GET', false, 'TEST DATA'],
['https://www.phpmyadmin.net/test/nothing', 'GET', true, false],
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, true, true],
['https://www.phpmyadmin.net/test/data', RequestMethod::Post, true, null],
['https://nonexisting.phpmyadmin.net/test/data', RequestMethod::Get, true, null],
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, false, 'TEST DATA'],
['https://www.phpmyadmin.net/test/nothing', RequestMethod::Get, true, false],
];
}
}

0 comments on commit 42310b1

Please sign in to comment.