Skip to content

Commit 681e02d

Browse files
Merge pull request #19325 from kamil-tekiela/Refactoring-BrowseForeigners
Refactoring browse foreigners
2 parents 408bf15 + c2cbd15 commit 681e02d

5 files changed

Lines changed: 49 additions & 62 deletions

File tree

phpstan-baseline.neon

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,11 @@ parameters:
148148
count: 1
149149
path: src/Bookmarks/BookmarkRepository.php
150150

151-
-
152-
message: "#^Cannot cast mixed to string\\.$#"
153-
count: 1
154-
path: src/BrowseForeigners.php
155-
156151
-
157152
message: "#^Parameter \\#1 \\$description of method PhpMyAdmin\\\\BrowseForeigners\\:\\:getDescriptionAndTitle\\(\\) expects string, mixed given\\.$#"
158153
count: 2
159154
path: src/BrowseForeigners.php
160155

161-
-
162-
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, mixed given\\.$#"
163-
count: 1
164-
path: src/BrowseForeigners.php
165-
166156
-
167157
message: "#^Parameter \\#1 \\$state of static method PhpMyAdmin\\\\Charsets\\\\Charset\\:\\:fromServer\\(\\) expects array\\{Charset\\?\\: string, Description\\?\\: string, Default collation\\?\\: string, Maxlen\\?\\: string\\}, array\\<string\\|null\\> given\\.$#"
168158
count: 1
@@ -1391,6 +1381,11 @@ parameters:
13911381
count: 1
13921382
path: src/Console.php
13931383

1384+
-
1385+
message: "#^Cannot cast mixed to int\\.$#"
1386+
count: 1
1387+
path: src/Controllers/BrowseForeignersController.php
1388+
13941389
-
13951390
message: """
13961391
#^Call to deprecated method getInstance\\(\\) of class PhpMyAdmin\\\\Config\\:

psalm-baseline.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@
8787
<code><![CDATA[$leftKeyname]]></code>
8888
<code><![CDATA[$rightKeyname]]></code>
8989
</MixedAssignment>
90-
<PossiblyInvalidArgument>
91-
<code><![CDATA[$_POST['foreign_filter']]]></code>
92-
</PossiblyInvalidArgument>
93-
<PossiblyInvalidCast>
94-
<code><![CDATA[$_POST['rownumber']]]></code>
95-
</PossiblyInvalidCast>
96-
<PossiblyInvalidOperand>
97-
<code><![CDATA[$pos]]></code>
98-
<code><![CDATA[$this->settings->maxRows]]></code>
99-
</PossiblyInvalidOperand>
10090
</file>
10191
<file src="src/Cache.php">
10292
<MixedAssignment>

src/BrowseForeigners.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,24 @@ private function getHtmlForOneKey(
130130
/**
131131
* Function to get html for relational field selection
132132
*
133-
* @param string $db current database
134-
* @param string $table current table
135-
* @param string $field field
136-
* @param string|null $fieldKey field key
137-
* @param string $currentValue current columns's value
133+
* @param string $db current database
134+
* @param string $table current table
135+
* @param string $field field
136+
* @param string $fieldKey field key
137+
* @param string $currentValue current columns's value
138138
*/
139139
public function getHtmlForRelationalFieldSelection(
140140
string $db,
141141
string $table,
142142
string $field,
143143
ForeignData $foreignData,
144-
string|null $fieldKey,
144+
string $fieldKey,
145145
string $currentValue,
146+
int $pos,
147+
string $foreignFilter,
148+
string|null $rownumber,
146149
): string {
147-
$gotoPage = $this->getHtmlForGotoPage($foreignData);
150+
$gotoPage = $this->getHtmlForGotoPage($foreignData, $pos);
148151
$foreignShowAll = '';
149152
if (
150153
$foreignData->dispRow !== null &&
@@ -160,16 +163,13 @@ public function getHtmlForRelationalFieldSelection(
160163
. Url::getHiddenInputs($db, $table) . "\n"
161164
. '<input type="hidden" name="field" value="' . htmlspecialchars($field) . '">' . "\n"
162165
. '<input type="hidden" name="fieldkey" value="'
163-
. (isset($fieldKey) ? htmlspecialchars($fieldKey) : '') . '">' . "\n";
166+
. htmlspecialchars($fieldKey) . '">' . "\n";
164167

165-
if (isset($_POST['rownumber'])) {
166-
$output .= '<input type="hidden" name="rownumber" value="'
167-
. htmlspecialchars((string) $_POST['rownumber']) . '">';
168+
if ($rownumber !== null) {
169+
$output .= '<input type="hidden" name="rownumber" value="' . htmlspecialchars($rownumber) . '">';
168170
}
169171

170-
$filterValue = isset($_POST['foreign_filter'])
171-
? htmlspecialchars($_POST['foreign_filter'])
172-
: '';
172+
$filterValue = htmlspecialchars($foreignFilter);
173173
$output .= '<div class="col-auto">'
174174
. '<label class="form-label" for="input_foreign_filter">' . __('Search:') . '</label></div>' . "\n"
175175
. '<div class="col-auto"><input class="form-control" type="text" name="foreign_filter" '
@@ -238,7 +238,7 @@ public function getHtmlForRelationalFieldSelection(
238238
*
239239
* @param string $description the key name's description
240240
*
241-
* @return array<int,string> the new description and title
241+
* @return array{string, string} the new description and title
242242
*/
243243
private function getDescriptionAndTitle(string $description): array
244244
{
@@ -255,9 +255,8 @@ private function getDescriptionAndTitle(string $description): array
255255
/**
256256
* Function to get html for the goto page option
257257
*/
258-
private function getHtmlForGotoPage(ForeignData $foreignData): string
258+
private function getHtmlForGotoPage(ForeignData $foreignData, int $pos): string
259259
{
260-
isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
261260
if ($foreignData->dispRow === null) {
262261
return '';
263262
}
@@ -283,19 +282,12 @@ private function getHtmlForGotoPage(ForeignData $foreignData): string
283282
return '';
284283
}
285284

286-
/**
287-
* Function to get foreign limit
288-
*
289-
* @param string|null $foreignShowAll foreign navigation
290-
*/
291-
public function getForeignLimit(string|null $foreignShowAll): string|null
285+
public function getForeignLimit(string|null $foreignShowAll, int $pos): string
292286
{
293287
if ($foreignShowAll === __('Show all')) {
294-
return null;
288+
return '';
295289
}
296290

297-
isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
298-
299291
return 'LIMIT ' . $pos . ', ' . $this->settings->maxRows . ' ';
300292
}
301293
}

src/Controllers/BrowseForeignersController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function __invoke(ServerRequest $request): Response
3838
$foreignShowAll = $request->getParsedBodyParam('foreign_showAll');
3939
/** @var string $foreignFilter */
4040
$foreignFilter = $request->getParsedBodyParam('foreign_filter', '');
41+
/** @var string|null $rownumber */
42+
$rownumber = $request->getParsedBodyParam('rownumber');
4143

4244
if (! isset($database, $table, $field)) {
4345
return $this->response->response();
@@ -48,13 +50,14 @@ public function __invoke(ServerRequest $request): Response
4850
$header->disableMenuAndConsole();
4951
$header->setBodyId('body_browse_foreigners');
5052

51-
$foreignLimit = $this->browseForeigners->getForeignLimit($foreignShowAll);
53+
$pos = (int) $request->getParsedBodyParam('pos');
54+
$foreignLimit = $this->browseForeigners->getForeignLimit($foreignShowAll, $pos);
5255
$foreignData = $this->relation->getForeignData(
5356
$this->relation->getForeigners($database, $table),
5457
$field,
5558
true,
5659
$foreignFilter,
57-
$foreignLimit ?? '',
60+
$foreignLimit,
5861
true,
5962
);
6063

@@ -65,6 +68,9 @@ public function __invoke(ServerRequest $request): Response
6568
$foreignData,
6669
$fieldKey,
6770
$data,
71+
$pos,
72+
$foreignFilter,
73+
$rownumber,
6874
));
6975

7076
return $this->response->response();

tests/unit/BrowseForeignersTest.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@ protected function setUp(): void
3131
*/
3232
public function testGetForeignLimit(): void
3333
{
34-
self::assertNull(
35-
$this->browseForeigners->getForeignLimit('Show all'),
34+
self::assertSame(
35+
'',
36+
$this->browseForeigners->getForeignLimit('Show all', 0),
3637
);
3738

3839
self::assertSame(
3940
'LIMIT 0, 25 ',
40-
$this->browseForeigners->getForeignLimit(null),
41+
$this->browseForeigners->getForeignLimit(null, 0),
4142
);
4243

43-
$_POST['pos'] = 10;
44-
4544
self::assertSame(
4645
'LIMIT 10, 25 ',
47-
$this->browseForeigners->getForeignLimit(null),
46+
$this->browseForeigners->getForeignLimit(null, 10),
4847
);
4948

5049
$config = new Config();
@@ -53,12 +52,12 @@ public function testGetForeignLimit(): void
5352

5453
self::assertSame(
5554
'LIMIT 10, 50 ',
56-
$browseForeigners->getForeignLimit(null),
55+
$browseForeigners->getForeignLimit(null, 10),
5756
);
5857

5958
self::assertSame(
6059
'LIMIT 10, 50 ',
61-
$browseForeigners->getForeignLimit('xyz'),
60+
$browseForeigners->getForeignLimit('xyz', 10),
6261
);
6362
}
6463

@@ -74,11 +73,10 @@ public function testGetHtmlForGotoPage(): void
7473
$this->browseForeigners,
7574
BrowseForeigners::class,
7675
'getHtmlForGotoPage',
77-
[$foreignData],
76+
[$foreignData, 0],
7877
),
7978
);
8079

81-
$_POST['pos'] = 15;
8280
$foreignData = new ForeignData(false, 5, '', [], '');
8381

8482
self::assertSame(
@@ -87,7 +85,7 @@ public function testGetHtmlForGotoPage(): void
8785
$this->browseForeigners,
8886
BrowseForeigners::class,
8987
'getHtmlForGotoPage',
90-
[$foreignData],
88+
[$foreignData, 15],
9189
),
9290
);
9391

@@ -96,7 +94,7 @@ public function testGetHtmlForGotoPage(): void
9694
$this->browseForeigners,
9795
BrowseForeigners::class,
9896
'getHtmlForGotoPage',
99-
[$foreignData],
97+
[$foreignData, 15],
10098
);
10199

102100
self::assertStringStartsWith('Page number:', $result);
@@ -153,15 +151,18 @@ public function testGetHtmlForRelationalFieldSelection(): void
153151
$foreignData = new ForeignData(false, 0, '', null, '');
154152
$fieldkey = 'bar';
155153
$currentValue = '';
156-
$_POST['rownumber'] = 1;
157-
$_POST['foreign_filter'] = '5';
154+
$rownumber = '1';
155+
$foreignFilter = '5';
158156
$result = $this->browseForeigners->getHtmlForRelationalFieldSelection(
159157
$db,
160158
$table,
161159
$field,
162160
$foreignData,
163161
$fieldkey,
164162
$currentValue,
163+
0,
164+
$foreignFilter,
165+
$rownumber,
165166
);
166167

167168
self::assertStringContainsString(
@@ -206,6 +207,9 @@ public function testGetHtmlForRelationalFieldSelection(): void
206207
$foreignData,
207208
$fieldkey,
208209
$currentValue,
210+
0,
211+
$foreignFilter,
212+
$rownumber,
209213
);
210214

211215
self::assertStringContainsString(

0 commit comments

Comments
 (0)