Skip to content

Commit 2a58575

Browse files
Merge pull request #19144 from kamil-tekiela/UiProperty
Add UiProperty enum
2 parents 7ba6d98 + 24e6089 commit 2a58575

8 files changed

Lines changed: 65 additions & 68 deletions

File tree

psalm-baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11228,8 +11228,8 @@
1122811228
<code><![CDATA[$oneField]]></code>
1122911229
<code><![CDATA[$options['expr']]]></code>
1123011230
<code><![CDATA[$row['Type']]]></code>
11231-
<code><![CDATA[$this->uiprefs[$property]]]></code>
11232-
<code><![CDATA[$this->uiprefs[$property]]]></code>
11231+
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
11232+
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
1123311233
<code><![CDATA[$value]]></code>
1123411234
</MixedArgument>
1123511235
<MixedArgumentTypeCoercion>

src/Controllers/Sql/ColumnPreferencesController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpMyAdmin\Http\ServerRequest;
1212
use PhpMyAdmin\Message;
1313
use PhpMyAdmin\ResponseRenderer;
14-
use PhpMyAdmin\Table\Table;
14+
use PhpMyAdmin\Table\UiProperty;
1515

1616
use function array_map;
1717
use function explode;
@@ -36,14 +36,14 @@ public function __invoke(ServerRequest $request): Response|null
3636
$colorder = $request->getParsedBodyParam('col_order');
3737
if (is_string($colorder)) {
3838
$propertyValue = array_map(intval(...), explode(',', $colorder));
39-
$status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime);
39+
$status = $tableObject->setUiProp(UiProperty::ColumnOrder, $propertyValue, $tableCreateTime);
4040
}
4141

4242
// set column visibility
4343
$colvisib = $request->getParsedBodyParam('col_visib');
4444
if ($status === true && is_string($colvisib)) {
4545
$propertyValue = array_map(intval(...), explode(',', $colvisib));
46-
$status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime);
46+
$status = $tableObject->setUiProp(UiProperty::ColumnVisibility, $propertyValue, $tableCreateTime);
4747
}
4848

4949
if ($status instanceof Message) {

src/Controllers/Table/Structure/SaveController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpMyAdmin\Message;
1919
use PhpMyAdmin\ResponseRenderer;
2020
use PhpMyAdmin\Table\Table;
21+
use PhpMyAdmin\Table\UiProperty;
2122
use PhpMyAdmin\Transformations;
2223
use PhpMyAdmin\Url;
2324
use PhpMyAdmin\UserPrivileges;
@@ -102,11 +103,11 @@ private function updateColumns(UserPrivileges $userPrivileges): bool
102103
);
103104

104105
// find the remembered sort expression
105-
$sortedCol = $this->tableObj->getUiProp(Table::PROP_SORTED_COLUMN);
106+
$sortedCol = $this->tableObj->getUiProp(UiProperty::SortedColumn);
106107
// if the old column name is part of the remembered sort expression
107108
if (str_contains((string) $sortedCol, Util::backquote($_POST['field_orig'][$i]))) {
108109
// delete the whole remembered sort expression
109-
$this->tableObj->removeUiProp(Table::PROP_SORTED_COLUMN);
110+
$this->tableObj->removeUiProp(UiProperty::SortedColumn);
110111
}
111112

112113
if (

src/Display/Results.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PhpMyAdmin\SqlParser\Utils\StatementInfo;
3030
use PhpMyAdmin\SqlParser\Utils\StatementType;
3131
use PhpMyAdmin\Table\Table;
32+
use PhpMyAdmin\Table\UiProperty;
3233
use PhpMyAdmin\Template;
3334
use PhpMyAdmin\Theme\ThemeManager;
3435
use PhpMyAdmin\Transformations;
@@ -2391,26 +2392,26 @@ private function getColumnParams(StatementInfo $statementInfo): array
23912392

23922393
if ($this->isSelect($statementInfo)) {
23932394
$pmatable = new Table($this->table, $this->db, $this->dbi);
2394-
$colOrder = $pmatable->getUiProp(Table::PROP_COLUMN_ORDER);
2395+
$colOrder = $pmatable->getUiProp(UiProperty::ColumnOrder);
23952396
$fieldsCount = count($this->fieldsMeta);
23962397
/* Validate the value */
23972398
if (is_array($colOrder)) {
23982399
foreach ($colOrder as $value) {
23992400
if ($value >= $fieldsCount) {
2400-
$pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
2401+
$pmatable->removeUiProp(UiProperty::ColumnOrder);
24012402
break;
24022403
}
24032404
}
24042405

24052406
if ($fieldsCount !== count($colOrder)) {
2406-
$pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
2407+
$pmatable->removeUiProp(UiProperty::ColumnOrder);
24072408
$colOrder = false;
24082409
}
24092410
}
24102411

2411-
$colVisib = $pmatable->getUiProp(Table::PROP_COLUMN_VISIB);
2412+
$colVisib = $pmatable->getUiProp(UiProperty::ColumnVisibility);
24122413
if (is_array($colVisib) && $fieldsCount !== count($colVisib)) {
2413-
$pmatable->removeUiProp(Table::PROP_COLUMN_VISIB);
2414+
$pmatable->removeUiProp(UiProperty::ColumnVisibility);
24142415
$colVisib = false;
24152416
}
24162417
}

src/Sql.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PhpMyAdmin\SqlParser\Utils\StatementInfo;
2525
use PhpMyAdmin\SqlParser\Utils\StatementType;
2626
use PhpMyAdmin\Table\Table;
27+
use PhpMyAdmin\Table\UiProperty;
2728
use PhpMyAdmin\Utils\ForeignKey;
2829

2930
use function __;
@@ -82,7 +83,7 @@ private function handleSortOrder(
8283

8384
if (! $statementInfo->flags->order) {
8485
// Retrieving the name of the column we should sort after.
85-
$sortCol = $tableObject->getUiProp(Table::PROP_SORTED_COLUMN);
86+
$sortCol = $tableObject->getUiProp(UiProperty::SortedColumn);
8687
if (empty($sortCol)) {
8788
return $statementInfo;
8889
}
@@ -106,7 +107,7 @@ private function handleSortOrder(
106107
} else {
107108
// Store the remembered table into session.
108109
$tableObject->setUiProp(
109-
Table::PROP_SORTED_COLUMN,
110+
UiProperty::SortedColumn,
110111
Query::getClause(
111112
$statementInfo->statement,
112113
$statementInfo->parser->list,

src/Table/Table.php

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@
7272
*/
7373
class Table implements Stringable
7474
{
75-
/**
76-
* UI preferences properties
77-
*/
78-
public const PROP_SORTED_COLUMN = 'sorted_col';
79-
public const PROP_COLUMN_ORDER = 'col_order';
80-
public const PROP_COLUMN_VISIB = 'col_visib';
81-
8275
/** @var mixed[] UI preferences */
8376
public array $uiprefs = [];
8477

@@ -1677,28 +1670,22 @@ protected function loadUiPrefs(): void
16771670
/**
16781671
* Get a property from UI preferences.
16791672
* Return false if the property is not found.
1680-
* Available property:
1681-
* - PROP_SORTED_COLUMN
1682-
* - PROP_COLUMN_ORDER
1683-
* - PROP_COLUMN_VISIB
1684-
*
1685-
* @param string $property property
16861673
*/
1687-
public function getUiProp(string $property): mixed
1674+
public function getUiProp(UiProperty $property): mixed
16881675
{
16891676
if ($this->uiprefs === []) {
16901677
$this->loadUiPrefs();
16911678
}
16921679

16931680
// do checking based on property
1694-
if ($property === self::PROP_SORTED_COLUMN) {
1695-
if (! isset($this->uiprefs[$property])) {
1681+
if ($property === UiProperty::SortedColumn) {
1682+
if (! isset($this->uiprefs[$property->value])) {
16961683
return false;
16971684
}
16981685

16991686
if (! isset($_POST['discard_remembered_sort'])) {
17001687
// check if the column name exists in this table
1701-
$tmp = explode(' ', $this->uiprefs[$property]);
1688+
$tmp = explode(' ', $this->uiprefs[$property->value]);
17021689
$colname = $tmp[0];
17031690
//remove backquoting from colname
17041691
$colname = str_replace('`', '', $colname);
@@ -1708,7 +1695,7 @@ public function getUiProp(string $property): mixed
17081695
foreach ($availColumns as $eachCol) {
17091696
// check if $each_col ends with $colname
17101697
if (substr_compare($eachCol, $colname, mb_strlen($eachCol) - mb_strlen($colname)) === 0) {
1711-
return $this->uiprefs[$property];
1698+
return $this->uiprefs[$property->value];
17121699
}
17131700
}
17141701
}
@@ -1719,47 +1706,43 @@ public function getUiProp(string $property): mixed
17191706
return false;
17201707
}
17211708

1722-
if ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB) {
1723-
if ($this->isView() || ! isset($this->uiprefs[$property])) {
1724-
return false;
1725-
}
1726-
1727-
// check if the table has not been modified
1728-
if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) {
1729-
return array_map(intval(...), $this->uiprefs[$property]);
1730-
}
1731-
1732-
// remove the property, since the table has been modified
1733-
$this->removeUiProp($property);
1734-
1709+
if ($this->isView() || ! isset($this->uiprefs[$property->value])) {
17351710
return false;
17361711
}
17371712

1738-
// default behaviour for other property:
1739-
return $this->uiprefs[$property] ?? false;
1713+
// check if the table has not been modified
1714+
if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) {
1715+
return array_map(intval(...), $this->uiprefs[$property->value]);
1716+
}
1717+
1718+
// remove the property, since the table has been modified
1719+
$this->removeUiProp($property);
1720+
1721+
return false;
17401722
}
17411723

17421724
/**
17431725
* Set a property from UI preferences.
17441726
* If pmadb and table_uiprefs is set, it will save the UI preferences to
17451727
* phpMyAdmin database.
1746-
* Available property:
1747-
* - PROP_SORTED_COLUMN
1748-
* - PROP_COLUMN_ORDER
1749-
* - PROP_COLUMN_VISIB
1750-
*
1751-
* @param string $property Property
1752-
* @param mixed $value Value for the property
1753-
* @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB
1728+
*
1729+
* @param int[]|string $value Value for the property
1730+
* @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB
17541731
*/
1755-
public function setUiProp(string $property, mixed $value, string|null $tableCreateTime = null): bool|Message
1756-
{
1732+
public function setUiProp(
1733+
UiProperty $property,
1734+
array|string $value,
1735+
string|null $tableCreateTime = null,
1736+
): bool|Message {
17571737
if ($this->uiprefs === []) {
17581738
$this->loadUiPrefs();
17591739
}
17601740

17611741
// we want to save the create time if the property is PROP_COLUMN_ORDER
1762-
if (! $this->isView() && ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB)) {
1742+
if (
1743+
! $this->isView()
1744+
&& ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)
1745+
) {
17631746
$currCreateTime = $this->getStatusInfo('CREATE_TIME');
17641747
if ($tableCreateTime === null || $tableCreateTime != $currCreateTime) {
17651748
// there is no $table_create_time, or
@@ -1772,7 +1755,7 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
17721755
'not be persistent after you refresh this page. ' .
17731756
'Please check if the table structure has been changed.',
17741757
),
1775-
$property,
1758+
$property->value,
17761759
),
17771760
);
17781761
}
@@ -1781,7 +1764,7 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
17811764
}
17821765

17831766
// save the value
1784-
$this->uiprefs[$property] = $value;
1767+
$this->uiprefs[$property->value] = $value;
17851768

17861769
// check if pmadb is set
17871770
$uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature;
@@ -1795,18 +1778,16 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
17951778
/**
17961779
* Remove a property from UI preferences.
17971780
*
1798-
* @param string $property the property
1799-
*
18001781
* @return true|Message
18011782
*/
1802-
public function removeUiProp(string $property): bool|Message
1783+
public function removeUiProp(UiProperty $property): bool|Message
18031784
{
18041785
if ($this->uiprefs === []) {
18051786
$this->loadUiPrefs();
18061787
}
18071788

1808-
if (isset($this->uiprefs[$property])) {
1809-
unset($this->uiprefs[$property]);
1789+
if (isset($this->uiprefs[$property->value])) {
1790+
unset($this->uiprefs[$property->value]);
18101791

18111792
// check if pmadb is set
18121793
$uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature;

src/Table/UiProperty.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Table;
6+
7+
enum UiProperty: string
8+
{
9+
case SortedColumn = 'sorted_col';
10+
case ColumnOrder = 'col_order';
11+
case ColumnVisibility = 'col_visib';
12+
}

tests/unit/Table/TableTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMyAdmin\Query\Cache;
1414
use PhpMyAdmin\SqlParser\Context;
1515
use PhpMyAdmin\Table\Table;
16+
use PhpMyAdmin\Table\UiProperty;
1617
use PhpMyAdmin\Tests\AbstractTestCase;
1718
use PhpMyAdmin\Tests\FieldHelper;
1819
use PhpMyAdmin\Tests\Stubs\DbiDummy;
@@ -1281,17 +1282,17 @@ public function testSetUiProp(): void
12811282

12821283
$table = new Table($tableName, $db, $this->mockedDbi);
12831284

1284-
$property = Table::PROP_COLUMN_ORDER;
1285+
$property = UiProperty::ColumnOrder;
12851286
$value = 'UiProp_value';
12861287
$tableCreateTime = null;
12871288
$table->setUiProp($property, $value, $tableCreateTime);
12881289

12891290
//set UI prop successfully
1290-
self::assertSame($value, $table->uiprefs[$property]);
1291+
self::assertSame($value, $table->uiprefs[$property->value]);
12911292

12921293
//removeUiProp
12931294
$table->removeUiProp($property);
1294-
$isDefineProperty = isset($table->uiprefs[$property]);
1295+
$isDefineProperty = isset($table->uiprefs[$property->value]);
12951296
self::assertFalse($isDefineProperty);
12961297

12971298
//getUiProp after removeUiProp

0 commit comments

Comments
 (0)