diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0ca1317422..4b925e6f88 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -11228,8 +11228,8 @@ - uiprefs[$property]]]> - uiprefs[$property]]]> + uiprefs[$property->value]]]> + uiprefs[$property->value]]]> diff --git a/src/Controllers/Sql/ColumnPreferencesController.php b/src/Controllers/Sql/ColumnPreferencesController.php index 5623de7f49..c34dc0c76a 100644 --- a/src/Controllers/Sql/ColumnPreferencesController.php +++ b/src/Controllers/Sql/ColumnPreferencesController.php @@ -11,7 +11,7 @@ use PhpMyAdmin\Http\Response; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; -use PhpMyAdmin\Table\Table; +use PhpMyAdmin\Table\UiProperty; use function array_map; use function explode; @@ -36,14 +36,14 @@ final class ColumnPreferencesController implements InvocableController $colorder = $request->getParsedBodyParam('col_order'); if (is_string($colorder)) { $propertyValue = array_map(intval(...), explode(',', $colorder)); - $status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime); + $status = $tableObject->setUiProp(UiProperty::ColumnOrder, $propertyValue, $tableCreateTime); } // set column visibility $colvisib = $request->getParsedBodyParam('col_visib'); if ($status === true && is_string($colvisib)) { $propertyValue = array_map(intval(...), explode(',', $colvisib)); - $status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime); + $status = $tableObject->setUiProp(UiProperty::ColumnVisibility, $propertyValue, $tableCreateTime); } if ($status instanceof Message) { diff --git a/src/Controllers/Table/Structure/SaveController.php b/src/Controllers/Table/Structure/SaveController.php index 09e2427409..1df732fe06 100644 --- a/src/Controllers/Table/Structure/SaveController.php +++ b/src/Controllers/Table/Structure/SaveController.php @@ -18,6 +18,7 @@ use PhpMyAdmin\Index; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Table\Table; +use PhpMyAdmin\Table\UiProperty; use PhpMyAdmin\Transformations; use PhpMyAdmin\Url; use PhpMyAdmin\UserPrivileges; @@ -102,11 +103,11 @@ final class SaveController implements InvocableController ); // find the remembered sort expression - $sortedCol = $this->tableObj->getUiProp(Table::PROP_SORTED_COLUMN); + $sortedCol = $this->tableObj->getUiProp(UiProperty::SortedColumn); // if the old column name is part of the remembered sort expression if (str_contains((string) $sortedCol, Util::backquote($_POST['field_orig'][$i]))) { // delete the whole remembered sort expression - $this->tableObj->removeUiProp(Table::PROP_SORTED_COLUMN); + $this->tableObj->removeUiProp(UiProperty::SortedColumn); } if ( diff --git a/src/Display/Results.php b/src/Display/Results.php index 82554d150b..2dc8c2d967 100644 --- a/src/Display/Results.php +++ b/src/Display/Results.php @@ -29,6 +29,7 @@ use PhpMyAdmin\SqlParser\Utils\Query; use PhpMyAdmin\SqlParser\Utils\StatementInfo; use PhpMyAdmin\SqlParser\Utils\StatementType; use PhpMyAdmin\Table\Table; +use PhpMyAdmin\Table\UiProperty; use PhpMyAdmin\Template; use PhpMyAdmin\Theme\ThemeManager; use PhpMyAdmin\Transformations; @@ -2391,26 +2392,26 @@ class Results if ($this->isSelect($statementInfo)) { $pmatable = new Table($this->table, $this->db, $this->dbi); - $colOrder = $pmatable->getUiProp(Table::PROP_COLUMN_ORDER); + $colOrder = $pmatable->getUiProp(UiProperty::ColumnOrder); $fieldsCount = count($this->fieldsMeta); /* Validate the value */ if (is_array($colOrder)) { foreach ($colOrder as $value) { if ($value >= $fieldsCount) { - $pmatable->removeUiProp(Table::PROP_COLUMN_ORDER); + $pmatable->removeUiProp(UiProperty::ColumnOrder); break; } } if ($fieldsCount !== count($colOrder)) { - $pmatable->removeUiProp(Table::PROP_COLUMN_ORDER); + $pmatable->removeUiProp(UiProperty::ColumnOrder); $colOrder = false; } } - $colVisib = $pmatable->getUiProp(Table::PROP_COLUMN_VISIB); + $colVisib = $pmatable->getUiProp(UiProperty::ColumnVisibility); if (is_array($colVisib) && $fieldsCount !== count($colVisib)) { - $pmatable->removeUiProp(Table::PROP_COLUMN_VISIB); + $pmatable->removeUiProp(UiProperty::ColumnVisibility); $colVisib = false; } } diff --git a/src/Sql.php b/src/Sql.php index 7171c3870d..c2e69baeb9 100644 --- a/src/Sql.php +++ b/src/Sql.php @@ -24,6 +24,7 @@ use PhpMyAdmin\SqlParser\Utils\Query; use PhpMyAdmin\SqlParser\Utils\StatementInfo; use PhpMyAdmin\SqlParser\Utils\StatementType; use PhpMyAdmin\Table\Table; +use PhpMyAdmin\Table\UiProperty; use PhpMyAdmin\Utils\ForeignKey; use function __; @@ -82,7 +83,7 @@ class Sql if (! $statementInfo->flags->order) { // Retrieving the name of the column we should sort after. - $sortCol = $tableObject->getUiProp(Table::PROP_SORTED_COLUMN); + $sortCol = $tableObject->getUiProp(UiProperty::SortedColumn); if (empty($sortCol)) { return $statementInfo; } @@ -106,7 +107,7 @@ class Sql } else { // Store the remembered table into session. $tableObject->setUiProp( - Table::PROP_SORTED_COLUMN, + UiProperty::SortedColumn, Query::getClause( $statementInfo->statement, $statementInfo->parser->list, diff --git a/src/Table/Table.php b/src/Table/Table.php index 768636ec5e..f43741f8c7 100644 --- a/src/Table/Table.php +++ b/src/Table/Table.php @@ -72,13 +72,6 @@ use function trim; */ class Table implements Stringable { - /** - * UI preferences properties - */ - public const PROP_SORTED_COLUMN = 'sorted_col'; - public const PROP_COLUMN_ORDER = 'col_order'; - public const PROP_COLUMN_VISIB = 'col_visib'; - /** @var mixed[] UI preferences */ public array $uiprefs = []; @@ -1677,28 +1670,22 @@ class Table implements Stringable /** * Get a property from UI preferences. * Return false if the property is not found. - * Available property: - * - PROP_SORTED_COLUMN - * - PROP_COLUMN_ORDER - * - PROP_COLUMN_VISIB - * - * @param string $property property */ - public function getUiProp(string $property): mixed + public function getUiProp(UiProperty $property): mixed { if ($this->uiprefs === []) { $this->loadUiPrefs(); } // do checking based on property - if ($property === self::PROP_SORTED_COLUMN) { - if (! isset($this->uiprefs[$property])) { + if ($property === UiProperty::SortedColumn) { + if (! isset($this->uiprefs[$property->value])) { return false; } if (! isset($_POST['discard_remembered_sort'])) { // check if the column name exists in this table - $tmp = explode(' ', $this->uiprefs[$property]); + $tmp = explode(' ', $this->uiprefs[$property->value]); $colname = $tmp[0]; //remove backquoting from colname $colname = str_replace('`', '', $colname); @@ -1708,7 +1695,7 @@ class Table implements Stringable foreach ($availColumns as $eachCol) { // check if $each_col ends with $colname if (substr_compare($eachCol, $colname, mb_strlen($eachCol) - mb_strlen($colname)) === 0) { - return $this->uiprefs[$property]; + return $this->uiprefs[$property->value]; } } } @@ -1719,47 +1706,43 @@ class Table implements Stringable return false; } - if ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB) { - if ($this->isView() || ! isset($this->uiprefs[$property])) { - return false; - } - - // check if the table has not been modified - if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) { - return array_map(intval(...), $this->uiprefs[$property]); - } - - // remove the property, since the table has been modified - $this->removeUiProp($property); - + if ($this->isView() || ! isset($this->uiprefs[$property->value])) { return false; } - // default behaviour for other property: - return $this->uiprefs[$property] ?? false; + // check if the table has not been modified + if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) { + return array_map(intval(...), $this->uiprefs[$property->value]); + } + + // remove the property, since the table has been modified + $this->removeUiProp($property); + + return false; } /** * Set a property from UI preferences. * If pmadb and table_uiprefs is set, it will save the UI preferences to * phpMyAdmin database. - * Available property: - * - PROP_SORTED_COLUMN - * - PROP_COLUMN_ORDER - * - PROP_COLUMN_VISIB * - * @param string $property Property - * @param mixed $value Value for the property - * @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB + * @param int[]|string $value Value for the property + * @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB */ - public function setUiProp(string $property, mixed $value, string|null $tableCreateTime = null): bool|Message - { + public function setUiProp( + UiProperty $property, + array|string $value, + string|null $tableCreateTime = null, + ): bool|Message { if ($this->uiprefs === []) { $this->loadUiPrefs(); } // we want to save the create time if the property is PROP_COLUMN_ORDER - if (! $this->isView() && ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB)) { + if ( + ! $this->isView() + && ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility) + ) { $currCreateTime = $this->getStatusInfo('CREATE_TIME'); if ($tableCreateTime === null || $tableCreateTime != $currCreateTime) { // there is no $table_create_time, or @@ -1772,7 +1755,7 @@ class Table implements Stringable 'not be persistent after you refresh this page. ' . 'Please check if the table structure has been changed.', ), - $property, + $property->value, ), ); } @@ -1781,7 +1764,7 @@ class Table implements Stringable } // save the value - $this->uiprefs[$property] = $value; + $this->uiprefs[$property->value] = $value; // check if pmadb is set $uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature; @@ -1795,18 +1778,16 @@ class Table implements Stringable /** * Remove a property from UI preferences. * - * @param string $property the property - * * @return true|Message */ - public function removeUiProp(string $property): bool|Message + public function removeUiProp(UiProperty $property): bool|Message { if ($this->uiprefs === []) { $this->loadUiPrefs(); } - if (isset($this->uiprefs[$property])) { - unset($this->uiprefs[$property]); + if (isset($this->uiprefs[$property->value])) { + unset($this->uiprefs[$property->value]); // check if pmadb is set $uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature; diff --git a/src/Table/UiProperty.php b/src/Table/UiProperty.php new file mode 100644 index 0000000000..77374e2e73 --- /dev/null +++ b/src/Table/UiProperty.php @@ -0,0 +1,12 @@ +mockedDbi); - $property = Table::PROP_COLUMN_ORDER; + $property = UiProperty::ColumnOrder; $value = 'UiProp_value'; $tableCreateTime = null; $table->setUiProp($property, $value, $tableCreateTime); //set UI prop successfully - self::assertSame($value, $table->uiprefs[$property]); + self::assertSame($value, $table->uiprefs[$property->value]); //removeUiProp $table->removeUiProp($property); - $isDefineProperty = isset($table->uiprefs[$property]); + $isDefineProperty = isset($table->uiprefs[$property->value]); self::assertFalse($isDefineProperty); //getUiProp after removeUiProp