diff options
author | MaurĂcio Meneghini Fauth <mauricio@fauth.dev> | 2022-11-06 23:01:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-06 23:01:07 +0300 |
commit | 456be438ea2f000988d4fb654d6159a127edc24c (patch) | |
tree | 909755d4c7b63a4406b14770d4948b0cb8ad5dfa | |
parent | dc3dc0d5f11864c18c66ae9ecf3041aa10ae43f6 (diff) | |
parent | aa3b4ed737f8965b609843a6f722ce9a1fbef877 (diff) |
Merge pull request #17872 from kamil-tekiela/Remove-$params-2
Refactoring of Processes.php
7 files changed, 90 insertions, 97 deletions
diff --git a/libraries/classes/Controllers/Server/Status/Processes/RefreshController.php b/libraries/classes/Controllers/Server/Status/Processes/RefreshController.php index c297e3b256..795869c77c 100644 --- a/libraries/classes/Controllers/Server/Status/Processes/RefreshController.php +++ b/libraries/classes/Controllers/Server/Status/Processes/RefreshController.php @@ -24,18 +24,15 @@ final class RefreshController extends AbstractController public function __invoke(ServerRequest $request): void { - $params = [ - 'showExecuting' => $_POST['showExecuting'] ?? null, - 'full' => $_POST['full'] ?? null, - 'column_name' => $_POST['column_name'] ?? null, - 'order_by_field' => $_POST['order_by_field'] ?? null, - 'sort_order' => $_POST['sort_order'] ?? null, - ]; - if (! $this->response->isAjax()) { return; } - $this->render('server/status/processes/list', $this->processes->getList($params)); + $this->render('server/status/processes/list', $this->processes->getList( + $request->hasBodyParam('showExecuting'), + $request->hasBodyParam('full'), + (string) $request->getParsedBodyParam('order_by_field', ''), + (string) $request->getParsedBodyParam('sort_order', '') + )); } } diff --git a/libraries/classes/Controllers/Server/Status/ProcessesController.php b/libraries/classes/Controllers/Server/Status/ProcessesController.php index 28072d5371..7b71676758 100644 --- a/libraries/classes/Controllers/Server/Status/ProcessesController.php +++ b/libraries/classes/Controllers/Server/Status/ProcessesController.php @@ -34,41 +34,37 @@ class ProcessesController extends AbstractController public function __invoke(ServerRequest $request): void { - $GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null; - - $params = [ - 'showExecuting' => $_POST['showExecuting'] ?? null, - 'full' => $_POST['full'] ?? null, - 'column_name' => $_POST['column_name'] ?? null, - 'order_by_field' => $_POST['order_by_field'] ?? null, - 'sort_order' => $_POST['sort_order'] ?? null, - ]; $GLOBALS['errorUrl'] = Url::getFromRoute('/'); + $showExecuting = $request->hasBodyParam('showExecuting'); + $full = $request->hasBodyParam('full'); + $orderByField = (string) $request->getParsedBodyParam('order_by_field', ''); + $sortOrder = (string) $request->getParsedBodyParam('sort_order', ''); + if ($this->dbi->isSuperUser()) { $this->dbi->selectDb('mysql'); } $this->addScriptFiles(['server/status/processes.js']); - $isChecked = false; - if (! empty($params['showExecuting'])) { - $isChecked = true; - } + $listHtml = $this->template->render('server/status/processes/list', $this->processes->getList( + $showExecuting, + $full, + $orderByField, + $sortOrder + )); $urlParams = [ 'ajax_request' => true, - 'full' => $params['full'] ?? '', - 'column_name' => $params['column_name'] ?? '', - 'order_by_field' => $params['order_by_field'] ?? '', - 'sort_order' => $params['sort_order'] ?? '', + 'full' => $full, + 'column_name' => $request->getParsedBodyParam('column_name', ''), + 'order_by_field' => $orderByField, + 'sort_order' => $sortOrder, ]; - $listHtml = $this->template->render('server/status/processes/list', $this->processes->getList($params)); - $this->render('server/status/processes/index', [ 'url_params' => $urlParams, - 'is_checked' => $isChecked, + 'is_checked' => $showExecuting, 'server_process_list' => $listHtml, ]); } diff --git a/libraries/classes/Server/Status/Processes.php b/libraries/classes/Server/Status/Processes.php index 166ab3563d..55e476a83c 100644 --- a/libraries/classes/Server/Status/Processes.php +++ b/libraries/classes/Server/Status/Processes.php @@ -26,43 +26,33 @@ final class Processes } /** - * @param array $params Request parameters - * * @return array<string, array|string|bool> */ - public function getList(array $params): array + public function getList(bool $showExecuting, bool $showFullSql, string $orderByField, string $sortOrder): array { $urlParams = []; - $showFullSql = ! empty($params['full']); - if ($showFullSql) { - $urlParams['full'] = ''; - } else { - $urlParams['full'] = 1; - } + $urlParams['full'] = $showFullSql ? '' : 1; $sqlQuery = $showFullSql ? 'SHOW FULL PROCESSLIST' : 'SHOW PROCESSLIST'; if ( - (! empty($params['order_by_field']) - && ! empty($params['sort_order'])) - || ! empty($params['showExecuting']) + ($orderByField !== '' && $sortOrder !== '') + || $showExecuting ) { - $urlParams['order_by_field'] = $params['order_by_field']; - $urlParams['sort_order'] = $params['sort_order']; - $urlParams['showExecuting'] = $params['showExecuting']; + $urlParams['order_by_field'] = $orderByField; + $urlParams['sort_order'] = $sortOrder; + $urlParams['showExecuting'] = $showExecuting; $sqlQuery = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` '; } - if (! empty($params['showExecuting'])) { + if ($showExecuting) { $sqlQuery .= ' WHERE state != "" '; } - if (! empty($params['order_by_field']) && ! empty($params['sort_order'])) { - $sqlQuery .= ' ORDER BY ' - . Util::backquote($params['order_by_field']) - . ' ' . $params['sort_order']; + if ($orderByField !== '' && $sortOrder !== '') { + $sqlQuery .= ' ORDER BY ' . Util::backquote($orderByField) . ' ' . $sortOrder; } $result = $this->dbi->query($sqlQuery); @@ -93,16 +83,22 @@ final class Processes ]; } + $columns = $this->getSortableColumnsForProcessList($showExecuting, $showFullSql, $orderByField, $sortOrder); + return [ - 'columns' => $this->getSortableColumnsForProcessList($showFullSql, $params), + 'columns' => $columns, 'rows' => $rows, 'refresh_params' => $urlParams, 'is_mariadb' => $this->dbi->isMariaDB(), ]; } - private function getSortableColumnsForProcessList(bool $showFullSql, array $params): array - { + private function getSortableColumnsForProcessList( + bool $showExecuting, + bool $showFullSql, + string $orderByField, + string $sortOrder + ): array { // This array contains display name and real column name of each // sortable column in the table $sortableColumns = [ @@ -152,16 +148,16 @@ final class Processes $columns = []; foreach ($sortableColumns as $columnKey => $column) { - $is_sorted = ! empty($params['order_by_field']) - && ! empty($params['sort_order']) - && ($params['order_by_field'] == $column['order_by_field']); + $is_sorted = $orderByField !== '' + && $sortOrder !== '' + && ($orderByField == $column['order_by_field']); $column['sort_order'] = 'ASC'; - if ($is_sorted && $params['sort_order'] === 'ASC') { + if ($is_sorted && $sortOrder === 'ASC') { $column['sort_order'] = 'DESC'; } - if (isset($params['showExecuting'])) { + if ($showExecuting) { $column['showExecuting'] = 'on'; } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 784393c7e1..fd07b47ff3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1461,6 +1461,16 @@ parameters: path: libraries/classes/Controllers/Server/Status/Processes/KillController.php - + message: "#^Cannot cast mixed to string\\.$#" + count: 2 + path: libraries/classes/Controllers/Server/Status/Processes/RefreshController.php + + - + message: "#^Cannot cast mixed to string\\.$#" + count: 2 + path: libraries/classes/Controllers/Server/Status/ProcessesController.php + + - message: "#^Method PhpMyAdmin\\\\Controllers\\\\Server\\\\Status\\\\StatusController\\:\\:getConnectionsInfo\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: libraries/classes/Controllers/Server/Status/StatusController.php @@ -7666,21 +7676,11 @@ parameters: path: libraries/classes/Server/Status/Monitor.php - - message: "#^Method PhpMyAdmin\\\\Server\\\\Status\\\\Processes\\:\\:getList\\(\\) has parameter \\$params with no value type specified in iterable type array\\.$#" - count: 1 - path: libraries/classes/Server/Status/Processes.php - - - message: "#^Method PhpMyAdmin\\\\Server\\\\Status\\\\Processes\\:\\:getList\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: libraries/classes/Server/Status/Processes.php - - message: "#^Method PhpMyAdmin\\\\Server\\\\Status\\\\Processes\\:\\:getSortableColumnsForProcessList\\(\\) has parameter \\$params with no value type specified in iterable type array\\.$#" - count: 1 - path: libraries/classes/Server/Status/Processes.php - - - message: "#^Method PhpMyAdmin\\\\Server\\\\Status\\\\Processes\\:\\:getSortableColumnsForProcessList\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: libraries/classes/Server/Status/Processes.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e96264a44a..82109fffdd 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2911,11 +2911,6 @@ <code>$params['time_start']</code> </RiskyCast> </file> - <file src="libraries/classes/Controllers/Server/Status/ProcessesController.php"> - <MixedAssignment occurrences="1"> - <code>$GLOBALS['errorUrl']</code> - </MixedAssignment> - </file> <file src="libraries/classes/Controllers/Server/Status/QueriesController.php"> <MixedArgumentTypeCoercion occurrences="1"> <code>$key</code> @@ -13150,17 +13145,6 @@ </PossiblyNullOperand> </file> <file src="libraries/classes/Server/Status/Processes.php"> - <MixedArgument occurrences="1"> - <code>$params['order_by_field']</code> - </MixedArgument> - <MixedAssignment occurrences="3"> - <code>$urlParams['order_by_field']</code> - <code>$urlParams['showExecuting']</code> - <code>$urlParams['sort_order']</code> - </MixedAssignment> - <MixedOperand occurrences="1"> - <code>$params['sort_order']</code> - </MixedOperand> <RedundantCondition occurrences="1"> <code>0 !== --$sortableColCount</code> </RedundantCondition> diff --git a/test/classes/Controllers/Server/Status/Processes/RefreshControllerTest.php b/test/classes/Controllers/Server/Status/Processes/RefreshControllerTest.php index 5fccaacb01..b1b1f318f1 100644 --- a/test/classes/Controllers/Server/Status/Processes/RefreshControllerTest.php +++ b/test/classes/Controllers/Server/Status/Processes/RefreshControllerTest.php @@ -66,11 +66,18 @@ class RefreshControllerTest extends AbstractTestCase new Processes($GLOBALS['dbi']) ); - $_POST['full'] = '1'; - $_POST['order_by_field'] = 'process'; - $_POST['sort_order'] = 'DESC'; - - $controller($this->createStub(ServerRequest::class)); + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['column_name', '', ''], + ['order_by_field', '', 'process'], + ['sort_order', '', 'DESC'], + ]); + $request->method('hasBodyParam')->willReturnMap([ + ['full', true], + ['showExecuting', false], + ]); + + $controller($request); $html = $response->getHTMLResult(); $this->assertStringContainsString('index.php?route=/server/status/processes', $html); diff --git a/test/classes/Controllers/Server/Status/ProcessesControllerTest.php b/test/classes/Controllers/Server/Status/ProcessesControllerTest.php index a0ddb6e27c..6bd6dc4414 100644 --- a/test/classes/Controllers/Server/Status/ProcessesControllerTest.php +++ b/test/classes/Controllers/Server/Status/ProcessesControllerTest.php @@ -87,13 +87,19 @@ class ProcessesControllerTest extends AbstractTestCase $this->assertStringContainsString('Show full queries', $html); $this->assertStringContainsString('index.php?route=/server/status/processes', $html); - $_POST['full'] = '1'; - $_POST['column_name'] = 'Database'; - $_POST['order_by_field'] = 'Db'; - $_POST['sort_order'] = 'ASC'; + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['column_name', '', 'Database'], + ['order_by_field', '', 'Db'], + ['sort_order', '', 'ASC'], + ]); + $request->method('hasBodyParam')->willReturnMap([ + ['full', true], + ['showExecuting', false], + ]); $this->dummyDbi->addSelectDb('mysql'); - $controller($this->createStub(ServerRequest::class)); + $controller($request); $this->dummyDbi->assertAllSelectsConsumed(); $html = $response->getHTMLResult(); @@ -101,12 +107,19 @@ class ProcessesControllerTest extends AbstractTestCase $this->assertStringContainsString('Database', $html); $this->assertStringContainsString('DESC', $html); - $_POST['column_name'] = 'Host'; - $_POST['order_by_field'] = 'Host'; - $_POST['sort_order'] = 'DESC'; + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['column_name', '', 'Host'], + ['order_by_field', '', 'Host'], + ['sort_order', '', 'DESC'], + ]); + $request->method('hasBodyParam')->willReturnMap([ + ['full', true], + ['showExecuting', false], + ]); $this->dummyDbi->addSelectDb('mysql'); - $controller($this->createStub(ServerRequest::class)); + $controller($request); $this->dummyDbi->assertAllSelectsConsumed(); $html = $response->getHTMLResult(); |