Skip to content

Commit 371355a

Browse files
Merge pull request #18642 from kamil-tekiela/Use-match
Use match instead of if-else
2 parents 9e4af1b + 7329a61 commit 371355a

7 files changed

Lines changed: 44 additions & 60 deletions

File tree

libraries/classes/Controllers/Import/ImportController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,12 @@ public function __invoke(ServerRequest $request): void
419419

420420
// Calculate value of the limit
421421
$memoryUnit = mb_strtolower(substr((string) $GLOBALS['memory_limit'], -1));
422-
if ($memoryUnit === 'm') {
423-
$GLOBALS['memory_limit'] = (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024 * 1024;
424-
} elseif ($memoryUnit === 'k') {
425-
$GLOBALS['memory_limit'] = (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024;
426-
} elseif ($memoryUnit === 'g') {
427-
$GLOBALS['memory_limit'] = (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024 * 1024 * 1024;
428-
} else {
429-
$GLOBALS['memory_limit'] = (int) $GLOBALS['memory_limit'];
430-
}
422+
$GLOBALS['memory_limit'] = match ($memoryUnit) {
423+
'm' => (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024 * 1024,
424+
'k' => (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024,
425+
'g' => (int) substr((string) $GLOBALS['memory_limit'], 0, -1) * 1024 * 1024 * 1024,
426+
default => (int) $GLOBALS['memory_limit'],
427+
};
431428

432429
// Just to be sure, there might be lot of memory needed for uncompression
433430
$GLOBALS['read_limit'] = $GLOBALS['memory_limit'] / 8;

libraries/classes/Export/Export.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,21 +1260,19 @@ private function getHTMLForRefreshButton(string $exportType): string
12601260
private function getHTMLForBackButton(string $exportType, string $db, string $table): string
12611261
{
12621262
$backButton = '<p>[ <a href="';
1263-
if ($exportType === 'server') {
1264-
$backButton .= Url::getFromRoute('/server/export') . '" data-post="' . Url::getCommon([], '', false);
1265-
} elseif ($exportType === 'database') {
1266-
$backButton .= Url::getFromRoute('/database/export') . '" data-post="' . Url::getCommon(
1263+
$backButton .= match ($exportType) {
1264+
'server' => Url::getFromRoute('/server/export') . '" data-post="' . Url::getCommon([], '', false),
1265+
'database' => Url::getFromRoute('/database/export') . '" data-post="' . Url::getCommon(
12671266
['db' => $db],
12681267
'',
1269-
false,
1270-
);
1271-
} else {
1272-
$backButton .= Url::getFromRoute('/table/export') . '" data-post="' . Url::getCommon(
1268+
false,
1269+
),
1270+
default => Url::getFromRoute('/table/export') . '" data-post="' . Url::getCommon(
12731271
['db' => $db, 'table' => $table],
12741272
'',
12751273
false,
1276-
);
1277-
}
1274+
),
1275+
};
12781276

12791277
$postParams = array_filter($this->getPostParams($exportType), static fn ($value): bool => ! is_array($value));
12801278
$backButton .= '&amp;' . http_build_query($postParams);

libraries/classes/Plugins/Export/ExportSql.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,18 +1156,15 @@ private function exportConfigurationMetadata(
11561156
}
11571157

11581158
// remove auto_incrementing id field for some tables
1159-
if ($type === 'bookmark') {
1160-
$sqlQuery = 'SELECT `dbase`, `user`, `label`, `query` FROM ';
1161-
} elseif ($type === 'column_info') {
1162-
$sqlQuery = 'SELECT `db_name`, `table_name`, `column_name`,'
1159+
$sqlQuery = match ($type) {
1160+
'bookmark' => 'SELECT `dbase`, `user`, `label`, `query` FROM ',
1161+
'column_info' => 'SELECT `db_name`, `table_name`, `column_name`,'
11631162
. ' `comment`, `mimetype`, `transformation`,'
11641163
. ' `transformation_options`, `input_transformation`,'
1165-
. ' `input_transformation_options` FROM';
1166-
} elseif ($type === 'savedsearches') {
1167-
$sqlQuery = 'SELECT `username`, `db_name`, `search_name`, `search_data` FROM';
1168-
} else {
1169-
$sqlQuery = 'SELECT * FROM ';
1170-
}
1164+
. ' `input_transformation_options` FROM',
1165+
'savedsearches' => 'SELECT `username`, `db_name`, `search_name`, `search_data` FROM',
1166+
default => 'SELECT * FROM ',
1167+
};
11711168

11721169
$sqlQuery .= Util::backquote($relationParameters->db)
11731170
. '.' . Util::backquote((string) $relationParams[$type])

libraries/classes/Plugins/Export/ExportXml.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,11 @@ private function exportDefinitions(string $db, string $type, array $names): stri
164164
$head .= ' <pma:' . $type . ' name="' . htmlspecialchars($name) . '">' . "\n";
165165

166166
$dbi = DatabaseInterface::getInstance();
167-
if ($type === 'function') {
168-
$definition = Routines::getFunctionDefinition($dbi, $db, $name);
169-
} elseif ($type === 'procedure') {
170-
$definition = Routines::getProcedureDefinition($dbi, $db, $name);
171-
} else {
172-
$definition = Events::getDefinition($dbi, $db, $name);
173-
}
167+
$definition = match ($type) {
168+
'function' => Routines::getFunctionDefinition($dbi, $db, $name),
169+
'procedure' => Routines::getProcedureDefinition($dbi, $db, $name),
170+
default => Events::getDefinition($dbi, $db, $name),
171+
};
174172

175173
// Do some formatting
176174
$sql = htmlspecialchars(rtrim((string) $definition));

libraries/classes/Plugins/Transformations/Abs/DateFormatTransformationsPlugin.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,11 @@ public function applyTransformation(string $buffer, array $options = [], FieldMe
126126
if ($timestamp >= 0) {
127127
$timestamp -= (int) $options[0] * 60 * 60;
128128
$source = $buffer;
129-
if ($options[2] === 'local') {
130-
$text = Util::localisedDate($timestamp, $options[1]);
131-
} elseif ($options[2] === 'utc') {
132-
$text = gmdate($options[1], $timestamp);
133-
} else {
134-
$text = 'INVALID DATE TYPE';
135-
}
129+
$text = match ($options[2]) {
130+
'local' => Util::localisedDate($timestamp, $options[1]),
131+
'utc' => gmdate($options[1], $timestamp),
132+
default => 'INVALID DATE TYPE',
133+
};
136134

137135
return '<dfn onclick="alert(' . htmlspecialchars((string) json_encode($source), ENT_COMPAT) . ');" title="'
138136
. htmlspecialchars($source) . '">' . htmlspecialchars($text) . '</dfn>';

libraries/classes/Server/Privileges.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,23 +1487,21 @@ public function getUserSpecificRights(string $username, string $hostname, string
14871487
$dbRights[$dbRightsRow[$dbOrTableName]] = $dbRightsRow;
14881488
}
14891489

1490-
if ($type === 'database') {
1491-
$sqlQuery = 'SELECT * FROM `mysql`.`db`'
1492-
. $userHostCondition . ' ORDER BY `Db` ASC';
1493-
} elseif ($type === 'table') {
1494-
$sqlQuery = 'SELECT `Table_name`,'
1490+
$sqlQuery = match ($type) {
1491+
'database' => 'SELECT * FROM `mysql`.`db`'
1492+
. $userHostCondition . ' ORDER BY `Db` ASC',
1493+
'table' => 'SELECT `Table_name`,'
14951494
. ' `Table_priv`,'
14961495
. ' IF(`Column_priv` = _latin1 \'\', 0, 1)'
14971496
. ' AS \'Column_priv\''
14981497
. ' FROM `mysql`.`tables_priv`'
14991498
. $userHostCondition
1500-
. ' ORDER BY `Table_name` ASC;';
1501-
} else {
1502-
$sqlQuery = 'SELECT `Routine_name`, `Proc_priv`'
1499+
. ' ORDER BY `Table_name` ASC;',
1500+
default => 'SELECT `Routine_name`, `Proc_priv`'
15031501
. ' FROM `mysql`.`procs_priv`'
15041502
. $userHostCondition
1505-
. ' ORDER BY `Routine_name`';
1506-
}
1503+
. ' ORDER BY `Routine_name`',
1504+
};
15071505

15081506
$result = $this->dbi->query($sqlQuery);
15091507

libraries/classes/Tracking/Tracker.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,11 @@ public static function handleQuery(string $query): void
616616
return;
617617
}
618618

619-
if ($result['type'] === 'DDL') {
620-
$saveTo = 'schema_sql';
621-
} elseif ($result['type'] === 'DML') {
622-
$saveTo = 'data_sql';
623-
} else {
624-
$saveTo = '';
625-
}
619+
$saveTo = match ($result['type']) {
620+
'DDL' => 'schema_sql',
621+
'DML' => 'data_sql',
622+
default => '',
623+
};
626624

627625
$date = Util::date('Y-m-d H:i:s');
628626

0 commit comments

Comments
 (0)