Skip to content

Commit 42310b1

Browse files
HttpMethod enum (#19420)
* HttpMethod Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> * Rename to \PhpMyAdmin\Http\RequestMethod Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> --------- Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent f28d82f commit 42310b1

8 files changed

Lines changed: 76 additions & 53 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19557,12 +19557,6 @@ parameters:
1955719557
count: 2
1955819558
path: src/Utils/HttpRequest.php
1955919559

19560-
-
19561-
message: '#^Parameter \#3 \$value of function curl_setopt expects non\-empty\-string\|null, string given\.$#'
19562-
identifier: argument.type
19563-
count: 1
19564-
path: src/Utils/HttpRequest.php
19565-
1956619560
-
1956719561
message: '#^Variable \$http_response_header in isset\(\) always exists and is not nullable\.$#'
1956819562
identifier: isset.variable

src/Error/ErrorReport.php

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

77
use PhpMyAdmin\Config;
88
use PhpMyAdmin\ConfigStorage\Relation;
9+
use PhpMyAdmin\Http\RequestMethod;
910
use PhpMyAdmin\Template;
1011
use PhpMyAdmin\Url;
1112
use PhpMyAdmin\Utils\HttpRequest;
@@ -208,7 +209,7 @@ public function send(array $report): string|bool|null
208209
{
209210
return $this->httpRequest->create(
210211
$this->submissionUrl,
211-
'POST',
212+
RequestMethod::Post,
212213
false,
213214
json_encode($report),
214215
'Content-Type: application/json',

src/Git.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTimeImmutable;
88
use DateTimeZone;
99
use DirectoryIterator;
10+
use PhpMyAdmin\Http\RequestMethod;
1011
use PhpMyAdmin\Utils\HttpRequest;
1112
use stdClass;
1213

@@ -410,7 +411,7 @@ private function isRemoteCommit(mixed $commit, bool &$isRemoteCommit, string $ha
410411
}
411412

412413
$link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
413-
$isFound = $httpRequest->create($link, 'GET');
414+
$isFound = $httpRequest->create($link, RequestMethod::Get);
414415
if ($isFound === false) {
415416
$isRemoteCommit = false;
416417
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
@@ -586,7 +587,7 @@ public function checkGitRevision(): array|null
586587
} else {
587588
$httpRequest = new HttpRequest();
588589
$link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
589-
$isFound = $httpRequest->create($link, 'GET', true);
590+
$isFound = $httpRequest->create($link, RequestMethod::Get, true);
590591
if (is_bool($isFound)) {
591592
$isRemoteBranch = $isFound;
592593
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = $isFound;

src/Http/RequestMethod.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Http;
6+
7+
enum RequestMethod: string
8+
{
9+
case Get = 'GET';
10+
case Post = 'POST';
11+
}

src/Utils/HttpRequest.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Composer\CaBundle\CaBundle;
88
use PhpMyAdmin\Config;
9+
use PhpMyAdmin\Http\RequestMethod;
910

1011
use function base64_encode;
1112
use function curl_exec;
@@ -127,15 +128,15 @@ private function response(
127128
/**
128129
* Creates HTTP request using curl
129130
*
130-
* @param string $url Url to send the request
131-
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
132-
* @param bool $returnOnlyStatus If set to true, the method would only return response status
133-
* @param mixed $content Content to be sent with HTTP request
134-
* @param string $header Header to be set for the HTTP request
131+
* @param string $url Url to send the request
132+
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
133+
* @param bool $returnOnlyStatus If set to true, the method would only return response status
134+
* @param mixed $content Content to be sent with HTTP request
135+
* @param string $header Header to be set for the HTTP request
135136
*/
136137
private function curl(
137138
string $url,
138-
string $method,
139+
RequestMethod $method,
139140
bool $returnOnlyStatus = false,
140141
mixed $content = null,
141142
string $header = '',
@@ -159,15 +160,15 @@ private function curl(
159160

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

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

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

170-
if ($method === 'POST') {
171+
if ($method === RequestMethod::Post) {
171172
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
172173
}
173174

@@ -204,22 +205,22 @@ private function curl(
204205
/**
205206
* Creates HTTP request using file_get_contents
206207
*
207-
* @param string $url Url to send the request
208-
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
209-
* @param bool $returnOnlyStatus If set to true, the method would only return response status
210-
* @param mixed $content Content to be sent with HTTP request
211-
* @param string $header Header to be set for the HTTP request
208+
* @param string $url Url to send the request
209+
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
210+
* @param bool $returnOnlyStatus If set to true, the method would only return response status
211+
* @param mixed $content Content to be sent with HTTP request
212+
* @param string $header Header to be set for the HTTP request
212213
*/
213214
private function fopen(
214215
string $url,
215-
string $method,
216+
RequestMethod $method,
216217
bool $returnOnlyStatus = false,
217218
mixed $content = null,
218219
string $header = '',
219220
): string|bool|null {
220221
$context = [
221222
'http' => [
222-
'method' => $method,
223+
'method' => $method->value,
223224
'request_fulluri' => true,
224225
'timeout' => 10,
225226
'user_agent' => 'phpMyAdmin',
@@ -231,7 +232,7 @@ private function fopen(
231232
$context['http']['header'] .= "\n" . $header;
232233
}
233234

234-
if ($method === 'POST') {
235+
if ($method === RequestMethod::Post) {
235236
$context['http']['content'] = $content;
236237
}
237238

@@ -262,15 +263,15 @@ private function fopen(
262263
/**
263264
* Creates HTTP request
264265
*
265-
* @param string $url Url to send the request
266-
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
267-
* @param bool $returnOnlyStatus If set to true, the method would only return response status
268-
* @param mixed $content Content to be sent with HTTP request
269-
* @param string $header Header to be set for the HTTP request
266+
* @param string $url Url to send the request
267+
* @param RequestMethod $method HTTP request method (GET, POST, PUT, DELETE, etc)
268+
* @param bool $returnOnlyStatus If set to true, the method would only return response status
269+
* @param mixed $content Content to be sent with HTTP request
270+
* @param string $header Header to be set for the HTTP request
270271
*/
271272
public function create(
272273
string $url,
273-
string $method,
274+
RequestMethod $method,
274275
bool $returnOnlyStatus = false,
275276
mixed $content = null,
276277
string $header = '',

src/VersionInformation.php

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

88
namespace PhpMyAdmin;
99

10+
use PhpMyAdmin\Http\RequestMethod;
1011
use PhpMyAdmin\Utils\HttpRequest;
1112

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

5859
$response = $response ?: '{}';

tests/unit/Error/ErrorReportTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpMyAdmin\DatabaseInterface;
1010
use PhpMyAdmin\Error\Error;
1111
use PhpMyAdmin\Error\ErrorReport;
12+
use PhpMyAdmin\Http\RequestMethod;
1213
use PhpMyAdmin\Template;
1314
use PhpMyAdmin\Tests\AbstractTestCase;
1415
use PhpMyAdmin\Tests\Stubs\DbiDummy;
@@ -124,7 +125,7 @@ public function testSend(): void
124125
->method('create')
125126
->with(
126127
$submissionUrl,
127-
'POST',
128+
RequestMethod::Post,
128129
false,
129130
json_encode($report),
130131
'Content-Type: application/json',

tests/unit/Utils/HttpRequestTest.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\Tests\Utils;
66

77
use PhpMyAdmin\DatabaseInterface;
8+
use PhpMyAdmin\Http\RequestMethod;
89
use PhpMyAdmin\Tests\AbstractTestCase;
910
use PhpMyAdmin\Utils\HttpRequest;
1011
use PHPUnit\Framework\Attributes\CoversClass;
@@ -61,15 +62,19 @@ private function checkCurlSslFlagsSupport(): void
6162
* Test for http request using Curl
6263
*
6364
* @param string $url url
64-
* @param string $method method
65+
* @param RequestMethod $method method
6566
* @param bool $returnOnlyStatus return only status
6667
* @param bool|string|null $expected expected result
6768
*/
6869
#[DataProvider('httpRequests')]
6970
#[Group('network')]
7071
#[RequiresPhpExtension('curl')]
71-
public function testCurl(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
72-
{
72+
public function testCurl(
73+
string $url,
74+
RequestMethod $method,
75+
bool $returnOnlyStatus,
76+
bool|string|null $expected,
77+
): void {
7378
$result = $this->callFunction(
7479
$this->httpRequest,
7580
HttpRequest::class,
@@ -83,7 +88,7 @@ public function testCurl(string $url, string $method, bool $returnOnlyStatus, bo
8388
* Test for http request using Curl with CURLOPT_CAPATH
8489
*
8590
* @param string $url url
86-
* @param string $method method
91+
* @param RequestMethod $method method
8792
* @param bool $returnOnlyStatus return only status
8893
* @param bool|string|null $expected expected result
8994
*/
@@ -92,7 +97,7 @@ public function testCurl(string $url, string $method, bool $returnOnlyStatus, bo
9297
#[RequiresPhpExtension('curl')]
9398
public function testCurlCAPath(
9499
string $url,
95-
string $method,
100+
RequestMethod $method,
96101
bool $returnOnlyStatus,
97102
bool|string|null $expected,
98103
): void {
@@ -112,7 +117,7 @@ public function testCurlCAPath(
112117
* Test for http request using Curl with CURLOPT_CAINFO
113118
*
114119
* @param string $url url
115-
* @param string $method method
120+
* @param RequestMethod $method method
116121
* @param bool $returnOnlyStatus return only status
117122
* @param bool|string|null $expected expected result
118123
*/
@@ -121,7 +126,7 @@ public function testCurlCAPath(
121126
#[RequiresPhpExtension('curl')]
122127
public function testCurlCAInfo(
123128
string $url,
124-
string $method,
129+
RequestMethod $method,
125130
bool $returnOnlyStatus,
126131
bool|string|null $expected,
127132
): void {
@@ -141,14 +146,18 @@ public function testCurlCAInfo(
141146
* Test for http request using fopen
142147
*
143148
* @param string $url url
144-
* @param string $method method
149+
* @param RequestMethod $method method
145150
* @param bool $returnOnlyStatus return only status
146151
* @param bool|string|null $expected expected result
147152
*/
148153
#[DataProvider('httpRequests')]
149154
#[Group('network')]
150-
public function testFopen(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
151-
{
155+
public function testFopen(
156+
string $url,
157+
RequestMethod $method,
158+
bool $returnOnlyStatus,
159+
bool|string|null $expected,
160+
): void {
152161
if (! ini_get('allow_url_fopen')) {
153162
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
154163
}
@@ -166,15 +175,19 @@ public function testFopen(string $url, string $method, bool $returnOnlyStatus, b
166175
* Test for http request using generic interface
167176
*
168177
* @param string $url url
169-
* @param string $method method
178+
* @param RequestMethod $method method
170179
* @param bool $returnOnlyStatus return only status
171180
* @param bool|string|null $expected expected result
172181
*/
173182
#[DataProvider('httpRequests')]
174183
#[Group('network')]
175184
#[RequiresPhpExtension('curl')]
176-
public function testCreate(string $url, string $method, bool $returnOnlyStatus, bool|string|null $expected): void
177-
{
185+
public function testCreate(
186+
string $url,
187+
RequestMethod $method,
188+
bool $returnOnlyStatus,
189+
bool|string|null $expected,
190+
): void {
178191
if (! ini_get('allow_url_fopen')) {
179192
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
180193
}
@@ -206,16 +219,16 @@ private function validateHttp(mixed $result, mixed $expected): void
206219
/**
207220
* Data provider for HTTP tests
208221
*
209-
* @return mixed[][]
222+
* @return list<array{string, RequestMethod, bool, bool|string|null}>
210223
*/
211224
public static function httpRequests(): array
212225
{
213226
return [
214-
['https://www.phpmyadmin.net/test/data', 'GET', true, true],
215-
['https://www.phpmyadmin.net/test/data', 'POST', true, null],
216-
['https://nonexisting.phpmyadmin.net/test/data', 'GET', true, null],
217-
['https://www.phpmyadmin.net/test/data', 'GET', false, 'TEST DATA'],
218-
['https://www.phpmyadmin.net/test/nothing', 'GET', true, false],
227+
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, true, true],
228+
['https://www.phpmyadmin.net/test/data', RequestMethod::Post, true, null],
229+
['https://nonexisting.phpmyadmin.net/test/data', RequestMethod::Get, true, null],
230+
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, false, 'TEST DATA'],
231+
['https://www.phpmyadmin.net/test/nothing', RequestMethod::Get, true, false],
219232
];
220233
}
221234
}

0 commit comments

Comments
 (0)