Skip to content

Commit d612269

Browse files
Merge pull request #18432 from kamil-tekiela/ForeignKeyRelatedTable
ForeignKeyRelatedTable DTO
2 parents d0b219f + dfcceb1 commit d612269

4 files changed

Lines changed: 111 additions & 76 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Display;
6+
7+
final class ForeignKeyRelatedTable
8+
{
9+
public function __construct(
10+
public readonly string $table,
11+
public readonly string $field,
12+
public readonly string $displayField,
13+
public readonly string $database,
14+
) {
15+
}
16+
}

libraries/classes/Display/Results.php

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,10 +1890,10 @@ private function addClass(
18901890
*
18911891
* @see getTable()
18921892
*
1893-
* @param ResultInterface $dtResult the link id associated to the query
1894-
* which results have to be displayed
1895-
* @param array<string, string[]> $map the list of relations
1896-
* @param bool $isLimitedDisplay with limited operations or not
1893+
* @param ResultInterface $dtResult the link id associated to the query
1894+
* which results have to be displayed
1895+
* @param ForeignKeyRelatedTable[] $map the list of relations
1896+
* @param bool $isLimitedDisplay with limited operations or not
18971897
*
18981898
* @return string html content
18991899
*
@@ -2218,16 +2218,16 @@ private function setMimeMap(): void
22182218
*
22192219
* @see getTableBody()
22202220
*
2221-
* @param mixed[] $row current row data
2222-
* @param int $rowNumber the index of current row
2223-
* @param mixed[]|false $colOrder the column order false when
2224-
* a property not found false
2225-
* when a property not found
2226-
* @param array<string, string[]> $map the list of relations
2227-
* @param bool|mixed[]|string $colVisib column is visible(false);
2228-
* column isn't visible(string
2229-
* array)
2230-
* @param string $urlSqlQuery the analyzed sql query
2221+
* @param mixed[] $row current row data
2222+
* @param int $rowNumber the index of current row
2223+
* @param mixed[]|false $colOrder the column order false when
2224+
* a property not found false
2225+
* when a property not found
2226+
* @param ForeignKeyRelatedTable[] $map the list of relations
2227+
* @param bool|mixed[]|string $colVisib column is visible(false);
2228+
* column isn't visible(string
2229+
* array)
2230+
* @param string $urlSqlQuery the analyzed sql query
22312231
* @psalm-param 'double-click'|'click'|'disabled' $gridEditConfig
22322232
*
22332233
* @return string html content
@@ -2847,12 +2847,12 @@ private function getClassForDateTimeRelatedFields(FieldMetadata $meta): string
28472847
*
28482848
* @see getTableBody()
28492849
*
2850-
* @param string|null $column the column's value
2851-
* @param string $class the html class for column
2852-
* @param bool $conditionField the column should highlighted or not
2853-
* @param FieldMetadata $meta the meta-information about this field
2854-
* @param array<string, string[]> $map the list of relations
2855-
* @param mixed[] $transformOptions the transformation parameters
2850+
* @param string|null $column the column's value
2851+
* @param string $class the html class for column
2852+
* @param bool $conditionField the column should highlighted or not
2853+
* @param FieldMetadata $meta the meta-information about this field
2854+
* @param ForeignKeyRelatedTable[] $map the list of relations
2855+
* @param mixed[] $transformOptions the transformation parameters
28562856
*
28572857
* @return string the prepared cell, html content
28582858
*/
@@ -2896,13 +2896,13 @@ private function getDataCellForNumericColumns(
28962896
*
28972897
* @see getTableBody()
28982898
*
2899-
* @param string|null $column the relevant column in data row
2900-
* @param string $class the html class for column
2901-
* @param FieldMetadata $meta the meta-information about this field
2902-
* @param array<string, string[]> $map the list of relations
2903-
* @param mixed[] $urlParams the parameters for generate url
2904-
* @param bool $conditionField the column should highlighted or not
2905-
* @param mixed[] $transformOptions the transformation parameters
2899+
* @param string|null $column the relevant column in data row
2900+
* @param string $class the html class for column
2901+
* @param FieldMetadata $meta the meta-information about this field
2902+
* @param ForeignKeyRelatedTable[] $map the list of relations
2903+
* @param mixed[] $urlParams the parameters for generate url
2904+
* @param bool $conditionField the column should highlighted or not
2905+
* @param mixed[] $transformOptions the transformation parameters
29062906
*
29072907
* @return string the prepared data cell, html content
29082908
*/
@@ -3012,13 +3012,13 @@ private function getDataCellForGeometryColumns(
30123012
*
30133013
* @see getTableBody()
30143014
*
3015-
* @param string|null $column the relevant column in data row
3016-
* @param string $class the html class for column
3017-
* @param FieldMetadata $meta the meta-information about the field
3018-
* @param array<string, string[]> $map the list of relations
3019-
* @param mixed[] $urlParams the parameters for generate url
3020-
* @param bool $conditionField the column should highlighted or not
3021-
* @param mixed[] $transformOptions the transformation parameters
3015+
* @param string|null $column the relevant column in data row
3016+
* @param string $class the html class for column
3017+
* @param FieldMetadata $meta the meta-information about the field
3018+
* @param ForeignKeyRelatedTable[] $map the list of relations
3019+
* @param mixed[] $urlParams the parameters for generate url
3020+
* @param bool $conditionField the column should highlighted or not
3021+
* @param mixed[] $transformOptions the transformation parameters
30223022
*
30233023
* @return string the prepared data cell, html content
30243024
*/
@@ -3442,17 +3442,17 @@ public function getTable(
34423442

34433443
if ($this->properties['table'] !== '') {
34443444
// This method set the values for $map array
3445-
$map = $this->setParamForLinkForeignKeyRelatedTables($map);
3445+
$map = $this->getForeignKeyRelatedTables();
34463446

34473447
// Coming from 'Distinct values' action of structure page
34483448
// We manipulate relations mechanism to show a link to related rows.
34493449
if ($this->properties['is_browse_distinct']) {
3450-
$map[$fieldsMeta[1]->name] = [
3450+
$map[$fieldsMeta[1]->name] = new ForeignKeyRelatedTable(
34513451
$this->properties['table'],
34523452
$fieldsMeta[1]->name,
34533453
'',
34543454
$this->properties['db'],
3455-
];
3455+
);
34563456
}
34573457
}
34583458

@@ -3735,13 +3735,9 @@ private function setMessageInformation(
37353735
/**
37363736
* Set the value of $map array for linking foreign key related tables
37373737
*
3738-
* @see getTable()
3739-
*
3740-
* @param array<string, string[]> $map the list of relations
3741-
*
3742-
* @return array<string, string[]>
3738+
* @return ForeignKeyRelatedTable[]
37433739
*/
3744-
private function setParamForLinkForeignKeyRelatedTables(array $map): array
3740+
private function getForeignKeyRelatedTables(): array
37453741
{
37463742
// To be able to later display a link to the related table,
37473743
// we verify both types of relations: either those that are
@@ -3757,13 +3753,19 @@ private function setParamForLinkForeignKeyRelatedTables(array $map): array
37573753
);
37583754

37593755
if ($existRel === []) {
3760-
return $map;
3756+
return [];
37613757
}
37623758

3759+
$map = [];
37633760
foreach ($existRel as $masterField => $rel) {
37643761
if ($masterField !== 'foreign_keys_data') {
37653762
$displayField = $this->relation->getDisplayField($rel['foreign_db'], $rel['foreign_table']);
3766-
$map[$masterField] = [$rel['foreign_table'], $rel['foreign_field'], $displayField, $rel['foreign_db']];
3763+
$map[$masterField] = new ForeignKeyRelatedTable(
3764+
$rel['foreign_table'],
3765+
$rel['foreign_field'],
3766+
$displayField,
3767+
$rel['foreign_db'],
3768+
);
37673769
} else {
37683770
foreach ($rel as $oneKey) {
37693771
foreach ($oneKey['index_list'] as $index => $oneField) {
@@ -3772,12 +3774,12 @@ private function setParamForLinkForeignKeyRelatedTables(array $map): array
37723774
$oneKey['ref_table_name'],
37733775
);
37743776

3775-
$map[$oneField] = [
3777+
$map[$oneField] = new ForeignKeyRelatedTable(
37763778
$oneKey['ref_table_name'],
37773779
$oneKey['ref_index_list'][$index],
37783780
$displayField,
37793781
$oneKey['ref_db_name'] ?? $GLOBALS['db'],
3780-
];
3782+
);
37813783
}
37823784
}
37833785
}
@@ -4024,21 +4026,21 @@ private function handleNonPrintableContents(
40244026
/**
40254027
* Retrieves the associated foreign key info for a data cell
40264028
*
4027-
* @param string[] $fieldInfo the relation
4028-
* @param string $whereComparison data for the where clause
4029+
* @param ForeignKeyRelatedTable $fieldInfo the relation
4030+
* @param string $whereComparison data for the where clause
40294031
*
40304032
* @return string|null formatted data
40314033
*/
4032-
private function getFromForeign(array $fieldInfo, string $whereComparison): string|null
4034+
private function getFromForeign(ForeignKeyRelatedTable $fieldInfo, string $whereComparison): string|null
40334035
{
40344036
$dispsql = 'SELECT '
4035-
. Util::backquote($fieldInfo[2])
4037+
. Util::backquote($fieldInfo->displayField)
40364038
. ' FROM '
4037-
. Util::backquote($fieldInfo[3])
4039+
. Util::backquote($fieldInfo->database)
40384040
. '.'
4039-
. Util::backquote($fieldInfo[0])
4041+
. Util::backquote($fieldInfo->table)
40404042
. ' WHERE '
4041-
. Util::backquote($fieldInfo[1])
4043+
. Util::backquote($fieldInfo->field)
40424044
. $whereComparison;
40434045

40444046
$dispval = $this->dbi->fetchValue($dispsql);
@@ -4063,17 +4065,17 @@ private function getFromForeign(array $fieldInfo, string $whereComparison): stri
40634065
* @see getDataCellForNumericColumns(), getDataCellForGeometryColumns(),
40644066
* getDataCellForNonNumericColumns(),
40654067
*
4066-
* @param string $class css classes for the td element
4067-
* @param bool $conditionField whether the column is a part of the where clause
4068-
* @param FieldMetadata $meta the meta-information about the field
4069-
* @param array<string, string[]> $map the list of relations
4070-
* @param string $data data
4071-
* @param string $displayedData data that will be displayed (maybe be chunked)
4072-
* @param string $nowrap 'nowrap' if the content should not be wrapped
4073-
* @param string $whereComparison data for the where clause
4074-
* @param mixed[] $transformOptions options for transformation
4075-
* @param bool $isFieldTruncated whether the field is truncated
4076-
* @param string $originalLength of a truncated column, or ''
4068+
* @param string $class css classes for the td element
4069+
* @param bool $conditionField whether the column is a part of the where clause
4070+
* @param FieldMetadata $meta the meta-information about the field
4071+
* @param ForeignKeyRelatedTable[] $map the list of relations
4072+
* @param string $data data
4073+
* @param string $displayedData data that will be displayed (maybe be chunked)
4074+
* @param string $nowrap 'nowrap' if the content should not be wrapped
4075+
* @param string $whereComparison data for the where clause
4076+
* @param mixed[] $transformOptions options for transformation
4077+
* @param bool $isFieldTruncated whether the field is truncated
4078+
* @param string $originalLength of a truncated column, or ''
40774079
*
40784080
* @return string formatted data
40794081
*/
@@ -4119,14 +4121,12 @@ private function getRowData(
41194121
}
41204122

41214123
if (isset($map[$meta->name])) {
4122-
/** @var array{0: string, 1: string, 2: string, 3: string} $relation */
41234124
$relation = $map[$meta->name];
41244125
// Field to display from the foreign table?
41254126
$dispval = '';
41264127

41274128
// Check that we have a valid column name
4128-
// Relation::getDisplayField() returns false by default
4129-
if ($relation[2] !== '') {
4129+
if ($relation->displayField !== '') {
41304130
$dispval = $this->getFromForeign($relation, $whereComparison);
41314131
}
41324132

@@ -4140,15 +4140,15 @@ private function getRowData(
41404140
$value .= ' <code>[-&gt;' . $dispval . ']</code>';
41414141
} else {
41424142
$sqlQuery = 'SELECT * FROM '
4143-
. Util::backquote($relation[3]) . '.'
4144-
. Util::backquote($relation[0])
4143+
. Util::backquote($relation->database) . '.'
4144+
. Util::backquote($relation->table)
41454145
. ' WHERE '
4146-
. Util::backquote($relation[1])
4146+
. Util::backquote($relation->field)
41474147
. $whereComparison;
41484148

41494149
$urlParams = [
4150-
'db' => $relation[3],
4151-
'table' => $relation[0],
4150+
'db' => $relation->database,
4151+
'table' => $relation->table,
41524152
'pos' => '0',
41534153
'sql_signature' => Core::signSqlQuery($sqlQuery),
41544154
'sql_query' => $sqlQuery,
@@ -4158,7 +4158,7 @@ private function getRowData(
41584158
// always apply a transformation on the real data,
41594159
// not on the display field
41604160
$displayedData = $transformationPlugin->applyTransformation($data, $transformOptions, $meta);
4161-
} elseif ($relationalDisplay === self::RELATIONAL_DISPLAY_COLUMN && $relation[2] !== '') {
4161+
} elseif ($relationalDisplay === self::RELATIONAL_DISPLAY_COLUMN && $relation->displayField !== '') {
41624162
// user chose "relational display field" in the
41634163
// display options, so show display field in the cell
41644164
$displayedData = $dispval === null ? '<em>NULL</em>' : Core::mimeDefaultFunction($dispval);

phpstan-baseline.neon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,11 +3710,21 @@ parameters:
37103710
count: 1
37113711
path: libraries/classes/Display/Results.php
37123712

3713+
-
3714+
message: "#^Parameter \\#1 \\$table of class PhpMyAdmin\\\\Display\\\\ForeignKeyRelatedTable constructor expects string, mixed given\\.$#"
3715+
count: 2
3716+
path: libraries/classes/Display/Results.php
3717+
37133718
-
37143719
message: "#^Parameter \\#2 \\$columnValue of method PhpMyAdmin\\\\Display\\\\Results\\:\\:getSpecialLinkUrl\\(\\) expects string, mixed given\\.$#"
37153720
count: 1
37163721
path: libraries/classes/Display/Results.php
37173722

3723+
-
3724+
message: "#^Parameter \\#2 \\$field of class PhpMyAdmin\\\\Display\\\\ForeignKeyRelatedTable constructor expects string, mixed given\\.$#"
3725+
count: 2
3726+
path: libraries/classes/Display/Results.php
3727+
37183728
-
37193729
message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#"
37203730
count: 2
@@ -3745,6 +3755,11 @@ parameters:
37453755
count: 3
37463756
path: libraries/classes/Display/Results.php
37473757

3758+
-
3759+
message: "#^Parameter \\#4 \\$database of class PhpMyAdmin\\\\Display\\\\ForeignKeyRelatedTable constructor expects string, mixed given\\.$#"
3760+
count: 1
3761+
path: libraries/classes/Display/Results.php
3762+
37483763
-
37493764
message: "#^Parameter \\#5 \\$sortDirection of method PhpMyAdmin\\\\Display\\\\Results\\:\\:getSingleAndMultiSortUrls\\(\\) expects array\\<string\\>, array given\\.$#"
37503765
count: 1

psalm-baseline.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6117,8 +6117,14 @@
61176117
<code><![CDATA[$multipleUrlParams['sql_query']]]></code>
61186118
<code>$newSortExpressionNoDirection</code>
61196119
<code><![CDATA[$oneKey['ref_db_name'] ?? $GLOBALS['db']]]></code>
6120+
<code><![CDATA[$oneKey['ref_db_name'] ?? $GLOBALS['db']]]></code>
6121+
<code><![CDATA[$oneKey['ref_index_list'][$index]]]></code>
6122+
<code><![CDATA[$oneKey['ref_table_name']]]></code>
61206123
<code><![CDATA[$oneKey['ref_table_name']]]></code>
61216124
<code><![CDATA[$rel['foreign_db']]]></code>
6125+
<code><![CDATA[$rel['foreign_db']]]></code>
6126+
<code><![CDATA[$rel['foreign_field']]]></code>
6127+
<code><![CDATA[$rel['foreign_table']]]></code>
61226128
<code><![CDATA[$rel['foreign_table']]]></code>
61236129
<code>$row[$i]</code>
61246130
<code>$row[$i]</code>
@@ -6358,9 +6364,7 @@
63586364
<code><![CDATA[$statementInfo->statement->limit->offset]]></code>
63596365
</MixedPropertyFetch>
63606366
<MixedReturnTypeCoercion>
6361-
<code>$map</code>
63626367
<code><![CDATA[[$singleSortOrder, implode(', ', $sortOrderColumns), $orderImg ?? '']]]></code>
6363-
<code><![CDATA[array<string, string[]>]]></code>
63646368
<code>string[]</code>
63656369
</MixedReturnTypeCoercion>
63666370
<PossiblyInvalidArgument>

0 commit comments

Comments
 (0)