Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19042 from MauricioFauth/init-extractio…
Browse files Browse the repository at this point in the history
…n-from-controllers

Extracts loadTableInfo method from controller constructors
  • Loading branch information
MauricioFauth authored Mar 9, 2024
2 parents c8654bf + 0b07402 commit 38bd9dd
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 329 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4970,11 +4970,6 @@ parameters:
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Method PhpMyAdmin\\\\Controllers\\\\Table\\\\SearchController\\:\\:getColumnMinMax\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Only booleans are allowed in &&, array given on the left side\\.$#"
count: 1
Expand All @@ -5000,21 +4995,11 @@ parameters:
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#"
count: 2
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#1 \\$params of static method PhpMyAdmin\\\\Url\\:\\:getCommon\\(\\) expects array\\<string, bool\\|int\\|string\\>, mixed given\\.$#"
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#1 \\$sqlQuery of static method PhpMyAdmin\\\\Core\\:\\:checkSqlQuerySignature\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#1 \\$string of function strtoupper expects string, string\\|null given\\.$#"
count: 1
Expand All @@ -5035,11 +5020,6 @@ parameters:
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#2 \\$signature of static method PhpMyAdmin\\\\Core\\:\\:checkSqlQuerySignature\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Table/SearchController.php

-
message: "#^Parameter \\#3 \\$selectedOperator of method PhpMyAdmin\\\\Types\\:\\:getTypeOperatorsHtml\\(\\) expects string\\|null, mixed given\\.$#"
count: 1
Expand Down
24 changes: 0 additions & 24 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3628,23 +3628,11 @@
</MixedAssignment>
<PossiblyInvalidArgument>
<code><![CDATA[$_POST['column']]]></code>
<code><![CDATA[$_POST['db']]]></code>
<code><![CDATA[$_POST['table']]]></code>
<code><![CDATA[$_POST['where_clause']]]></code>
<code><![CDATA[$_POST['where_clause_sign']]]></code>
<code><![CDATA[$selectedOperator]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$_POST['column']]]></code>
<code><![CDATA[$_POST['where_clause']]]></code>
<code><![CDATA[$_POST['where_clause_sign']]]></code>
</PossiblyInvalidCast>
<PossiblyInvalidOperand>
<code><![CDATA[$_POST['where_clause']]]></code>
</PossiblyInvalidOperand>
<PossiblyUnusedMethod>
<code><![CDATA[getColumnProperties]]></code>
</PossiblyUnusedMethod>
<RedundantCastGivenDocblockType>
<code><![CDATA[(int) $config->settings['MaxRows']]]></code>
</RedundantCastGivenDocblockType>
Expand Down Expand Up @@ -13035,13 +13023,6 @@
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/FindReplaceControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/ImportControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
Expand Down Expand Up @@ -13135,11 +13116,6 @@
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/SearchControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Controllers/Table/SqlControllerTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
Expand Down
13 changes: 6 additions & 7 deletions resources/templates/table/search/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</tr>
</thead>
<tbody>
{% for column_index in 0..column_names|length - 1 %}
{% for column_index in column_names|keys %}
<tr class="noclick">
{# If 'Function' column is present trying to change comment #}
{% if geom_column_flag %}
Expand All @@ -69,19 +69,18 @@
{#- Keep this without extra spaces because it is used for a request to build the BETWEEN modal -#}
{{- column_names[column_index] -}}
</th>
{% set properties = self.getColumnProperties(column_index, column_index) %}
<td dir="ltr">
{{ properties['type'] }}
{{ properties[column_index]['type'] }}
</td>
<td>
{{ properties['collation'] }}
{{ properties[column_index]['collation'] }}
</td>
<td>
{{ properties['func']|raw }}
{{ properties[column_index]['func']|raw }}
</td>
{# here, the data-type attribute is needed for a date/time picker #}
<td data-type="{{ properties['type'] }}">
{{ properties['value']|raw }}
<td data-type="{{ properties[column_index]['type'] }}">
{{ properties[column_index]['value']|raw }}
{# Displays hidden fields #}
<input type="hidden" name="criteriaColumnNames[{{ column_index }}]" value="{{ column_names[column_index] }}">
<input type="hidden" name="criteriaColumnTypes[{{ column_index }}]" value="{{ column_types[column_index] }}">
Expand Down
10 changes: 4 additions & 6 deletions resources/templates/table/zoom_search/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@
</select>
</th>
{% if criteria_column_names[i] is defined and criteria_column_names[i] != 'pma_null' %}
{% set key = keys[criteria_column_names[i]] %}
{% set properties = self.getColumnProperties(i, key) %}
{% set type = type|merge({(i): properties['type']}) %}
{% set collation = collation|merge({(i): properties['collation']}) %}
{% set func = func|merge({(i): properties['func']}) %}
{% set value = value|merge({(i): properties['value']}) %}
{% set type = type|merge({(i): properties[i]['type']}) %}
{% set collation = collation|merge({(i): properties[i]['collation']}) %}
{% set func = func|merge({(i): properties[i]['func']}) %}
{% set value = value|merge({(i): properties[i]['value']}) %}
{% endif %}
{# Column type #}
<td dir="ltr">
Expand Down
18 changes: 8 additions & 10 deletions src/Controllers/Table/FindReplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ class FindReplaceController extends AbstractController
/** @var mixed[] */
private array $columnTypes = [];

private string $connectionCharSet;

public function __construct(
ResponseRenderer $response,
Template $template,
private DatabaseInterface $dbi,
private readonly DbTableExists $dbTableExists,
) {
parent::__construct($response, $template);

$this->loadTableInfo();
$this->connectionCharSet = (string) $this->dbi->fetchValue('SELECT @@character_set_connection');
}

public function __invoke(ServerRequest $request): void
Expand Down Expand Up @@ -99,13 +94,16 @@ public function __invoke(ServerRequest $request): void
return;
}

$this->loadTableInfo();
$connectionCharSet = (string) $this->dbi->fetchValue('SELECT @@character_set_connection');

Check warning on line 98 in src/Controllers/Table/FindReplaceController.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ return; } $this->loadTableInfo(); - $connectionCharSet = (string) $this->dbi->fetchValue('SELECT @@character_set_connection'); + $connectionCharSet = $this->dbi->fetchValue('SELECT @@character_set_connection'); $useRegex = (bool) $request->getParsedBodyParam('useRegex'); $replaceWith = (string) $request->getParsedBodyParam('replaceWith'); $columnIndex = (int) $request->getParsedBodyParam('columnIndex');

$useRegex = (bool) $request->getParsedBodyParam('useRegex');
$replaceWith = (string) $request->getParsedBodyParam('replaceWith');
$columnIndex = (int) $request->getParsedBodyParam('columnIndex');

if ($request->hasBodyParam('find')) {
$find = (string) $request->getParsedBodyParam('find');
$preview = $this->getReplacePreview($columnIndex, $find, $replaceWith, $useRegex, $this->connectionCharSet);
$preview = $this->getReplacePreview($columnIndex, $find, $replaceWith, $useRegex, $connectionCharSet);
$this->response->addJSON('preview', $preview);

return;
Expand All @@ -115,7 +113,7 @@ public function __invoke(ServerRequest $request): void

if ($request->hasBodyParam('replace')) {
$findString = (string) $request->getParsedBodyParam('findString');
$this->replace($columnIndex, $findString, $replaceWith, $useRegex, $this->connectionCharSet);
$this->replace($columnIndex, $findString, $replaceWith, $useRegex, $connectionCharSet);
$this->response->addHTML(
Generator::getMessage(
__('Your SQL query has been executed successfully.'),
Expand Down Expand Up @@ -168,7 +166,7 @@ private function loadTableInfo(): void
/**
* Display selection form action
*/
public function displaySelectionFormAction(): void
private function displaySelectionFormAction(): void
{
if (! isset($GLOBALS['goto'])) {
$GLOBALS['goto'] = Util::getScriptNameForOption(
Expand Down Expand Up @@ -203,7 +201,7 @@ public function displaySelectionFormAction(): void
*
* @return string HTML for previewing strings found and their replacements
*/
public function getReplacePreview(
private function getReplacePreview(
int $columnIndex,
string $find,
string $replaceWith,
Expand Down Expand Up @@ -303,7 +301,7 @@ private function getRegexReplaceRows(
* @param bool $useRegex to use Regex replace or not
* @param string $charSet character set of the connection
*/
public function replace(
private function replace(
int $columnIndex,
string $find,
string $replaceWith,
Expand Down
56 changes: 16 additions & 40 deletions src/Controllers/Table/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\ConfigStorage\RelationCleanup;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\DbTableExists;
Expand All @@ -28,6 +27,7 @@
use PhpMyAdmin\Utils\Gis;

use function __;
use function array_keys;
use function in_array;
use function is_array;
use function mb_strtolower;
Expand Down Expand Up @@ -97,8 +97,6 @@ public function __construct(
private readonly DbTableExists $dbTableExists,
) {
parent::__construct($response, $template);

$this->loadTableInfo();
}

/**
Expand Down Expand Up @@ -198,6 +196,8 @@ public function __invoke(ServerRequest $request): void
return;
}

$this->loadTableInfo();

Check warning on line 199 in src/Controllers/Table/SearchController.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $this->redirect('/', ['reload' => true, 'message' => __('No table selected.')]); return; } - $this->loadTableInfo(); + $this->addScriptFiles(['makegrid.js', 'sql.js', 'table/select.js', 'table/change.js', 'vendor/jquery/jquery.uitablefilter.js', 'gis_data_editor.js']); if (isset($_POST['range_search'])) { $this->rangeSearchAction();

$this->addScriptFiles([
'makegrid.js',
'sql.js',
Expand All @@ -223,41 +223,10 @@ public function __invoke(ServerRequest $request): void
}
}

/**
* Get data row action
*/
public function getDataRowAction(): void
{
if (! Core::checkSqlQuerySignature($_POST['where_clause'], $_POST['where_clause_sign'])) {
return;
}

$extraData = [];
$rowInfoQuery = 'SELECT * FROM ' . Util::backquote($_POST['db']) . '.'
. Util::backquote($_POST['table']) . ' WHERE ' . $_POST['where_clause'];
$result = $this->dbi->query($rowInfoQuery . ';');
$fieldsMeta = $this->dbi->getFieldsMeta($result);
while ($row = $result->fetchAssoc()) {
// for bit fields we need to convert them to printable form
$i = 0;
foreach ($row as $col => $val) {
if (isset($fieldsMeta[$i]) && $fieldsMeta[$i]->isMappedTypeBit) {
$row[$col] = Util::printableBitValue((int) $val, $fieldsMeta[$i]->length);
}

$i++;
}

$extraData['row_info'] = $row;
}

$this->response->addJSON($extraData);
}

/**
* Do selection action
*/
public function doSelectionAction(): void
private function doSelectionAction(): void
{
/**
* Selection criteria have been submitted -> do the work
Expand Down Expand Up @@ -297,18 +266,23 @@ public function doSelectionAction(): void
/**
* Display selection form action
*/
public function displaySelectionFormAction(): void
private function displaySelectionFormAction(): void
{
$config = Config::getInstance();
if (! isset($GLOBALS['goto'])) {
$GLOBALS['goto'] = Util::getScriptNameForOption($config->settings['DefaultTabTable'], 'table');
}

$properties = [];
foreach (array_keys($this->columnNames) as $columnIndex) {
$properties[$columnIndex] = $this->getColumnProperties($columnIndex, $columnIndex);
}

$this->render('table/search/index', [
'db' => Current::$database,
'table' => Current::$table,
'goto' => $GLOBALS['goto'],
'self' => $this,
'properties' => $properties,
'geom_column_flag' => $this->geomColumnFlag,
'column_names' => $this->columnNames,
'column_types' => $this->columnTypes,
Expand All @@ -321,7 +295,7 @@ public function displaySelectionFormAction(): void
/**
* Range search action
*/
public function rangeSearchAction(): void
private function rangeSearchAction(): void
{
$minMax = $this->getColumnMinMax($_POST['column']);
$this->response->addJSON('column_data', $minMax);
Expand All @@ -331,8 +305,10 @@ public function rangeSearchAction(): void
* Finds minimum and maximum value of a given column.
*
* @param string $column Column name
*
* @return mixed[]|null
*/
public function getColumnMinMax(string $column): array|null
private function getColumnMinMax(string $column): array|null
{
$sqlQuery = 'SELECT MIN(' . Util::backquote($column) . ') AS `min`, '
. 'MAX(' . Util::backquote($column) . ') AS `max` '
Expand All @@ -351,7 +327,7 @@ public function getColumnMinMax(string $column): array|null
*
* @return mixed[] Array containing column's properties
*/
public function getColumnProperties(int $searchIndex, int $columnIndex): array
private function getColumnProperties(int $searchIndex, int $columnIndex): array
{
$selectedOperator = $_POST['criteriaColumnOperators'][$searchIndex] ?? '';
$enteredValue = $_POST['criteriaValues'] ?? '';
Expand Down
Loading

0 comments on commit 38bd9dd

Please sign in to comment.