Use match expressions in some places.

This commit is contained in:
wn_
2024-11-23 19:18:52 +00:00
parent 43e8864ead
commit 9b0baf9b32
9 changed files with 73 additions and 140 deletions

View File

@@ -241,7 +241,6 @@ class API extends Handler {
$field_raw = (int)clean($_REQUEST["field"]);
$field = "";
$set_to = "";
$additional_fields = "";
switch ($field_raw) {
@@ -265,20 +264,17 @@ class API extends Handler {
break;
};
switch ($mode) {
case 1:
$set_to = "true";
break;
case 0:
$set_to = "false";
break;
case 2:
$set_to = "NOT $field";
break;
}
$set_to = match ($mode) {
0 => 'false',
1 => 'true',
2 => "NOT $field",
default => null,
};
if ($field == "note") $set_to = $this->pdo->quote($data);
if ($field == "score") $set_to = (int) $data;
if ($field == 'note')
$set_to = $this->pdo->quote($data);
elseif ($field == 'score')
$set_to = (int) $data;
if ($field && $set_to && count($article_ids) > 0) {

View File

@@ -424,14 +424,11 @@ class Config {
}
static function cast_to(string $value, int $type_hint): bool|int|string {
switch ($type_hint) {
case self::T_BOOL:
return sql_bool_to_bool($value);
case self::T_INT:
return (int) $value;
default:
return $value;
}
return match ($type_hint) {
self::T_BOOL => sql_bool_to_bool($value),
self::T_INT => (int) $value,
default => $value,
};
}
private function _get(string $param): bool|int|string {

View File

@@ -72,19 +72,15 @@ class FeedParser {
$root = $root_list->item(0);
if ($root) {
switch (mb_strtolower($root->tagName)) {
case "rdf:rdf":
$this->type = $this::FEED_RDF;
break;
case "channel":
$this->type = $this::FEED_RSS;
break;
case "feed":
case "atom:feed":
$this->type = $this::FEED_ATOM;
break;
default:
$this->error ??= "Unknown/unsupported feed type";
$this->type = match (mb_strtolower($root->tagName)) {
'rdf:rdf' => $this::FEED_RDF,
'channel' => $this::FEED_RSS,
'feed', 'atom:feed' => $this::FEED_ATOM,
default => null,
};
if (!$this->type) {
$this->error ??= 'Unknown/unsupported feed type';
return;
}
}

View File

@@ -397,23 +397,14 @@ class Feeds extends Handler_Protected {
if ($query_error_override) {
$message = $query_error_override;
} else {
switch ($view_mode) {
case "unread":
$message = __("No unread articles found to display.");
break;
case "updated":
$message = __("No updated articles found to display.");
break;
case "marked":
$message = __("No starred articles found to display.");
break;
default:
if ($feed < LABEL_BASE_INDEX) {
$message = __("No articles found to display. You can assign articles to labels manually from article header context menu (applies to all selected articles) or use a filter.");
} else {
$message = __("No articles found to display.");
}
}
$message = match ($view_mode) {
'unread' => __('No unread articles found to display.'),
'updated' => __('No updated articles found to display.'),
'marked' => __('No starred articles found to display.'),
default => $feed < LABEL_BASE_INDEX ?
__('No articles found to display. You can assign articles to labels manually from article header context menu (applies to all selected articles) or use a filter.')
: __('No articles found to display.'),
};
}
if (!$offset && $message) {
@@ -1153,26 +1144,15 @@ class Feeds extends Handler_Protected {
* @return false|string false if the icon ID was unrecognized, otherwise, the icon identifier string
*/
static function _get_icon(int $id): false|string {
switch ($id) {
case Feeds::FEED_ARCHIVED:
return "archive";
case Feeds::FEED_STARRED:
return "star";
case Feeds::FEED_PUBLISHED:
return "rss_feed";
case Feeds::FEED_FRESH:
return "whatshot";
case Feeds::FEED_ALL:
return "inbox";
case Feeds::FEED_RECENTLY_READ:
return "restore";
default:
if ($id < LABEL_BASE_INDEX) {
return "label";
} else {
return self::_get_icon_url($id);
}
}
return match ($id) {
Feeds::FEED_ARCHIVED => 'archive',
Feeds::FEED_STARRED => 'star',
Feeds::FEED_PUBLISHED => 'rss_feed',
Feeds::FEED_FRESH => 'whatshot',
Feeds::FEED_ALL => 'inbox',
Feeds::FEED_RECENTLY_READ => 'restore',
default => $id < LABEL_BASE_INDEX ? 'label' : self::_get_icon_url($id),
};
}
/**

View File

@@ -57,19 +57,12 @@ class Logger {
}
function __construct() {
switch (Config::get(Config::LOG_DESTINATION)) {
case self::LOG_DEST_SQL:
$this->adapter = new Logger_SQL();
break;
case self::LOG_DEST_SYSLOG:
$this->adapter = new Logger_Syslog();
break;
case self::LOG_DEST_STDOUT:
$this->adapter = new Logger_Stdout();
break;
default:
$this->adapter = null;
}
$this->adapter = match (Config::get(Config::LOG_DESTINATION)) {
self::LOG_DEST_SQL => new Logger_SQL(),
self::LOG_DEST_SYSLOG => new Logger_Syslog(),
self::LOG_DEST_STDOUT => new Logger_Stdout(),
default => null,
};
if ($this->adapter && !implements_interface($this->adapter, "Logger_Adapter"))
user_error("Adapter for LOG_DESTINATION: " . Config::LOG_DESTINATION . " does not implement required interface.", E_USER_ERROR);

View File

@@ -590,19 +590,12 @@ class OPML extends Handler_Protected {
$dst_cat_id = $cat_id;
}
switch ($cat_title) {
case "tt-rss-prefs":
$this->opml_import_preference($node, $owner_uid, $nest+1);
break;
case "tt-rss-labels":
$this->opml_import_label($node, $owner_uid, $nest+1);
break;
case "tt-rss-filters":
$this->opml_import_filter($node, $owner_uid, $nest+1);
break;
default:
$this->opml_import_feed($node, $dst_cat_id, $owner_uid, $nest+1);
}
match ($cat_title) {
'tt-rss-prefs' => $this->opml_import_preference($node, $owner_uid, $nest+1),
'tt-rss-labels' => $this->opml_import_label($node, $owner_uid, $nest+1),
'tt-rss-filters' => $this->opml_import_filter($node, $owner_uid, $nest+1),
default => $this->opml_import_feed($node, $dst_cat_id, $owner_uid, $nest+1),
};
}
}
}

View File

@@ -38,16 +38,11 @@ class Pref_System extends Handler_Administrative {
}
private function _log_viewer(int $page, int $severity): void {
$errno_values = [];
switch ($severity) {
case E_USER_ERROR:
$errno_values = [ E_ERROR, E_USER_ERROR, E_PARSE, E_COMPILE_ERROR ];
break;
case E_USER_WARNING:
$errno_values = [ E_ERROR, E_USER_ERROR, E_PARSE, E_COMPILE_ERROR, E_WARNING, E_USER_WARNING, E_DEPRECATED, E_USER_DEPRECATED ];
break;
}
$errno_values = match ($severity) {
E_USER_ERROR => [E_ERROR, E_USER_ERROR, E_PARSE, E_COMPILE_ERROR],
E_USER_WARNING => [E_ERROR, E_USER_ERROR, E_PARSE, E_COMPILE_ERROR, E_WARNING, E_USER_WARNING, E_DEPRECATED, E_USER_DEPRECATED],
default => [],
};
if (count($errno_values) > 0) {
$errno_qmarks = arr_qmarks($errno_values);

View File

@@ -785,16 +785,11 @@ class RPC extends Handler_Protected {
if (strlen($keys[$i]) > 1) {
$tmp = '';
foreach (str_split($keys[$i]) as $c) {
switch ($c) {
case '*':
$tmp .= __('Shift') . '+';
break;
case '^':
$tmp .= __('Ctrl') . '+';
break;
default:
$tmp .= $c;
}
$tmp .= match ($c) {
'*' => __('Shift') . '+',
'^' => __('Ctrl') . '+',
default => $c,
};
}
$keys[$i] = $tmp;
}

View File

@@ -383,30 +383,18 @@ class UserHelper {
* @return false|string False if the password couldn't be hashed, otherwise the hash string.
*/
static function hash_password(string $pass, string $salt, string $algo = self::HASH_ALGOS[0]): false|string {
$pass_hash = "";
$pass_hash = match ($algo) {
self::HASH_ALGO_SHA1 => sha1($pass),
self::HASH_ALGO_SHA1X => sha1("$salt:$pass"),
self::HASH_ALGO_MODE2, self::HASH_ALGO_SSHA256 => hash('sha256', $salt . $pass),
self::HASH_ALGO_SSHA512 => hash('sha512', $salt . $pass),
default => null,
};
switch ($algo) {
case self::HASH_ALGO_SHA1:
$pass_hash = sha1($pass);
break;
case self::HASH_ALGO_SHA1X:
$pass_hash = sha1("$salt:$pass");
break;
case self::HASH_ALGO_MODE2:
case self::HASH_ALGO_SSHA256:
$pass_hash = hash('sha256', $salt . $pass);
break;
case self::HASH_ALGO_SSHA512:
$pass_hash = hash('sha512', $salt . $pass);
break;
default:
user_error("hash_password: unknown hash algo: $algo", E_USER_ERROR);
}
if ($pass_hash === null)
user_error("hash_password: unknown hash algo: $algo", E_USER_ERROR);
if ($pass_hash)
return "$algo:$pass_hash";
else
return false;
return $pass_hash ? "$algo:$pass_hash" : false;
}
/**