Skip to content

Commit

Permalink
Merge pull request #19144 from kamil-tekiela/UiProperty
Browse files Browse the repository at this point in the history
Add UiProperty enum
  • Loading branch information
MauricioFauth committed May 8, 2024
2 parents 7ba6d98 + 24e6089 commit 2a58575
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 68 deletions.
4 changes: 2 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11228,8 +11228,8 @@
<code><![CDATA[$oneField]]></code>
<code><![CDATA[$options['expr']]]></code>
<code><![CDATA[$row['Type']]]></code>
<code><![CDATA[$this->uiprefs[$property]]]></code>
<code><![CDATA[$this->uiprefs[$property]]]></code>
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
<code><![CDATA[$value]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/Sql/ColumnPreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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;
Expand All @@ -36,14 +36,14 @@ public function __invoke(ServerRequest $request): Response|null
$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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Controllers/Table/Structure/SaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPrivileges;
Expand Down Expand Up @@ -102,11 +103,11 @@ private function updateColumns(UserPrivileges $userPrivileges): bool
);

// 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]))) {

Check warning on line 108 in src/Controllers/Table/Structure/SaveController.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ // find the remembered sort expression $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]))) { + if (!str_contains((string) $sortedCol, Util::backquote($_POST['field_orig'][$i]))) { // delete the whole remembered sort expression $this->tableObj->removeUiProp(UiProperty::SortedColumn); }
// delete the whole remembered sort expression
$this->tableObj->removeUiProp(Table::PROP_SORTED_COLUMN);
$this->tableObj->removeUiProp(UiProperty::SortedColumn);
}

if (
Expand Down
11 changes: 6 additions & 5 deletions src/Display/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -2391,26 +2392,26 @@ private function getColumnParams(StatementInfo $statementInfo): array

if ($this->isSelect($statementInfo)) {

Check warning on line 2393 in src/Display/Results.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ { $colOrder = false; $colVisib = false; - if ($this->isSelect($statementInfo)) { + if (!$this->isSelect($statementInfo)) { $pmatable = new Table($this->table, $this->db, $this->dbi); $colOrder = $pmatable->getUiProp(UiProperty::ColumnOrder); $fieldsCount = count($this->fieldsMeta);
$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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PhpMyAdmin\SqlParser\Utils\StatementInfo;
use PhpMyAdmin\SqlParser\Utils\StatementType;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Utils\ForeignKey;

use function __;
Expand Down Expand Up @@ -82,7 +83,7 @@ private function handleSortOrder(

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;
}
Expand All @@ -106,7 +107,7 @@ private function handleSortOrder(
} else {
// Store the remembered table into session.
$tableObject->setUiProp(
Table::PROP_SORTED_COLUMN,
UiProperty::SortedColumn,
Query::getClause(
$statementInfo->statement,
$statementInfo->parser->list,
Expand Down
83 changes: 32 additions & 51 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@
*/
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 = [];

Expand Down Expand Up @@ -1677,28 +1670,22 @@ protected function loadUiPrefs(): void
/**
* 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) {

Check warning on line 1681 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $this->loadUiPrefs(); } // do checking based on property - if ($property === UiProperty::SortedColumn) { + if ($property !== UiProperty::SortedColumn) { if (!isset($this->uiprefs[$property->value])) { return false; }
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);
Expand All @@ -1708,7 +1695,7 @@ public function getUiProp(string $property): mixed
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];
}
}
}
Expand All @@ -1719,47 +1706,43 @@ public function getUiProp(string $property): mixed
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])) {

Check warning on line 1709 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "LogicalNot": --- Original +++ New @@ @@ $this->removeUiProp($property); return false; } - if ($this->isView() || !isset($this->uiprefs[$property->value])) { + if ($this->isView() || isset($this->uiprefs[$property->value])) { return false; } // check if the table has not been modified

Check warning on line 1709 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ $this->removeUiProp($property); return false; } - if ($this->isView() || !isset($this->uiprefs[$property->value])) { + if ($this->isView() && !isset($this->uiprefs[$property->value])) { return false; } // check if the table has not been modified

Check warning on line 1709 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ $this->removeUiProp($property); return false; } - if ($this->isView() || !isset($this->uiprefs[$property->value])) { + if (!$this->isView() || !isset($this->uiprefs[$property->value])) { return false; } // check if the table has not been modified
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()

Check warning on line 1743 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": --- Original +++ New @@ @@ $this->loadUiPrefs(); } // we want to save the create time if the property is PROP_COLUMN_ORDER - if (!$this->isView() && ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)) { + 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
&& ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)

Check warning on line 1744 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $this->loadUiPrefs(); } // we want to save the create time if the property is PROP_COLUMN_ORDER - if (!$this->isView() && ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)) { + 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

Check warning on line 1744 in src/Table/Table.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $this->loadUiPrefs(); } // we want to save the create time if the property is PROP_COLUMN_ORDER - if (!$this->isView() && ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)) { + 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
) {
$currCreateTime = $this->getStatusInfo('CREATE_TIME');
if ($tableCreateTime === null || $tableCreateTime != $currCreateTime) {
// there is no $table_create_time, or
Expand All @@ -1772,7 +1755,7 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
'not be persistent after you refresh this page. ' .
'Please check if the table structure has been changed.',
),
$property,
$property->value,
),
);
}
Expand All @@ -1781,7 +1764,7 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
}

// save the value
$this->uiprefs[$property] = $value;
$this->uiprefs[$property->value] = $value;

// check if pmadb is set
$uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature;
Expand All @@ -1795,18 +1778,16 @@ public function setUiProp(string $property, mixed $value, string|null $tableCrea
/**
* 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;
Expand Down
12 changes: 12 additions & 0 deletions src/Table/UiProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Table;

enum UiProperty: string
{
case SortedColumn = 'sorted_col';
case ColumnOrder = 'col_order';
case ColumnVisibility = 'col_visib';
}
7 changes: 4 additions & 3 deletions tests/unit/Table/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpMyAdmin\Query\Cache;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\FieldHelper;
use PhpMyAdmin\Tests\Stubs\DbiDummy;
Expand Down Expand Up @@ -1281,17 +1282,17 @@ public function testSetUiProp(): void

$table = new Table($tableName, $db, $this->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
Expand Down

0 comments on commit 2a58575

Please sign in to comment.