diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-01 19:29:05 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-01 19:29:05 +0300 |
commit | 5725516656e8d537adab523cfe5db3baf2057921 (patch) | |
tree | 69b7d215e58477af2a7dc21a7c3fc658ec3b68b8 | |
parent | e5e7bd2f95911b92ebdf1a7e357a4477f360e891 (diff) |
Inline `Core::ifSetOr` method
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | libraries/classes/Controllers/Setup/ConfigController.php | 2 | ||||
-rw-r--r-- | libraries/classes/Controllers/Setup/HomeController.php | 6 | ||||
-rw-r--r-- | libraries/classes/Core.php | 38 | ||||
-rw-r--r-- | libraries/classes/Database/Qbe.php | 36 | ||||
-rw-r--r-- | libraries/classes/Footer.php | 4 | ||||
-rw-r--r-- | libraries/classes/ResponseRenderer.php | 4 | ||||
-rw-r--r-- | libraries/classes/Server/Privileges.php | 8 | ||||
-rw-r--r-- | psalm-baseline.xml | 10 | ||||
-rw-r--r-- | setup/config.php | 4 | ||||
-rw-r--r-- | test/classes/CoreTest.php | 8 |
10 files changed, 39 insertions, 81 deletions
diff --git a/libraries/classes/Controllers/Setup/ConfigController.php b/libraries/classes/Controllers/Setup/ConfigController.php index 2e6db1627f..e0814f410c 100644 --- a/libraries/classes/Controllers/Setup/ConfigController.php +++ b/libraries/classes/Controllers/Setup/ConfigController.php @@ -28,7 +28,7 @@ class ConfigController extends AbstractController return $this->template->render('setup/config/index', [ 'formset' => $params['formset'] ?? '', 'pages' => $pages, - 'eol' => Core::ifSetOr($params['eol'], 'unix'), + 'eol' => Core::isValid($params['eol'], 'similar', 'unix') ? $params['eol'] : 'unix', 'config' => $config, 'has_check_page_refresh' => $hasCheckPageRefresh, ]); diff --git a/libraries/classes/Controllers/Setup/HomeController.php b/libraries/classes/Controllers/Setup/HomeController.php index 5a96ccdeff..c47add0ec7 100644 --- a/libraries/classes/Controllers/Setup/HomeController.php +++ b/libraries/classes/Controllers/Setup/HomeController.php @@ -132,6 +132,8 @@ class HomeController extends AbstractController $hasCheckPageRefresh = true; } + $isWindows = (bool) $GLOBALS['config']->get('PMA_IS_WINDOWS'); + return $this->template->render('setup/home/index', [ 'formset' => $params['formset'] ?? '', 'languages' => $languages, @@ -140,7 +142,9 @@ class HomeController extends AbstractController 'servers' => $servers, 'pages' => $pages, 'has_check_page_refresh' => $hasCheckPageRefresh, - 'eol' => Core::ifSetOr($_SESSION['eol'], ($GLOBALS['config']->get('PMA_IS_WINDOWS') ? 'win' : 'unix')), + 'eol' => Core::isValid($_SESSION['eol'], 'similar', $isWindows ? 'win' : 'unix') + ? $_SESSION['eol'] + : ($isWindows ? 'win' : 'unix'), ]); } } diff --git a/libraries/classes/Core.php b/libraries/classes/Core.php index cd6979cae3..68e56bad26 100644 --- a/libraries/classes/Core.php +++ b/libraries/classes/Core.php @@ -65,44 +65,6 @@ use const FILTER_VALIDATE_IP; class Core { /** - * checks given $var and returns it if valid, or $default of not valid - * given $var is also checked for type being 'similar' as $default - * or against any other type if $type is provided - * - * <code> - * // $_REQUEST['db'] not set - * echo Core::ifSetOr($_REQUEST['db'], ''); // '' - * // $_POST['sql_query'] not set - * echo Core::ifSetOr($_POST['sql_query']); // null - * // $cfg['EnableFoo'] not set - * echo Core::ifSetOr($cfg['EnableFoo'], false, 'boolean'); // false - * echo Core::ifSetOr($cfg['EnableFoo']); // null - * // $cfg['EnableFoo'] set to 1 - * echo Core::ifSetOr($cfg['EnableFoo'], false, 'boolean'); // false - * echo Core::ifSetOr($cfg['EnableFoo'], false, 'similar'); // 1 - * echo Core::ifSetOr($cfg['EnableFoo'], false); // 1 - * // $cfg['EnableFoo'] set to true - * echo Core::ifSetOr($cfg['EnableFoo'], false, 'boolean'); // true - * </code> - * - * @see self::isValid() - * - * @param mixed $var param to check - * @param mixed $default default value - * @param mixed $type var type or array of values to check against $var - * - * @return mixed $var or $default - */ - public static function ifSetOr(&$var, $default = null, $type = 'similar') - { - if (! self::isValid($var, $type, $default)) { - return $default; - } - - return $var; - } - - /** * checks given $var against $type or $compare * * $type can be: diff --git a/libraries/classes/Database/Qbe.php b/libraries/classes/Database/Qbe.php index 79bab23708..6519f9369e 100644 --- a/libraries/classes/Database/Qbe.php +++ b/libraries/classes/Database/Qbe.php @@ -311,16 +311,12 @@ class Qbe { $criteriaColumnCount = $this->initializeCriteriasCount(); - $this->criteriaColumnInsert = Core::ifSetOr( - $_POST['criteriaColumnInsert'], - null, - 'array' - ); - $this->criteriaColumnDelete = Core::ifSetOr( - $_POST['criteriaColumnDelete'], - null, - 'array' - ); + $this->criteriaColumnInsert = Core::isValid($_POST['criteriaColumnInsert'], 'array', null) + ? $_POST['criteriaColumnInsert'] + : null; + $this->criteriaColumnDelete = Core::isValid($_POST['criteriaColumnDelete'], 'array', null) + ? $_POST['criteriaColumnDelete'] + : null; $this->prevCriteria = $_POST['prev_criteria'] ?? []; $this->criteria = $_POST['criteria'] ?? array_fill(0, $criteriaColumnCount, ''); @@ -1846,24 +1842,20 @@ class Qbe private function initializeCriteriasCount(): int { // sets column count - $criteriaColumnCount = Core::ifSetOr( - $_POST['criteriaColumnCount'], - 3, - 'numeric' - ); - $criteriaColumnAdd = Core::ifSetOr( - $_POST['criteriaColumnAdd'], - 0, - 'numeric' - ); + $criteriaColumnCount = Core::isValid($_POST['criteriaColumnCount'], 'numeric', 3) + ? $_POST['criteriaColumnCount'] + : 3; + $criteriaColumnAdd = Core::isValid($_POST['criteriaColumnAdd'], 'numeric', 0) + ? $_POST['criteriaColumnAdd'] + : 0; $this->criteriaColumnCount = max( $criteriaColumnCount + $criteriaColumnAdd, 0 ); // sets row count - $rows = Core::ifSetOr($_POST['rows'], 0, 'numeric'); - $criteriaRowAdd = Core::ifSetOr($_POST['criteriaRowAdd'], 0, 'numeric'); + $rows = Core::isValid($_POST['rows'], 'numeric', 0) ? $_POST['rows'] : 0; + $criteriaRowAdd = Core::isValid($_POST['criteriaRowAdd'], 'numeric', 0) ? $_POST['criteriaRowAdd'] : 0; $this->criteriaRowCount = min( 100, max($rows + $criteriaRowAdd, 0) diff --git a/libraries/classes/Footer.php b/libraries/classes/Footer.php index 478dd8125f..3d708e8882 100644 --- a/libraries/classes/Footer.php +++ b/libraries/classes/Footer.php @@ -239,8 +239,8 @@ class Footer } $this->relation->setHistory( - Core::ifSetOr($GLOBALS['db'], ''), - Core::ifSetOr($GLOBALS['table'], ''), + Core::isValid($GLOBALS['db'], 'similar', '') ? $GLOBALS['db'] : '', + Core::isValid($GLOBALS['table'], 'similar', '') ? $GLOBALS['table'] : '', $GLOBALS['cfg']['Server']['user'], $GLOBALS['sql_query'] ); diff --git a/libraries/classes/ResponseRenderer.php b/libraries/classes/ResponseRenderer.php index 5348874e49..8f4b56020a 100644 --- a/libraries/classes/ResponseRenderer.php +++ b/libraries/classes/ResponseRenderer.php @@ -376,8 +376,8 @@ class ResponseRenderer $this->addJSON( 'reloadQuerywindow', [ - 'db' => Core::ifSetOr($GLOBALS['db'], ''), - 'table' => Core::ifSetOr($GLOBALS['table'], ''), + 'db' => Core::isValid($GLOBALS['db'], 'similar', '') ? $GLOBALS['db'] : '', + 'table' => Core::isValid($GLOBALS['table'], 'similar', '') ? $GLOBALS['table'] : '', 'sql_query' => $query, ] ); diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php index 148a593bc9..d2525185db 100644 --- a/libraries/classes/Server/Privileges.php +++ b/libraries/classes/Server/Privileges.php @@ -3274,8 +3274,12 @@ class Privileges $privilegesTable = $this->getHtmlToDisplayPrivilegesTable( // If $dbname is an array, pass any one db as all have same privs. - Core::ifSetOr($dbname, is_array($dbname) ? $dbname[0] : '*', 'length'), - Core::ifSetOr($tablename, '*', 'length') + Core::isValid($dbname, 'length', is_array($dbname) ? $dbname[0] : '*') + ? $dbname + : (is_array($dbname) ? $dbname[0] : '*'), + Core::isValid($tablename, 'length', '*') + ? $tablename + : '*' ); $tableSpecificRights = ''; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e88faca215..9ba323297f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5315,11 +5315,11 @@ <code>$_POST['Or' . $rowIndex][$columnIndex]</code> <code>$clause</code> <code>$columns[$columnIndex]</code> - <code>$criteriaColumnCount</code> + <code>$criteriaColumnAdd</code> + <code>$criteriaRowAdd</code> <code>$eachTable</code> <code>$eachTable</code> <code>$index['Column_name']</code> - <code>$rows</code> <code>$select</code> <code>$selected['and'] ?? ''</code> <code>$selected['or'] ?? ''</code> @@ -7180,11 +7180,9 @@ </TypeDoesNotContainType> </file> <file src="libraries/classes/Footer.php"> - <MixedArgument occurrences="4"> + <MixedArgument occurrences="2"> <code>$db</code> <code>$table</code> - <code>Core::ifSetOr($GLOBALS['db'], '')</code> - <code>Core::ifSetOr($GLOBALS['table'], '')</code> </MixedArgument> <MixedArgumentTypeCoercion occurrences="1"> <code>$params</code> @@ -14345,8 +14343,6 @@ <code>$user</code> <code>$username</code> <code>$username</code> - <code>Core::ifSetOr($dbname, is_array($dbname) ? $dbname[0] : '*', 'length')</code> - <code>Core::ifSetOr($tablename, '*', 'length')</code> </MixedArgument> <MixedArgumentTypeCoercion occurrences="16"> <code>$queries</code> diff --git a/setup/config.php b/setup/config.php index 131b30ebac..5b8024e87f 100644 --- a/setup/config.php +++ b/setup/config.php @@ -33,7 +33,7 @@ if (isset($_POST['eol'])) { $_SESSION['eol'] = $_POST['eol'] === 'unix' ? 'unix' : 'win'; } -if (Core::ifSetOr($_POST['submit_clear'], '')) { +if (Core::isValid($_POST['submit_clear'], 'similar', '') ? $_POST['submit_clear'] : '') { // Clear current config and return to main page $GLOBALS['ConfigFile']->resetConfigData(); // drop post data @@ -41,7 +41,7 @@ if (Core::ifSetOr($_POST['submit_clear'], '')) { exit; } -if (Core::ifSetOr($_POST['submit_download'], '')) { +if (Core::isValid($_POST['submit_download'], 'similar', '') ? $_POST['submit_download'] : '') { // Output generated config file Core::downloadHeader('config.inc.php', 'text/plain'); $response->disable(); diff --git a/test/classes/CoreTest.php b/test/classes/CoreTest.php index f9a066f037..fd2a1f22f6 100644 --- a/test/classes/CoreTest.php +++ b/test/classes/CoreTest.php @@ -606,7 +606,7 @@ class CoreTest extends AbstractNetworkTestCase { $default = 'foo'; $in = 'bar'; - $out = Core::ifSetOr($in, $default); + $out = Core::isValid($in, 'similar', $default) ? $in : $default; $this->assertEquals($in, $out); } @@ -617,7 +617,7 @@ class CoreTest extends AbstractNetworkTestCase { $default = 'foo'; $in = 'bar'; - $out = Core::ifSetOr($in, $default, 'boolean'); + $out = Core::isValid($in, 'boolean', $default) ? $in : $default; $this->assertEquals($out, $default); } @@ -628,7 +628,7 @@ class CoreTest extends AbstractNetworkTestCase { $default = 'foo'; // $in is not set! - $out = Core::ifSetOr($in, $default); + $out = Core::isValid($in, 'similar', $default) ? $in : $default; $this->assertEquals($out, $default); } @@ -638,7 +638,7 @@ class CoreTest extends AbstractNetworkTestCase public function testVarNotSetNoDefault(): void { // $in is not set! - $out = Core::ifSetOr($in); + $out = Core::isValid($in, 'similar', null) ? $in : null; $this->assertNull($out); } |