Skip to content

Commit 43ebb1b

Browse files
Merge pull request #19134 from kamil-tekiela/ForeignData
Create ForeignData
2 parents 0cb2c46 + 6e8f5b1 commit 43ebb1b

15 files changed

Lines changed: 94 additions & 165 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ parameters:
140140
count: 1
141141
path: src/Bookmarks/BookmarkRepository.php
142142

143-
-
144-
message: "#^Cannot access offset mixed on mixed\\.$#"
145-
count: 2
146-
path: src/BrowseForeigners.php
147-
148143
-
149144
message: "#^Cannot cast mixed to string\\.$#"
150145
count: 1
@@ -1140,16 +1135,6 @@ parameters:
11401135
count: 1
11411136
path: src/ConfigStorage/Relation.php
11421137

1143-
-
1144-
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) return type has no value type specified in iterable type array\\.$#"
1145-
count: 1
1146-
path: src/ConfigStorage/Relation.php
1147-
1148-
-
1149-
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) should return array\\{foreign_link\\: bool, the_total\\: int\\|string\\|null, foreign_display\\: string, disp_row\\: array\\<int, non\\-empty\\-array\\>\\|null, foreign_field\\: mixed\\} but returns array\\{foreign_link\\: bool, the_total\\: int\\|string\\|null, foreign_display\\: string, disp_row\\: array\\<int, array\\<string\\|null\\>\\>\\|null, foreign_field\\: mixed\\}\\.$#"
1150-
count: 1
1151-
path: src/ConfigStorage/Relation.php
1152-
11531138
-
11541139
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getTableReplacementNames\\(\\) should return array\\<string, string\\> but returns array\\<string, array\\<array\\<string\\>\\|bool\\|int\\|string\\>\\|int\\<1, max\\>\\|string\\|true\\>\\.$#"
11551140
count: 1
@@ -8625,11 +8610,6 @@ parameters:
86258610
count: 1
86268611
path: src/InsertEdit.php
86278612

8628-
-
8629-
message: "#^Only booleans are allowed in &&, array\\|false given on the left side\\.$#"
8630-
count: 2
8631-
path: src/InsertEdit.php
8632-
86338613
-
86348614
message: "#^Only booleans are allowed in &&, string\\|null given on the left side\\.$#"
86358615
count: 1

psalm-baseline.xml

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,14 @@
7979
<MixedArgument>
8080
<code><![CDATA[$descriptions[$indexByDescription]]]></code>
8181
<code><![CDATA[$descriptions[$indexByKeyname]]]></code>
82-
<code><![CDATA[$foreignData['the_total'] / $this->settings->maxRows]]></code>
8382
</MixedArgument>
84-
<MixedArrayAccess>
85-
<code><![CDATA[$relrow[$foreignData['foreign_display']]]]></code>
86-
<code><![CDATA[$relrow[$foreignData['foreign_field']]]]></code>
87-
</MixedArrayAccess>
8883
<MixedArrayOffset>
89-
<code><![CDATA[$relrow[$foreignData['foreign_display']]]]></code>
90-
<code><![CDATA[$relrow[$foreignData['foreign_field']]]]></code>
84+
<code><![CDATA[$relrow[$foreignData->foreignField]]]></code>
9185
</MixedArrayOffset>
9286
<MixedAssignment>
93-
<code><![CDATA[$descriptions[]]]></code>
94-
<code><![CDATA[$keys[]]]></code>
9587
<code><![CDATA[$leftKeyname]]></code>
96-
<code><![CDATA[$relrow]]></code>
9788
<code><![CDATA[$rightKeyname]]></code>
9889
</MixedAssignment>
99-
<MixedOperand>
100-
<code><![CDATA[$foreignData['the_total']]]></code>
101-
</MixedOperand>
10290
<PossiblyInvalidArgument>
10391
<code><![CDATA[$_POST['foreign_filter']]]></code>
10492
</PossiblyInvalidArgument>
@@ -109,9 +97,6 @@
10997
<code><![CDATA[$pos]]></code>
11098
<code><![CDATA[$this->settings->maxRows]]></code>
11199
</PossiblyInvalidOperand>
112-
<PossiblyUndefinedArrayOffset>
113-
<code><![CDATA[$foreignData['foreign_display']]]></code>
114-
</PossiblyUndefinedArrayOffset>
115100
</file>
116101
<file src="src/Cache.php">
117102
<MixedAssignment>
@@ -694,15 +679,6 @@
694679
<InvalidReturnType>
695680
<code><![CDATA[array<string, string>]]></code>
696681
</InvalidReturnType>
697-
<LessSpecificReturnStatement>
698-
<code><![CDATA[[
699-
'foreign_link' => $foreignLink,
700-
'the_total' => $theTotal,
701-
'foreign_display' => is_string($foreignDisplay) ? $foreignDisplay : '',
702-
'disp_row' => $dispRow,
703-
'foreign_field' => $foreignField,
704-
]]]></code>
705-
</LessSpecificReturnStatement>
706682
<MixedArgument>
707683
<code><![CDATA[$_SESSION['sql_history']]]></code>
708684
<code><![CDATA[$_SESSION['sql_history']]]></code>
@@ -772,15 +748,6 @@
772748
<code><![CDATA[$column['COLUMN_NAME']]]></code>
773749
<code><![CDATA[$foreigners[$column]]]></code>
774750
</MixedReturnStatement>
775-
<MoreSpecificReturnType>
776-
<code><![CDATA[array{
777-
* foreign_link: bool,
778-
* the_total: int|string|null,
779-
* foreign_display: string,
780-
* disp_row: list<non-empty-array>|null,
781-
* foreign_field: mixed
782-
* }]]></code>
783-
</MoreSpecificReturnType>
784751
<PossiblyFalseArgument>
785752
<code><![CDATA[$this->config->selectedServer['column_info']]]></code>
786753
<code><![CDATA[$this->config->selectedServer['column_info']]]></code>
@@ -3901,7 +3868,7 @@
39013868
<code><![CDATA[Config::getInstance()]]></code>
39023869
</DeprecatedMethod>
39033870
<MixedArgument>
3904-
<code><![CDATA[$foreignData['foreign_field']]]></code>
3871+
<code><![CDATA[$foreignData->foreignField]]></code>
39053872
</MixedArgument>
39063873
<MixedAssignment>
39073874
<code><![CDATA[$isUnsigned]]></code>
@@ -4236,7 +4203,7 @@
42364203
<code><![CDATA[$row[$_POST['criteriaColumnNames'][1]]]]></code>
42374204
</InvalidArrayOffset>
42384205
<MixedArgument>
4239-
<code><![CDATA[$foreignData['foreign_field']]]></code>
4206+
<code><![CDATA[$foreignData->foreignField]]></code>
42404207
<code><![CDATA[$properties['type']]]></code>
42414208
</MixedArgument>
42424209
<MixedAssignment>
@@ -6767,7 +6734,7 @@
67676734
<code><![CDATA[$defaultValue]]></code>
67686735
<code><![CDATA[$extractedColumnspec['enum_set_values']]]></code>
67696736
<code><![CDATA[$extractedColumnspec['spec_in_brackets']]]></code>
6770-
<code><![CDATA[$foreignData['foreign_field']]]></code>
6737+
<code><![CDATA[$foreignData->foreignField]]></code>
67716738
<code><![CDATA[$foreigner['foreign_db']]]></code>
67726739
<code><![CDATA[$foreigner['foreign_db']]]></code>
67736740
<code><![CDATA[$foreigner['foreign_db']]]></code>
@@ -6852,8 +6819,6 @@
68526819
<RiskyTruthyFalsyComparison>
68536820
<code><![CDATA[! $gotoInclude]]></code>
68546821
<code><![CDATA[$editField->salt]]></code>
6855-
<code><![CDATA[$foreigner]]></code>
6856-
<code><![CDATA[$foreigner]]></code>
68576822
</RiskyTruthyFalsyComparison>
68586823
</file>
68596824
<file src="src/IpAllowDeny.php">
@@ -10801,7 +10766,7 @@
1080110766
<code><![CDATA[string]]></code>
1080210767
</InvalidNullableReturnType>
1080310768
<MixedArgument>
10804-
<code><![CDATA[$foreignData['foreign_field']]]></code>
10769+
<code><![CDATA[$foreignData->foreignField]]></code>
1080510770
<code><![CDATA[$numberOfLine / $maxRows]]></code>
1080610771
<code><![CDATA[$sortCol]]></code>
1080710772
<code><![CDATA[$statement->expr]]></code>
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
{% if foreign_data.disp_row is iterable and
2-
(show_all and foreign_data.the_total > max_rows) %}
3-
<input class="btn btn-secondary" type="submit" id="foreign_showAll" name="foreign_showAll" value="
4-
{{- t('Show all') }}">
5-
{% endif %}
1+
<input class="btn btn-secondary" type="submit" id="foreign_showAll" name="foreign_showAll" value="
2+
{{- t('Show all') }}">

resources/templates/table/search/input_box.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{# Get inputbox based on different column types (Foreign key, geometrical, enum) #}
22
{% if foreigners and search_column_in_foreigners %}
3-
{% if foreign_data['disp_row'] is iterable %}
3+
{% if foreign_data.dispRow is iterable %}
44
<select name="criteriaValues[{{ column_index }}]"
55
id="{{ column_id }}{{ column_index }}">
66
{{ foreign_dropdown|raw }}
77
</select>
8-
{% elseif foreign_data['foreign_link'] == true %}
8+
{% elseif foreign_data.foreignLink %}
99
<input type="text"
1010
id="{{ column_id }}{{ column_index }}"
1111
name="criteriaValues[{{ column_index }}]"

src/BrowseForeigners.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace PhpMyAdmin;
99

1010
use PhpMyAdmin\Config\Settings;
11+
use PhpMyAdmin\ConfigStorage\ForeignData;
1112
use PhpMyAdmin\Theme\ThemeManager;
1213

1314
use function __;
@@ -16,7 +17,6 @@
1617
use function ceil;
1718
use function floor;
1819
use function htmlspecialchars;
19-
use function is_array;
2020
use function mb_strlen;
2121
use function mb_substr;
2222

@@ -133,24 +133,25 @@ private function getHtmlForOneKey(
133133
* @param string $db current database
134134
* @param string $table current table
135135
* @param string $field field
136-
* @param mixed[] $foreignData foreign column data
137136
* @param string|null $fieldKey field key
138137
* @param string $currentValue current columns's value
139138
*/
140139
public function getHtmlForRelationalFieldSelection(
141140
string $db,
142141
string $table,
143142
string $field,
144-
array $foreignData,
143+
ForeignData $foreignData,
145144
string|null $fieldKey,
146145
string $currentValue,
147146
): string {
148147
$gotoPage = $this->getHtmlForGotoPage($foreignData);
149-
$foreignShowAll = $this->template->render('table/browse_foreigners/show_all', [
150-
'foreign_data' => $foreignData,
151-
'show_all' => $this->settings->showAll,
152-
'max_rows' => $this->settings->maxRows,
153-
]);
148+
$foreignShowAll = '';
149+
if (
150+
$foreignData->dispRow !== null &&
151+
$this->settings->showAll && $foreignData->theTotal > $this->settings->maxRows
152+
) {
153+
$foreignShowAll = $this->template->render('table/browse_foreigners/show_all');
154+
}
154155

155156
$output = '<form class="ajax" '
156157
. 'id="browse_foreign_form" name="browse_foreign_from" action="'
@@ -185,7 +186,7 @@ public function getHtmlForRelationalFieldSelection(
185186

186187
$output .= '<table class="table table-striped table-hover" id="browse_foreign_table">' . "\n";
187188

188-
if (! is_array($foreignData['disp_row'])) {
189+
if ($foreignData->dispRow === null) {
189190
return $output . '</tbody>'
190191
. '</table>';
191192
}
@@ -204,14 +205,9 @@ public function getHtmlForRelationalFieldSelection(
204205

205206
$descriptions = [];
206207
$keys = [];
207-
foreach ($foreignData['disp_row'] as $relrow) {
208-
if ($foreignData['foreign_display'] != false) {
209-
$descriptions[] = $relrow[$foreignData['foreign_display']] ?? '';
210-
} else {
211-
$descriptions[] = '';
212-
}
213-
214-
$keys[] = $relrow[$foreignData['foreign_field']];
208+
foreach ($foreignData->dispRow as $relrow) {
209+
$descriptions[] = $relrow[$foreignData->foreignDisplay] ?? '';
210+
$keys[] = $relrow[$foreignData->foreignField];
215211
}
216212

217213
asort($keys);
@@ -258,20 +254,18 @@ private function getDescriptionAndTitle(string $description): array
258254

259255
/**
260256
* Function to get html for the goto page option
261-
*
262-
* @param mixed[]|null $foreignData foreign data
263257
*/
264-
private function getHtmlForGotoPage(array|null $foreignData): string
258+
private function getHtmlForGotoPage(ForeignData $foreignData): string
265259
{
266260
isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
267-
if ($foreignData === null || ! is_array($foreignData['disp_row'])) {
261+
if ($foreignData->dispRow === null) {
268262
return '';
269263
}
270264

271265
$pageNow = (int) floor($pos / $this->settings->maxRows) + 1;
272-
$nbTotalPage = (int) ceil($foreignData['the_total'] / $this->settings->maxRows);
266+
$nbTotalPage = (int) ceil($foreignData->theTotal / $this->settings->maxRows);
273267

274-
if ($foreignData['the_total'] > $this->settings->maxRows) {
268+
if ($foreignData->theTotal > $this->settings->maxRows) {
275269
return Util::pageselector(
276270
'pos',
277271
$this->settings->maxRows,

src/ConfigStorage/ForeignData.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\ConfigStorage;
6+
7+
class ForeignData
8+
{
9+
/** @param list<array<array-key, string|null>>|null $dispRow */
10+
public function __construct(
11+
public bool $foreignLink,
12+
public int $theTotal,
13+
public string $foreignDisplay,
14+
public array|null $dispRow,
15+
public mixed $foreignField,
16+
) {
17+
}
18+
}

src/ConfigStorage/Relation.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -885,15 +885,6 @@ public function foreignDropdown(
885885
* @param bool $getTotal optional, whether to get total num of rows
886886
* in $foreignData['the_total;]
887887
* (has an effect of performance)
888-
*
889-
* @return array<string, mixed> data about the foreign keys
890-
* @psalm-return array{
891-
* foreign_link: bool,
892-
* the_total: int|string|null,
893-
* foreign_display: string,
894-
* disp_row: list<non-empty-array>|null,
895-
* foreign_field: mixed
896-
* }
897888
*/
898889
public function getForeignData(
899890
array|bool $foreigners,
@@ -902,7 +893,7 @@ public function getForeignData(
902893
string $foreignFilter,
903894
string $foreignLimit,
904895
bool $getTotal = false,
905-
): array {
896+
): ForeignData {
906897
// we always show the foreign field in the drop-down; if a display
907898
// field is defined, we show it besides the foreign field
908899
$foreignLink = false;
@@ -966,17 +957,10 @@ public function getForeignData(
966957

967958
if ($foreignFilter !== '') {
968959
$theTotal = $this->dbi->fetchValue('SELECT COUNT(*)' . $fQueryFrom . $fQueryFilter);
969-
if ($theTotal === false) {
970-
$theTotal = 0;
971-
}
972960
}
973961

974962
$disp = $this->dbi->tryQuery($fQueryMain . $fQueryFrom . $fQueryFilter . $fQueryOrder . $fQueryLimit);
975963
if ($disp && $disp->numRows() > 0) {
976-
// If a resultset has been created, pre-cache it in the $disp_row
977-
// array. This helps us from not needing to use mysql_data_seek by
978-
// accessing a pre-cached PHP array. Usually those resultsets are
979-
// not that big, so a performance hit should not be expected.
980964
$dispRow = $disp->fetchAllAssoc();
981965
} else {
982966
// Either no data in the foreign table or
@@ -996,13 +980,13 @@ public function getForeignData(
996980
->countRecords(true);
997981
}
998982

999-
return [
1000-
'foreign_link' => $foreignLink,
1001-
'the_total' => $theTotal,
1002-
'foreign_display' => is_string($foreignDisplay) ? $foreignDisplay : '',
1003-
'disp_row' => $dispRow,
1004-
'foreign_field' => $foreignField,
1005-
];
983+
return new ForeignData(
984+
$foreignLink,
985+
(int) $theTotal,
986+
is_string($foreignDisplay) ? $foreignDisplay : '',
987+
$dispRow,
988+
$foreignField,
989+
);
1006990
}
1007991

1008992
/**

src/Controllers/Table/SearchController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use function __;
3030
use function array_keys;
3131
use function in_array;
32-
use function is_array;
3332
use function mb_strtolower;
3433
use function md5;
3534
use function preg_match;
@@ -377,12 +376,12 @@ private function getColumnProperties(int $searchIndex, int $columnIndex): array
377376
if (
378377
$this->foreigners
379378
&& $searchColumnInForeigners
380-
&& is_array($foreignData['disp_row'])
379+
&& $foreignData->dispRow !== null
381380
) {
382381
$foreignDropdown = $this->relation->foreignDropdown(
383-
$foreignData['disp_row'],
384-
$foreignData['foreign_field'],
385-
$foreignData['foreign_display'],
382+
$foreignData->dispRow,
383+
$foreignData->foreignField,
384+
$foreignData->foreignDisplay,
386385
'',
387386
Config::getInstance()->settings['ForeignKeyMaxLimit'],
388387
);

0 commit comments

Comments
 (0)