diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-19 19:17:32 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-19 19:17:32 +0300 |
commit | 229d8dfb5e14dbe5c1faa17cdb84ec8863bd9583 (patch) | |
tree | e70b5b821147f4ad408b3d76c35d66f15b511abd /libraries/classes/Config | |
parent | e2da751efd939bd7cd83f5b759f451abebec49b3 (diff) |
Extract Console, Debug and SqlQueryBox classes from Settings
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'libraries/classes/Config')
-rw-r--r-- | libraries/classes/Config/Settings.php | 190 | ||||
-rw-r--r-- | libraries/classes/Config/Settings/Console.php | 205 | ||||
-rw-r--r-- | libraries/classes/Config/Settings/Debug.php | 82 | ||||
-rw-r--r-- | libraries/classes/Config/Settings/SqlQueryBox.php | 84 |
4 files changed, 389 insertions, 172 deletions
diff --git a/libraries/classes/Config/Settings.php b/libraries/classes/Config/Settings.php index 4e177ba224..16a2ceb866 100644 --- a/libraries/classes/Config/Settings.php +++ b/libraries/classes/Config/Settings.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace PhpMyAdmin\Config; +use PhpMyAdmin\Config\Settings\Console; +use PhpMyAdmin\Config\Settings\Debug; use PhpMyAdmin\Config\Settings\Export; use PhpMyAdmin\Config\Settings\Import; use PhpMyAdmin\Config\Settings\Schema; use PhpMyAdmin\Config\Settings\Server; +use PhpMyAdmin\Config\Settings\SqlQueryBox; use PhpMyAdmin\Config\Settings\Transformations; use function count; @@ -1357,8 +1360,7 @@ final class Settings * SQL Query box settings * These are the links display in all of the SQL Query boxes * - * @var array<string, bool> - * @psalm-var array{Edit: bool, Explain: bool, ShowAsPHP: bool, Refresh: bool} + * @var SqlQueryBox */ public $SQLQuery; @@ -1479,8 +1481,7 @@ final class Settings /** * Developers ONLY! * - * @var array<string, bool> - * @psalm-var array{sql: bool, sqllog: bool, demo: bool, simple2fa: bool} + * @var Debug */ public $DBG; @@ -1539,19 +1540,7 @@ final class Settings * * This is mostly meant for user preferences. * - * @var array<string, string|int|bool> - * @psalm-var array{ - * StartHistory: bool, - * AlwaysExpand: bool, - * CurrentQuery: bool, - * EnterExecutes: bool, - * DarkTheme: bool, - * Mode: 'info'|'show'|'collapse', - * Height: positive-int, - * GroupQueries: bool, - * OrderBy: 'exec'|'time'|'count', - * Order: 'asc'|'desc' - * } + * @var Console */ public $Console; @@ -4231,48 +4220,14 @@ final class Settings /** * @param array<int|string, mixed> $settings - * - * @return array<string, bool> - * @psalm-return array{Edit: bool, Explain: bool, ShowAsPHP: bool, Refresh: bool} */ - private function setSQLQuery(array $settings): array + private function setSQLQuery(array $settings): SqlQueryBox { - $sqlQuery = [ - // Display an "Edit" link on the results page to change a query - 'Edit' => true, - - // Display an "Explain SQL" link on the results page - 'Explain' => true, - - // Display a "Create PHP code" link on the results page to wrap a query in PHP - 'ShowAsPHP' => true, - - // Display a "Refresh" link on the results page - 'Refresh' => true, - ]; - if (! isset($settings['SQLQuery']) || ! is_array($settings['SQLQuery'])) { - return $sqlQuery; + if (isset($settings['SQLQuery']) && is_array($settings['SQLQuery'])) { + return new SqlQueryBox($settings['SQLQuery']); } - if (isset($settings['SQLQuery']['Edit'])) { - $sqlQuery['Edit'] = (bool) $settings['SQLQuery']['Edit']; - } - - if (isset($settings['SQLQuery']['Explain'])) { - $sqlQuery['Explain'] = (bool) $settings['SQLQuery']['Explain']; - } - - if (isset($settings['SQLQuery']['ShowAsPHP'])) { - $sqlQuery['ShowAsPHP'] = (bool) $settings['SQLQuery']['ShowAsPHP']; - } - - if (! isset($settings['SQLQuery']['Refresh'])) { - return $sqlQuery; - } - - $sqlQuery['Refresh'] = (bool) $settings['SQLQuery']['Refresh']; - - return $sqlQuery; + return new SqlQueryBox(); } /** @@ -4464,48 +4419,14 @@ final class Settings /** * @param array<int|string, mixed> $settings - * - * @return array<string, bool> - * @psalm-return array{sql: bool, sqllog: bool, demo: bool, simple2fa: bool} */ - private function setDBG(array $settings): array + private function setDBG(array $settings): Debug { - $debug = [ - // Output executed queries and their execution times - 'sql' => false, - - // Log executed queries and their execution times to syslog - 'sqllog' => false, - - // Enable to let server present itself as demo server. - 'demo' => false, - - // Enable Simple two-factor authentication - 'simple2fa' => false, - ]; - if (! isset($settings['DBG']) || ! is_array($settings['DBG'])) { - return $debug; + if (isset($settings['DBG']) && is_array($settings['DBG'])) { + return new Debug($settings['DBG']); } - if (isset($settings['DBG']['sql'])) { - $debug['sql'] = (bool) $settings['DBG']['sql']; - } - - if (isset($settings['DBG']['sqllog'])) { - $debug['sqllog'] = (bool) $settings['DBG']['sqllog']; - } - - if (isset($settings['DBG']['demo'])) { - $debug['demo'] = (bool) $settings['DBG']['demo']; - } - - if (! isset($settings['DBG']['simple2fa'])) { - return $debug; - } - - $debug['simple2fa'] = (bool) $settings['DBG']['simple2fa']; - - return $debug; + return new Debug(); } /** @@ -4624,89 +4545,14 @@ final class Settings /** * @param array<int|string, mixed> $settings - * - * @return array<string, string|int|bool> - * @psalm-return array{ - * StartHistory: bool, - * AlwaysExpand: bool, - * CurrentQuery: bool, - * EnterExecutes: bool, - * DarkTheme: bool, - * Mode: 'info'|'show'|'collapse', - * Height: positive-int, - * GroupQueries: bool, - * OrderBy: 'exec'|'time'|'count', - * Order: 'asc'|'desc' - * } */ - private function setConsole(array $settings): array + private function setConsole(array $settings): Console { - $console = [ - 'StartHistory' => false, - 'AlwaysExpand' => false, - 'CurrentQuery' => true, - 'EnterExecutes' => false, - 'DarkTheme' => false, - 'Mode' => 'info', - 'Height' => 92, - 'GroupQueries' => false, - 'OrderBy' => 'exec', - 'Order' => 'asc', - ]; - - if (! isset($settings['Console']) || ! is_array($settings['Console'])) { - return $console; - } - - if (isset($settings['Console']['StartHistory'])) { - $console['StartHistory'] = (bool) $settings['Console']['StartHistory']; - } - - if (isset($settings['Console']['AlwaysExpand'])) { - $console['AlwaysExpand'] = (bool) $settings['Console']['AlwaysExpand']; - } - - if (isset($settings['Console']['CurrentQuery'])) { - $console['CurrentQuery'] = (bool) $settings['Console']['CurrentQuery']; - } - - if (isset($settings['Console']['EnterExecutes'])) { - $console['EnterExecutes'] = (bool) $settings['Console']['EnterExecutes']; - } - - if (isset($settings['Console']['DarkTheme'])) { - $console['DarkTheme'] = (bool) $settings['Console']['DarkTheme']; - } - - if (isset($settings['Console']['Mode']) && in_array($settings['Console']['Mode'], ['show', 'collapse'], true)) { - $console['Mode'] = $settings['Console']['Mode']; - } - - if (isset($settings['Console']['Height'])) { - $height = (int) $settings['Console']['Height']; - if ($height > 0) { - $console['Height'] = $height; - } + if (isset($settings['Console']) && is_array($settings['Console'])) { + return new Console($settings['Console']); } - if (isset($settings['Console']['GroupQueries'])) { - $console['GroupQueries'] = (bool) $settings['Console']['GroupQueries']; - } - - if ( - isset($settings['Console']['OrderBy']) - && in_array($settings['Console']['OrderBy'], ['time', 'count'], true) - ) { - $console['OrderBy'] = $settings['Console']['OrderBy']; - } - - if (! isset($settings['Console']['Order']) || $settings['Console']['Order'] !== 'desc') { - return $console; - } - - $console['Order'] = 'desc'; - - return $console; + return new Console(); } /** diff --git a/libraries/classes/Config/Settings/Console.php b/libraries/classes/Config/Settings/Console.php new file mode 100644 index 0000000000..787535bd9e --- /dev/null +++ b/libraries/classes/Config/Settings/Console.php @@ -0,0 +1,205 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Config\Settings; + +use function in_array; + +// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + +/** + * @psalm-immutable + */ +final class Console +{ + /** @var bool */ + public $StartHistory; + + /** @var bool */ + public $AlwaysExpand; + + /** @var bool */ + public $CurrentQuery; + + /** @var bool */ + public $EnterExecutes; + + /** @var bool */ + public $DarkTheme; + + /** + * @var string + * @psalm-var 'info'|'show'|'collapse' + */ + public $Mode; + + /** + * @var int + * @psalm-var positive-int + */ + public $Height; + + /** @var bool */ + public $GroupQueries; + + /** + * @var string + * @psalm-var 'exec'|'time'|'count' + */ + public $OrderBy; + + /** + * @var string + * @psalm-var 'asc'|'desc' + */ + public $Order; + + /** + * @param mixed[] $console + */ + public function __construct(array $console = []) + { + $this->StartHistory = $this->setStartHistory($console); + $this->AlwaysExpand = $this->setAlwaysExpand($console); + $this->CurrentQuery = $this->setCurrentQuery($console); + $this->EnterExecutes = $this->setEnterExecutes($console); + $this->DarkTheme = $this->setDarkTheme($console); + $this->Mode = $this->setMode($console); + $this->Height = $this->setHeight($console); + $this->GroupQueries = $this->setGroupQueries($console); + $this->OrderBy = $this->setOrderBy($console); + $this->Order = $this->setOrder($console); + } + + /** + * @param mixed[] $console + */ + private function setStartHistory(array $console): bool + { + if (isset($console['StartHistory'])) { + return (bool) $console['StartHistory']; + } + + return false; + } + + /** + * @param mixed[] $console + */ + private function setAlwaysExpand(array $console): bool + { + if (isset($console['AlwaysExpand'])) { + return (bool) $console['AlwaysExpand']; + } + + return false; + } + + /** + * @param mixed[] $console + */ + private function setCurrentQuery(array $console): bool + { + if (isset($console['CurrentQuery'])) { + return (bool) $console['CurrentQuery']; + } + + return true; + } + + /** + * @param mixed[] $console + */ + private function setEnterExecutes(array $console): bool + { + if (isset($console['EnterExecutes'])) { + return (bool) $console['EnterExecutes']; + } + + return false; + } + + /** + * @param mixed[] $console + */ + private function setDarkTheme(array $console): bool + { + if (isset($console['DarkTheme'])) { + return (bool) $console['DarkTheme']; + } + + return false; + } + + /** + * @param mixed[] $console + * + * @psalm-return 'info'|'show'|'collapse' + */ + private function setMode(array $console): string + { + if (isset($console['Mode']) && in_array($console['Mode'], ['show', 'collapse'], true)) { + return $console['Mode']; + } + + return 'info'; + } + + /** + * @param mixed[] $console + * + * @psalm-return positive-int + */ + private function setHeight(array $console): int + { + if (isset($console['Height'])) { + $height = (int) $console['Height']; + if ($height >= 1) { + return $height; + } + } + + return 92; + } + + /** + * @param mixed[] $console + */ + private function setGroupQueries(array $console): bool + { + if (isset($console['GroupQueries'])) { + return (bool) $console['GroupQueries']; + } + + return false; + } + + /** + * @param mixed[] $console + * + * @psalm-return 'exec'|'time'|'count' + */ + private function setOrderBy(array $console): string + { + if (isset($console['OrderBy']) && in_array($console['OrderBy'], ['time', 'count'], true)) { + return $console['OrderBy']; + } + + return 'exec'; + } + + /** + * @param mixed[] $console + * + * @psalm-return 'asc'|'desc' + */ + private function setOrder(array $console): string + { + if (isset($console['Order']) && $console['Order'] === 'desc') { + return 'desc'; + } + + return 'asc'; + } +} diff --git a/libraries/classes/Config/Settings/Debug.php b/libraries/classes/Config/Settings/Debug.php new file mode 100644 index 0000000000..077bd4d345 --- /dev/null +++ b/libraries/classes/Config/Settings/Debug.php @@ -0,0 +1,82 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Config\Settings; + +/** + * @psalm-immutable + */ +final class Debug +{ + /** + * Output executed queries and their execution times. + * + * @var bool + */ + public $sql; + + /** + * Log executed queries and their execution times to syslog. + * + * @var bool + */ + public $sqllog; + + /** + * Enable to let server present itself as demo server. + * + * @var bool + */ + public $demo; + + /** + * Enable Simple two-factor authentication. + * + * @var bool + */ + public $simple2fa; + + /** + * @param mixed[] $debug + */ + public function __construct(array $debug = []) + { + $this->sql = $this->setSql($debug); + $this->sqllog = $this->setSqlLog($debug); + $this->demo = $this->setDemo($debug); + $this->simple2fa = $this->setSimple2fa($debug); + } + + /** + * @param mixed[] $debug + */ + private function setSql(array $debug): bool + { + return isset($debug['sql']) && $debug['sql']; + } + + /** + * @param mixed[] $debug + */ + private function setSqlLog(array $debug): bool + { + return isset($debug['sqllog']) && $debug['sqllog']; + } + + /** + * @param mixed[] $debug + */ + private function setDemo(array $debug): bool + { + return isset($debug['demo']) && $debug['demo']; + } + + /** + * @param mixed[] $debug + */ + private function setSimple2fa(array $debug): bool + { + return isset($debug['simple2fa']) && $debug['simple2fa']; + } +} diff --git a/libraries/classes/Config/Settings/SqlQueryBox.php b/libraries/classes/Config/Settings/SqlQueryBox.php new file mode 100644 index 0000000000..a0c549d37e --- /dev/null +++ b/libraries/classes/Config/Settings/SqlQueryBox.php @@ -0,0 +1,84 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Config\Settings; + +// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + +/** + * @psalm-immutable + */ +final class SqlQueryBox +{ + /** + * Display an "Edit" link on the results page to change a query. + * + * @var bool + */ + public $Edit; + + /** + * Display an "Explain SQL" link on the results page. + * + * @var bool + */ + public $Explain; + + /** + * Display a "Create PHP code" link on the results page to wrap a query in PHP. + * + * @var bool + */ + public $ShowAsPHP; + + /** + * Display a "Refresh" link on the results page. + * + * @var bool + */ + public $Refresh; + + /** + * @param mixed[] $sqlQueryBox + */ + public function __construct(array $sqlQueryBox = []) + { + $this->Edit = $this->setEdit($sqlQueryBox); + $this->Explain = $this->setExplain($sqlQueryBox); + $this->ShowAsPHP = $this->setShowAsPHP($sqlQueryBox); + $this->Refresh = $this->setRefresh($sqlQueryBox); + } + + /** + * @param mixed[] $sqlQueryBox + */ + private function setEdit(array $sqlQueryBox): bool + { + return ! isset($sqlQueryBox['Edit']) || $sqlQueryBox['Edit']; + } + + /** + * @param mixed[] $sqlQueryBox + */ + private function setExplain(array $sqlQueryBox): bool + { + return ! isset($sqlQueryBox['Explain']) || $sqlQueryBox['Explain']; + } + + /** + * @param mixed[] $sqlQueryBox + */ + private function setShowAsPHP(array $sqlQueryBox): bool + { + return ! isset($sqlQueryBox['ShowAsPHP']) || $sqlQueryBox['ShowAsPHP']; + } + + /** + * @param mixed[] $sqlQueryBox + */ + private function setRefresh(array $sqlQueryBox): bool + { + return ! isset($sqlQueryBox['Refresh']) || $sqlQueryBox['Refresh']; + } +} |