From 4cea325b6c0dd57d3d0da6fb3ab8c31119ca8e86 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Tue, 21 Nov 2023 09:34:52 +0700 Subject: [PATCH] Replaced some 'switch' with the 'match' expression (#1147) * Replaced some 'switch' with the 'match' expression * Update CHANGELOG.md --- CHANGELOG.md | 1 + library/ajax/post_mod_comment.php | 24 ++---- .../attach_mod/includes/functions_thumbs.php | 31 +++----- library/includes/functions.php | 73 +++++-------------- posting.php | 24 ++---- privmsg.php | 20 ++--- src/Ajax.php | 14 ++-- src/Legacy/Cache/Common.php | 14 ++-- src/Legacy/Datastore/Common.php | 14 ++-- src/Legacy/SqlDb.php | 14 ++-- viewtopic.php | 24 ++---- 11 files changed, 77 insertions(+), 176 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb825eab1..4dd9c1969 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **Merged pull requests:** +- Replaced some 'switch' with the 'match' expression [\#1147](https://github.com/torrentpier/torrentpier/pull/1147) ([belomaxorka](https://github.com/belomaxorka)) - Vote button code improvements [\#1140](https://github.com/torrentpier/torrentpier/pull/1140), [\#1142](https://github.com/torrentpier/torrentpier/pull/1142), [\#1143](https://github.com/torrentpier/torrentpier/pull/1143), [\#1146](https://github.com/torrentpier/torrentpier/pull/1146) ([belomaxorka](https://github.com/belomaxorka)) - Vote button and v2 file list topic url display [\#1138](https://github.com/torrentpier/torrentpier/pull/1138) ([kovalensky](https://github.com/kovalensky)) - Removed topic watch useless code [\#1137](https://github.com/torrentpier/torrentpier/pull/1137) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/library/ajax/post_mod_comment.php b/library/ajax/post_mod_comment.php index 17e12d467..d2cdd6174 100644 --- a/library/ajax/post_mod_comment.php +++ b/library/ajax/post_mod_comment.php @@ -46,23 +46,13 @@ if ($mc_type && $post['poster_id'] != $userdata['user_id']) { \TorrentPier\Sessions::cache_rm_user_sessions($post['poster_id']); } -switch ($mc_type) { - case 1: // Комментарий - $mc_class = 'success'; - break; - case 2: // Информация - $mc_class = 'info'; - break; - case 3: // Предупреждение - $mc_class = 'warning'; - break; - case 4: // Нарушение - $mc_class = 'danger'; - break; - default: - $mc_class = ''; - break; -} +$mc_class = match ($mc_type) { + 1 => 'success', + 2 => 'info', + 3 => 'warning', + 4 => 'danger', + default => '', +}; $this->response['mc_type'] = $mc_type; $this->response['post_id'] = $post_id; diff --git a/library/attach_mod/includes/functions_thumbs.php b/library/attach_mod/includes/functions_thumbs.php index b96a4db5c..cd37fc771 100644 --- a/library/attach_mod/includes/functions_thumbs.php +++ b/library/attach_mod/includes/functions_thumbs.php @@ -132,28 +132,15 @@ function create_thumbnail($source, $new_file, $mimetype) $type = get_supported_image_types($type); if ($type['gd']) { - switch ($type['format']) { - case IMG_GIF: - $image = imagecreatefromgif($source); - break; - case IMG_JPG: - $image = imagecreatefromjpeg($source); - break; - case IMG_PNG: - $image = imagecreatefrompng($source); - break; - case IMG_BMP: - $image = imagecreatefrombmp($source); - break; - case IMG_WBMP: - $image = imagecreatefromwbmp($source); - break; - case IMG_WEBP: - $image = imagecreatefromwebp($source); - break; - default: - throw new Exception('Unknown file format: ' . $type['format']); - } + $image = match ($type['format']) { + IMG_GIF => imagecreatefromgif($source), + IMG_JPG => imagecreatefromjpeg($source), + IMG_PNG => imagecreatefrompng($source), + IMG_BMP => imagecreatefrombmp($source), + IMG_WBMP => imagecreatefromwbmp($source), + IMG_WEBP => imagecreatefromwebp($source), + default => throw new Exception('Unknown file format: ' . $type['format']), + }; if ($type['version'] == 1 || !$attach_config['use_gd2']) { $new_image = imagecreate($new_width, $new_height); diff --git a/library/includes/functions.php b/library/includes/functions.php index ac4f9bf22..1c6d32056 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -384,30 +384,14 @@ function auth($type, $forum_id, $ug_data, array $f_access = [], $group_perm = UG if (!isset($f_data[$auth_type])) { continue; } - switch ($f_data[$auth_type]) { - case AUTH_ALL: - $auth[$f_id][$auth_type] = true; - break; - - case AUTH_REG: - $auth[$f_id][$auth_type] = !$is_guest; - break; - - case AUTH_ACL: - $auth[$f_id][$auth_type] = (auth_check('forum_perm', $auth_type, $u_access, $f_id, $is_admin) || $auth[$f_id]['auth_mod']); - break; - - case AUTH_MOD: - $auth[$f_id][$auth_type] = $auth[$f_id]['auth_mod']; - break; - - case AUTH_ADMIN: - $auth[$f_id][$auth_type] = $is_admin; - break; - - default: - $auth[$f_id][$auth_type] = false; - } + $auth[$f_id][$auth_type] = match ($f_data[$auth_type]) { + AUTH_ALL => true, + AUTH_REG => !$is_guest, + AUTH_ACL => (auth_check('forum_perm', $auth_type, $u_access, $f_id, $is_admin) || $auth[$f_id]['auth_mod']), + AUTH_MOD => $auth[$f_id]['auth_mod'], + AUTH_ADMIN => $is_admin, + default => false, + }; if ($add_auth_type_desc) { $auth[$f_id][$auth_type . '_type'] =& $lang['AUTH_TYPES'][$f_data[$auth_type]]; } @@ -945,22 +929,13 @@ function get_db_stat($mode) $row = DB()->sql_fetchrow($result); - switch ($mode) { - case 'usercount': - return $row['total']; - break; - case 'newestuser': - return $row; - break; - case 'postcount': - return $row['post_total']; - break; - case 'topiccount': - return $row['topic_total']; - break; - } - - return false; + return match ($mode) { + 'usercount' => $row['total'], + 'newestuser' => $row, + 'postcount' => $row['post_total'], + 'topiccount' => $row['topic_total'], + default => false, + }; } function clean_username($username) @@ -2001,19 +1976,11 @@ function gender_image($gender): string return $user_gender; } - switch ($gender) { - case MALE: - $user_gender = '' . $lang['GENDER_SELECT'][MALE] . ''; - break; - case FEMALE: - $user_gender = '' . $lang['GENDER_SELECT'][FEMALE] . ''; - break; - default: - $user_gender = '' . $lang['GENDER_SELECT'][NOGENDER] . ''; - break; - } - - return $user_gender; + return match ($gender) { + MALE => '' . $lang['GENDER_SELECT'][MALE] . '', + FEMALE => '' . $lang['GENDER_SELECT'][FEMALE] . '', + default => '' . $lang['GENDER_SELECT'][NOGENDER] . '', + }; } function is_gold($type): string diff --git a/posting.php b/posting.php index 84d457a96..b83712c8c 100644 --- a/posting.php +++ b/posting.php @@ -205,23 +205,13 @@ if (!$is_auth[$is_auth_type]) { bb_die(sprintf($lang['SORRY_' . strtoupper($is_auth_type)], $is_auth[$is_auth_type . '_type'])); } - switch ($mode) { - case 'newtopic': - $redirect = "mode=newtopic&f=$forum_id"; - break; - case 'new_rel': - $redirect = "mode=new_rel&f=$forum_id"; - break; - case 'reply': - $redirect = "mode=reply&t=$topic_id"; - break; - case 'quote': - case 'editpost': - $redirect = "mode=quote&p=$post_id"; - break; - default: - $redirect = ''; - } + $redirect = match ($mode) { + 'newtopic' => "mode=newtopic&f=$forum_id", + 'new_rel' => "mode=new_rel&f=$forum_id", + 'reply' => "mode=reply&t=$topic_id", + 'quote', 'editpost' => "mode=quote&p=$post_id", + default => '', + }; redirect(LOGIN_URL . "?redirect=/" . POSTING_URL . "?$redirect"); } diff --git a/privmsg.php b/privmsg.php index 6755d28e9..eadb7617c 100644 --- a/privmsg.php +++ b/privmsg.php @@ -1335,20 +1335,12 @@ if ($mode == 'read') { $template->assign_var('PM_BOX_SIZE_INFO'); - switch ($folder) { - case 'inbox': - $l_box_size_status = sprintf($lang['INBOX_SIZE'], $box_limit_percent); - break; - case 'sentbox': - $l_box_size_status = sprintf($lang['SENTBOX_SIZE'], $box_limit_percent); - break; - case 'savebox': - $l_box_size_status = sprintf($lang['SAVEBOX_SIZE'], $box_limit_percent); - break; - default: - $l_box_size_status = ''; - break; - } + $l_box_size_status = match ($folder) { + 'inbox' => sprintf($lang['INBOX_SIZE'], $box_limit_percent), + 'sentbox' => sprintf($lang['SENTBOX_SIZE'], $box_limit_percent), + 'savebox' => sprintf($lang['SAVEBOX_SIZE'], $box_limit_percent), + default => '', + }; } // diff --git a/src/Ajax.php b/src/Ajax.php index 08bbd4087..00d0f8452 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -293,15 +293,11 @@ class Ajax } foreach (debug_backtrace() as $trace) { if (!empty($trace['file']) && $trace['file'] !== __FILE__) { - switch ($mode) { - case 'file': - return $trace['file']; - case 'line': - return $trace['line']; - case 'all': - default: - return hide_bb_path($trace['file']) . '(' . $trace['line'] . ')'; - } + return match ($mode) { + 'file' => $trace['file'], + 'line' => $trace['line'], + default => hide_bb_path($trace['file']) . '(' . $trace['line'] . ')', + }; } } return 'src not found'; diff --git a/src/Legacy/Cache/Common.php b/src/Legacy/Cache/Common.php index 04f6e96a3..3acaf9b55 100644 --- a/src/Legacy/Cache/Common.php +++ b/src/Legacy/Cache/Common.php @@ -100,15 +100,11 @@ class Common } foreach (debug_backtrace() as $trace) { if (!empty($trace['file']) && $trace['file'] !== __FILE__) { - switch ($mode) { - case 'file': - return $trace['file']; - case 'line': - return $trace['line']; - case 'all': - default: - return hide_bb_path($trace['file']) . '(' . $trace['line'] . ')'; - } + return match ($mode) { + 'file' => $trace['file'], + 'line' => $trace['line'], + default => hide_bb_path($trace['file']) . '(' . $trace['line'] . ')', + }; } } return 'src not found'; diff --git a/src/Legacy/Datastore/Common.php b/src/Legacy/Datastore/Common.php index ba6c92a75..c5c324586 100644 --- a/src/Legacy/Datastore/Common.php +++ b/src/Legacy/Datastore/Common.php @@ -174,15 +174,11 @@ class Common } foreach (debug_backtrace() as $trace) { if (!empty($trace['file']) && $trace['file'] !== __FILE__) { - switch ($mode) { - case 'file': - return $trace['file']; - case 'line': - return $trace['line']; - case 'all': - default: - return hide_bb_path($trace['file']) . '(' . $trace['line'] . ')'; - } + return match ($mode) { + 'file' => $trace['file'], + 'line' => $trace['line'], + default => hide_bb_path($trace['file']) . '(' . $trace['line'] . ')', + }; } } return 'src not found'; diff --git a/src/Legacy/SqlDb.php b/src/Legacy/SqlDb.php index d2130d159..6838f783a 100644 --- a/src/Legacy/SqlDb.php +++ b/src/Legacy/SqlDb.php @@ -854,15 +854,11 @@ class SqlDb } foreach (debug_backtrace() as $trace) { if (!empty($trace['file']) && $trace['file'] !== __FILE__) { - switch ($mode) { - case 'file': - return $trace['file']; - case 'line': - return $trace['line']; - case 'all': - default: - return hide_bb_path($trace['file']) . '(' . $trace['line'] . ')'; - } + return match ($mode) { + 'file' => $trace['file'], + 'line' => $trace['line'], + default => hide_bb_path($trace['file']) . '(' . $trace['line'] . ')', + }; } } return 'src not found'; diff --git a/viewtopic.php b/viewtopic.php index 9c28ec2c1..91b138bf5 100644 --- a/viewtopic.php +++ b/viewtopic.php @@ -666,23 +666,13 @@ for ($i = 0; $i < $total_posts; $i++) { $pg_row_class = !($i % 2) ? 'row2' : 'row1'; // Mod comment - switch ($mc_type) { - case 1: // Комментарий - $mc_class = 'success'; - break; - case 2: // Информация - $mc_class = 'info'; - break; - case 3: // Предупреждение - $mc_class = 'warning'; - break; - case 4: // Нарушение - $mc_class = 'danger'; - break; - default: - $mc_class = ''; - break; - } + $mc_class = match ($mc_type) { + 1 => 'success', + 2 => 'info', + 3 => 'warning', + 4 => 'danger', + default => '', + }; $mc_select_type = []; foreach ($lang['MC_COMMENT'] as $key => $value) { $mc_select_type[$key] = $value['type'];