Skip to content

Commit b2d9b93

Browse files
Merge pull request #18743 from dardvas/refactor-17769
Replaced superglobals with ServerRequest in ChangeRowsController and FindReplaceController
2 parents f0e7934 + a1b5727 commit b2d9b93

File tree

5 files changed

+40
-86
lines changed

5 files changed

+40
-86
lines changed

phpstan-baseline.neon

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5837,7 +5837,12 @@ parameters:
58375837

58385838
-
58395839
message: "#^Cannot cast mixed to int\\.$#"
5840-
count: 2
5840+
count: 1
5841+
path: src/Controllers/Table/FindReplaceController.php
5842+
5843+
-
5844+
message: "#^Cannot cast mixed to string\\.$#"
5845+
count: 3
58415846
path: src/Controllers/Table/FindReplaceController.php
58425847

58435848
-
@@ -5865,31 +5870,11 @@ parameters:
58655870
count: 2
58665871
path: src/Controllers/Table/FindReplaceController.php
58675872

5868-
-
5869-
message: "#^Parameter \\#2 \\$find of method PhpMyAdmin\\\\Controllers\\\\Table\\\\FindReplaceController\\:\\:getReplacePreview\\(\\) expects string, mixed given\\.$#"
5870-
count: 1
5871-
path: src/Controllers/Table/FindReplaceController.php
5872-
5873-
-
5874-
message: "#^Parameter \\#2 \\$find of method PhpMyAdmin\\\\Controllers\\\\Table\\\\FindReplaceController\\:\\:replace\\(\\) expects string, mixed given\\.$#"
5875-
count: 1
5876-
path: src/Controllers/Table/FindReplaceController.php
5877-
58785873
-
58795874
message: "#^Parameter \\#2 \\$table of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getColumns\\(\\) expects string, mixed given\\.$#"
58805875
count: 1
58815876
path: src/Controllers/Table/FindReplaceController.php
58825877

5883-
-
5884-
message: "#^Parameter \\#3 \\$replaceWith of method PhpMyAdmin\\\\Controllers\\\\Table\\\\FindReplaceController\\:\\:getReplacePreview\\(\\) expects string, mixed given\\.$#"
5885-
count: 1
5886-
path: src/Controllers/Table/FindReplaceController.php
5887-
5888-
-
5889-
message: "#^Parameter \\#3 \\$replaceWith of method PhpMyAdmin\\\\Controllers\\\\Table\\\\FindReplaceController\\:\\:replace\\(\\) expects string, mixed given\\.$#"
5890-
count: 1
5891-
path: src/Controllers/Table/FindReplaceController.php
5892-
58935878
-
58945879
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, mixed given\\.$#"
58955880
count: 2

psalm-baseline.xml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,6 +3327,7 @@
33273327
<MixedAssignment>
33283328
<code><![CDATA[$GLOBALS['active_page']]]></code>
33293329
<code><![CDATA[$GLOBALS['where_clause']]]></code>
3330+
<code>$rowsToDelete</code>
33303331
</MixedAssignment>
33313332
</file>
33323333
<file src="src/Controllers/Table/ChartController.php">
@@ -3536,22 +3537,6 @@
35363537
<code>$row</code>
35373538
<code>$row</code>
35383539
</MixedAssignment>
3539-
<PossiblyInvalidArgument>
3540-
<code><![CDATA[$_POST['find']]]></code>
3541-
<code><![CDATA[$_POST['findString']]]></code>
3542-
<code><![CDATA[$_POST['replaceWith']]]></code>
3543-
<code><![CDATA[$_POST['replaceWith']]]></code>
3544-
</PossiblyInvalidArgument>
3545-
<PossiblyInvalidCast>
3546-
<code><![CDATA[$_POST['find']]]></code>
3547-
<code><![CDATA[$_POST['findString']]]></code>
3548-
<code><![CDATA[$_POST['replaceWith']]]></code>
3549-
<code><![CDATA[$_POST['replaceWith']]]></code>
3550-
</PossiblyInvalidCast>
3551-
<RiskyCast>
3552-
<code><![CDATA[$_POST['columnIndex']]]></code>
3553-
<code><![CDATA[$_POST['columnIndex']]]></code>
3554-
</RiskyCast>
35553540
</file>
35563541
<file src="src/Controllers/Table/GetFieldController.php">
35573542
<PossiblyNullArgument>

src/Controllers/Table/ChangeRowsController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public function __invoke(ServerRequest $request): void
2929
$GLOBALS['active_page'] ??= null;
3030
$GLOBALS['where_clause'] ??= null;
3131

32-
if (isset($_POST['goto']) && (! isset($_POST['rows_to_delete']) || ! is_array($_POST['rows_to_delete']))) {
32+
$rowsToDelete = $request->getParsedBodyParam('rows_to_delete');
33+
34+
if (
35+
(! $request->hasBodyParam('rows_to_delete') || ! is_array($rowsToDelete))
36+
&& $request->hasBodyParam('goto')
37+
) {
3338
$this->response->setRequestStatus(false);
3439
$this->response->addJSON('message', __('No row selected.'));
3540

@@ -41,8 +46,8 @@ public function __invoke(ServerRequest $request): void
4146
// indicating WHERE clause. Then we build the array which is used
4247
// for the /table/change script.
4348
$GLOBALS['where_clause'] = [];
44-
if (isset($_POST['rows_to_delete']) && is_array($_POST['rows_to_delete'])) {
45-
$GLOBALS['where_clause'] = array_values($_POST['rows_to_delete']);
49+
if (is_array($rowsToDelete)) {
50+
$GLOBALS['where_clause'] = array_values($rowsToDelete);
4651
}
4752

4853
$GLOBALS['active_page'] = Url::getFromRoute('/table/change');

src/Controllers/Table/FindReplaceController.php

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use PhpMyAdmin\Util;
2020

2121
use function __;
22-
use function array_key_exists;
2322
use function is_array;
2423
use function mb_strtolower;
2524
use function preg_match;
@@ -99,16 +98,30 @@ public function __invoke(ServerRequest $request): void
9998
return;
10099
}
101100

102-
if (isset($_POST['find'])) {
103-
$this->findAction();
101+
$useRegex = (bool) $request->getParsedBodyParam('useRegex');
102+
$replaceWith = (string) $request->getParsedBodyParam('replaceWith');
103+
$columnIndex = (int) $request->getParsedBodyParam('columnIndex');
104+
105+
if ($request->hasBodyParam('find')) {
106+
$find = (string) $request->getParsedBodyParam('find');
107+
$preview = $this->getReplacePreview($columnIndex, $find, $replaceWith, $useRegex, $this->connectionCharSet);
108+
$this->response->addJSON('preview', $preview);
104109

105110
return;
106111
}
107112

108113
$this->addScriptFiles(['table/find_replace.js']);
109114

110-
if (isset($_POST['replace'])) {
111-
$this->replaceAction();
115+
if ($request->hasBodyParam('replace')) {
116+
$findString = (string) $request->getParsedBodyParam('findString');
117+
$this->replace($columnIndex, $findString, $replaceWith, $useRegex, $this->connectionCharSet);
118+
$this->response->addHTML(
119+
Generator::getMessage(
120+
__('Your SQL query has been executed successfully.'),
121+
null,
122+
'success',
123+
),
124+
);
112125
}
113126

114127
// Displays the find and replace form
@@ -178,39 +191,6 @@ public function displaySelectionFormAction(): void
178191
]);
179192
}
180193

181-
public function findAction(): void
182-
{
183-
$useRegex = array_key_exists('useRegex', $_POST)
184-
&& $_POST['useRegex'] === 'on';
185-
186-
$preview = $this->getReplacePreview(
187-
(int) $_POST['columnIndex'],
188-
$_POST['find'],
189-
$_POST['replaceWith'],
190-
$useRegex,
191-
$this->connectionCharSet,
192-
);
193-
$this->response->addJSON('preview', $preview);
194-
}
195-
196-
public function replaceAction(): void
197-
{
198-
$this->replace(
199-
(int) $_POST['columnIndex'],
200-
$_POST['findString'],
201-
$_POST['replaceWith'],
202-
(bool) $_POST['useRegex'],
203-
$this->connectionCharSet,
204-
);
205-
$this->response->addHTML(
206-
Generator::getMessage(
207-
__('Your SQL query has been executed successfully.'),
208-
null,
209-
'success',
210-
),
211-
);
212-
}
213-
214194
/**
215195
* Returns HTML for previewing strings found and their replacements
216196
*

test/classes/Controllers/Table/ChangeRowsControllerTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpMyAdmin\Controllers\Table\ChangeController;
88
use PhpMyAdmin\Controllers\Table\ChangeRowsController;
99
use PhpMyAdmin\DatabaseInterface;
10-
use PhpMyAdmin\Http\ServerRequest;
10+
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
1111
use PhpMyAdmin\Template;
1212
use PhpMyAdmin\Tests\AbstractTestCase;
1313
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
@@ -29,9 +29,9 @@ protected function setUp(): void
2929

3030
public function testChangeRowsController(): void
3131
{
32-
$_POST['rows_to_delete'] = 'row';
32+
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
33+
->withParsedBody(['rows_to_delete' => 'row']);
3334

34-
$request = $this->createStub(ServerRequest::class);
3535
$mock = $this->createMock(ChangeController::class);
3636
$mock->expects($this->once())->method('__invoke')->with($request);
3737

@@ -45,9 +45,9 @@ public function testChangeRowsController(): void
4545

4646
public function testWithoutRowsToDelete(): void
4747
{
48-
$_POST['goto'] = 'goto';
48+
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
49+
->withParsedBody(['goto' => 'goto']);
4950

50-
$request = $this->createStub(ServerRequest::class);
5151
$mock = $this->createMock(ChangeController::class);
5252
$mock->expects($this->never())->method('__invoke')->with($request);
5353

@@ -64,10 +64,9 @@ public function testWithoutRowsToDelete(): void
6464

6565
public function testWithRowsToDelete(): void
6666
{
67-
$_POST['goto'] = 'goto';
68-
$_POST['rows_to_delete'] = ['key1' => 'row1', 'key2' => 'row2'];
67+
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
68+
->withParsedBody(['goto' => 'goto', 'rows_to_delete' => ['key1' => 'row1', 'key2' => 'row2']]);
6969

70-
$request = $this->createStub(ServerRequest::class);
7170
$mock = $this->createMock(ChangeController::class);
7271
$mock->expects($this->once())->method('__invoke')->with($request);
7372

0 commit comments

Comments
 (0)