From 93fd85df6f73732d3a6ed280d26224e1877c954f Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Aug 2022 14:13:26 +0000 Subject: Switch to direct type declarations of class properties. --- classes/db/migrations.php | 36 +++++++++--------------------------- classes/debug.php | 31 +++++++++---------------------- classes/diskcache.php | 6 ++---- classes/handler.php | 6 ++---- classes/mailer.php | 4 +--- classes/pluginhost.php | 33 +++++++++++++-------------------- classes/pref/prefs.php | 10 ++++------ classes/urlhelper.php | 33 ++++++++------------------------- 8 files changed, 48 insertions(+), 111 deletions(-) (limited to 'classes') diff --git a/classes/db/migrations.php b/classes/db/migrations.php index aecd9186c..5f969c513 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -1,33 +1,15 @@ pdo = Db::pdo(); diff --git a/classes/debug.php b/classes/debug.php index 4777e8c74..40fa27377 100644 --- a/classes/debug.php +++ b/classes/debug.php @@ -12,44 +12,31 @@ class Debug { Debug::LOG_EXTENDED, ]; - // TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ /** * @deprecated - * @var int */ - public static $LOG_DISABLED = self::LOG_DISABLED; + public static int $LOG_DISABLED = self::LOG_DISABLED; /** * @deprecated - * @var int */ - public static $LOG_NORMAL = self::LOG_NORMAL; + public static int $LOG_NORMAL = self::LOG_NORMAL; /** * @deprecated - * @var int */ - public static $LOG_VERBOSE = self::LOG_VERBOSE; + public static int $LOG_VERBOSE = self::LOG_VERBOSE; /** * @deprecated - * @var int */ - public static $LOG_EXTENDED = self::LOG_EXTENDED; + public static int $LOG_EXTENDED = self::LOG_EXTENDED; - /** @var bool */ - private static $enabled = false; + private static bool $enabled = false; + private static bool $quiet = false; + private static ?string $logfile = null; - /** @var bool */ - private static $quiet = false; - - /** @var string|null */ - private static $logfile = null; - - /** - * @var int Debug::LOG_* - */ - private static $loglevel = self::LOG_NORMAL; + private static int $loglevel = self::LOG_NORMAL; public static function set_logfile(string $logfile): void { self::$logfile = $logfile; @@ -70,7 +57,7 @@ class Debug { /** * @param Debug::LOG_* $level */ - public static function set_loglevel($level): void { + public static function set_loglevel(int $level): void { self::$loglevel = $level; } diff --git a/classes/diskcache.php b/classes/diskcache.php index 34bba25f1..01c713b99 100644 --- a/classes/diskcache.php +++ b/classes/diskcache.php @@ -1,15 +1,13 @@ */ - private $mimeMap = [ + private array $mimeMap = [ 'video/3gpp2' => '3g2', 'video/3gp' => '3gp', 'video/3gpp' => '3gp', diff --git a/classes/handler.php b/classes/handler.php index 806c9cfbe..5b54570d8 100644 --- a/classes/handler.php +++ b/classes/handler.php @@ -1,11 +1,9 @@ */ - protected $args; + protected array $args; /** * @param array $args diff --git a/classes/mailer.php b/classes/mailer.php index 60b1ce4fd..76c9abf8e 100644 --- a/classes/mailer.php +++ b/classes/mailer.php @@ -1,8 +1,6 @@ $params diff --git a/classes/pluginhost.php b/classes/pluginhost.php index 6ab4ac806..4c71d0cef 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -1,49 +1,42 @@ >> hook types -> priority levels -> Plugins */ - private $hooks = []; + private array $hooks = []; /** @var array */ - private $plugins = []; + private array $plugins = []; /** @var array> handler type -> method type -> Plugin */ - private $handlers = []; + private array $handlers = []; /** @var array command type -> details array */ - private $commands = []; + private array $commands = []; /** @var array> plugin name -> (potential profile array) -> key -> value */ - private $storage = []; + private array $storage = []; /** @var array> */ - private $feeds = []; + private array $feeds = []; /** @var array API method name, Plugin sender */ - private $api_methods = []; + private array $api_methods = []; /** @var array> */ - private $plugin_actions = []; + private array $plugin_actions = []; - /** @var int|null */ - private $owner_uid = null; + private ?int $owner_uid = null; - /** @var bool */ - private $data_loaded = false; + private bool $data_loaded = false; - /** @var PluginHost|null */ - private static $instance = null; + private static ?PluginHost $instance = null; const API_VERSION = 2; const PUBLIC_METHOD_DELIMITER = "--"; diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 7acd06aa4..3e651297e 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -2,18 +2,17 @@ use chillerlan\QRCode; class Pref_Prefs extends Handler_Protected { - // TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ /** @var array> */ - private $pref_help = []; + private array $pref_help = []; /** @var array> pref items are Prefs::*|Pref_Prefs::BLOCK_SEPARATOR (PHPStan was complaining) */ - private $pref_item_map = []; + private array $pref_item_map = []; /** @var array */ - private $pref_help_bottom = []; + private array $pref_help_bottom = []; /** @var array */ - private $pref_blacklist = []; + private array $pref_blacklist = []; private const BLOCK_SEPARATOR = 'BLOCK_SEPARATOR'; @@ -26,7 +25,6 @@ class Pref_Prefs extends Handler_Protected { const PI_ERR_PLUGIN_NOT_FOUND = "PI_ERR_PLUGIN_NOT_FOUND"; const PI_ERR_NO_WORKDIR = "PI_ERR_NO_WORKDIR"; - /** @param string $method */ function csrf_ignore(string $method) : bool { $csrf_ignored = array("index", "updateself", "otpqrcode"); diff --git a/classes/urlhelper.php b/classes/urlhelper.php index bb51f5d06..da812d422 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -10,31 +10,14 @@ class UrlHelper { "application/x-bittorrent" => [ "magnet" ], ]; - // TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+ - /** @var string */ - static $fetch_last_error; - - /** @var int */ - static $fetch_last_error_code; - - /** @var string */ - static $fetch_last_error_content; - - /** @var string */ - static $fetch_last_content_type; - - /** @var string */ - static $fetch_last_modified; - - - /** @var string */ - static $fetch_effective_url; - - /** @var string */ - static $fetch_effective_ip_addr; - - /** @var bool */ - static $fetch_curl_used; + static string $fetch_last_error; + static int $fetch_last_error_code; + static string $fetch_last_error_content; + static string $fetch_last_content_type; + static string $fetch_last_modified; + static string $fetch_effective_url; + static string $fetch_effective_ip_addr; + static bool $fetch_curl_used; /** * @param array $parts -- cgit v1.2.3 From 7567676ed8c2d72bdff626380ee038bdc2dbb39c Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Aug 2022 14:16:40 +0000 Subject: Remove a PHP < 7.1 branch in UrlHelper. --- classes/urlhelper.php | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'classes') diff --git a/classes/urlhelper.php b/classes/urlhelper.php index da812d422..5ccaaf2da 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -190,32 +190,26 @@ class UrlHelper { if ($nest > 10) return false; - if (version_compare(PHP_VERSION, '7.1.0', '>=')) { - $context_options = array( - 'http' => array( - 'header' => array( - 'Connection: close' - ), - 'method' => 'HEAD', - 'timeout' => $timeout, - 'protocol_version'=> 1.1) - ); - - if (Config::get(Config::HTTP_PROXY)) { - $context_options['http']['request_fulluri'] = true; - $context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY); - } + $context_options = array( + 'http' => array( + 'header' => array( + 'Connection: close' + ), + 'method' => 'HEAD', + 'timeout' => $timeout, + 'protocol_version'=> 1.1) + ); + + if (Config::get(Config::HTTP_PROXY)) { + $context_options['http']['request_fulluri'] = true; + $context_options['http']['proxy'] = Config::get(Config::HTTP_PROXY); + } - $context = stream_context_create($context_options); + $context = stream_context_create($context_options); - // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0 - // @phpstan-ignore-next-line - $headers = get_headers($url, 0, $context); - } else { - // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.1.0 - // @phpstan-ignore-next-line - $headers = get_headers($url, 0); - } + // PHP 8 changed the second param from int to bool, but we still support PHP >= 7.4.0 + // @phpstan-ignore-next-line + $headers = get_headers($url, 0, $context); if (is_array($headers)) { $headers = array_reverse($headers); // last one is the correct one -- cgit v1.2.3 From a63c949a5522a8fdeae2dfa77bb39565727f7a0a Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Aug 2022 14:41:21 +0000 Subject: Use arrow functions in some places. --- classes/db/migrations.php | 13 +++++-------- classes/digest.php | 4 +--- classes/pref/prefs.php | 10 ++++------ classes/rssutils.php | 6 +++--- 4 files changed, 13 insertions(+), 20 deletions(-) (limited to 'classes') diff --git a/classes/db/migrations.php b/classes/db/migrations.php index 5f969c513..ffce2af5f 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -189,14 +189,11 @@ class Db_Migrations { $filename = "{$this->base_path}/{$this->base_filename}"; if (file_exists($filename)) { - $lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)), - function ($line) { - return strlen(trim($line)) > 0 && strpos($line, "--") !== 0; - }); - - return array_filter(explode(";", implode("", $lines)), function ($line) { - return strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]); - }); + $lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)), + fn($line) => strlen(trim($line)) > 0 && strpos($line, "--") !== 0); + + return array_filter(explode(";", implode("", $lines)), + fn($line) => strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"])); } else { user_error("Requested schema file ${filename} not found.", E_USER_ERROR); diff --git a/classes/digest.php b/classes/digest.php index 15203166b..3e943e6dd 100644 --- a/classes/digest.php +++ b/classes/digest.php @@ -163,9 +163,7 @@ class Digest $article_labels_formatted = ""; if (is_array($article_labels) && count($article_labels) > 0) { - $article_labels_formatted = implode(", ", array_map(function($a) { - return $a[1]; - }, $article_labels)); + $article_labels_formatted = implode(", ", array_map(fn($a) => $a[1], $article_labels)); } $tpl->setVariable('FEED_TITLE', $line["feed_title"]); diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 3e651297e..01b58005b 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -872,13 +872,13 @@ class Pref_Prefs extends Handler_Protected { PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_PARSED), PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FETCH_FEED)); - $feed_handlers = array_filter($feed_handlers, function($plugin) use ($feed_handler_whitelist) { - return in_array(get_class($plugin), $feed_handler_whitelist) === false; }); + $feed_handlers = array_filter($feed_handlers, + fn($plugin) => in_array(get_class($plugin), $feed_handler_whitelist) === false); if (count($feed_handlers) > 0) { print_error( T_sprintf("The following plugins use per-feed content hooks. This may cause excessive data usage and origin server load resulting in a ban of your instance: %s" , - implode(", ", array_map(function($plugin) { return get_class($plugin); }, $feed_handlers)) + implode(", ", array_map(fn($plugin) => get_class($plugin), $feed_handlers)) ) . " (".__("More info...").")" ); } @@ -1067,9 +1067,7 @@ class Pref_Prefs extends Handler_Protected { } } - $rv = array_values(array_filter($rv, function ($item) { - return $item["rv"]["need_update"]; - })); + $rv = array_values(array_filter($rv, fn($item) => $item["rv"]["need_update"])); return $rv; } diff --git a/classes/rssutils.php b/classes/rssutils.php index e039284f2..e590f1665 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -39,7 +39,7 @@ class RSSUtils { // check icon files once every Config::get(Config::CACHE_MAX_DAYS) days $icon_files = array_filter(glob(Config::get(Config::ICONS_DIR) . "/*.ico"), - function($f) { return filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS); }); + fn($f) => filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS)); foreach ($icon_files as $icon) { $feed_id = basename($icon, ".ico"); @@ -865,7 +865,7 @@ class RSSUtils { $pluginhost->run_hooks(PluginHost::HOOK_FILTER_TRIGGERED, $feed, $feed_obj->owner_uid, $article, $matched_filters, $matched_rules, $article_filters); - $matched_filter_ids = array_map(function($f) { return $f['id']; }, $matched_filters); + $matched_filter_ids = array_map(fn($f) => $f['id'], $matched_filters); if (count($matched_filter_ids) > 0) { $filter_objs = ORM::for_table('ttrss_filters2') @@ -1801,7 +1801,7 @@ class RSSUtils { [$cat_id]); $check_cats_str = join(",", $check_cats); - $check_cats_fullids = array_map(function($a) { return "CAT:$a"; }, $check_cats); + $check_cats_fullids = array_map(fn($a) => "CAT:$a", $check_cats); while ($line = $sth->fetch()) { $filter_id = $line["id"]; -- cgit v1.2.3 From 3487c922b3f34449fecdddebe1fd2ee3b8c42e65 Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Aug 2022 15:31:19 +0000 Subject: Replace use of 'array_merge' with the spread operator and 'array_push' in various places. This isn't supported for arrays with string keys until PHP 8.1. https://wiki.php.net/rfc/spread_operator_for_array --- classes/api.php | 2 +- classes/article.php | 4 ++-- classes/counters.php | 38 +++++++++++++++++--------------------- classes/feeditem/atom.php | 2 +- classes/feeditem/common.php | 2 +- classes/feeditem/rss.php | 2 +- classes/feeds.php | 17 +++++++++-------- classes/mailer.php | 2 +- classes/pluginhost.php | 4 ++-- classes/pref/feeds.php | 6 +++--- classes/pref/filters.php | 6 +++--- classes/pref/prefs.php | 18 ++++++++++-------- classes/rpc.php | 6 +++--- classes/rssutils.php | 15 ++++++--------- 14 files changed, 60 insertions(+), 64 deletions(-) (limited to 'classes') diff --git a/classes/api.php b/classes/api.php index b17114693..0e873856f 100755 --- a/classes/api.php +++ b/classes/api.php @@ -286,7 +286,7 @@ class API extends Handler { $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_qmarks) AND owner_uid = ?"); - $sth->execute(array_merge($article_ids, [$_SESSION['uid']])); + $sth->execute([...$article_ids, $_SESSION['uid']]); $num_updated = $sth->rowCount(); diff --git a/classes/article.php b/classes/article.php index e113ed219..17a40ca4f 100755 --- a/classes/article.php +++ b/classes/article.php @@ -177,7 +177,7 @@ class Article extends Handler_Protected { $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET score = ? WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); - $sth->execute(array_merge([$score], $ids, [$_SESSION['uid']])); + $sth->execute([$score, ...$ids, $_SESSION['uid']]); print json_encode(["id" => $ids, "score" => $score]); } @@ -507,7 +507,7 @@ class Article extends Handler_Protected { WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); } - $sth->execute(array_merge($ids, [$owner_uid])); + $sth->execute([...$ids, $owner_uid]); } /** diff --git a/classes/counters.php b/classes/counters.php index 8756b5acf..bc4d2d4a3 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -5,13 +5,13 @@ class Counters { * @return array> */ static function get_all(): array { - return array_merge( - self::get_global(), - self::get_virt(), - self::get_labels(), - self::get_feeds(), - self::get_cats() - ); + return [ + ...self::get_global(), + ...self::get_virt(), + ...self::get_labels(), + ...self::get_feeds(), + ...self::get_cats(), + ]; } /** @@ -20,13 +20,13 @@ class Counters { * @return array> */ static function get_conditional(array $feed_ids = null, array $label_ids = null): array { - return array_merge( - self::get_global(), - self::get_virt(), - self::get_labels($label_ids), - self::get_feeds($feed_ids), - self::get_cats(is_array($feed_ids) ? Feeds::_cats_of($feed_ids, $_SESSION["uid"], true) : null) - ); + return [ + ...self::get_global(), + ...self::get_virt(), + ...self::get_labels($label_ids), + ...self::get_feeds($feed_ids), + ...self::get_cats(is_array($feed_ids) ? Feeds::_cats_of($feed_ids, $_SESSION["uid"], true) : null) + ]; } /** @@ -93,11 +93,7 @@ class Counters { ue.feed_id = f.id AND ue.owner_uid = ?"); - $sth->execute(array_merge( - [$_SESSION['uid']], - $cat_ids, - [$_SESSION['uid']] - )); + $sth->execute([$_SESSION['uid'], ...$cat_ids, $_SESSION['uid']]); } else { $sth = $pdo->prepare("SELECT fc.id, @@ -170,7 +166,7 @@ class Counters { WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks) GROUP BY f.id"); - $sth->execute(array_merge([$_SESSION['uid']], $feed_ids)); + $sth->execute([$_SESSION['uid'], ...$feed_ids]); } else { $sth = $pdo->prepare("SELECT f.id, f.title, @@ -319,7 +315,7 @@ class Counters { LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = ? WHERE ttrss_labels2.owner_uid = ? AND ttrss_labels2.id IN ($label_ids_qmarks) GROUP BY ttrss_labels2.id, ttrss_labels2.caption"); - $sth->execute(array_merge([$_SESSION["uid"], $_SESSION["uid"]], $label_ids)); + $sth->execute([$_SESSION["uid"], $_SESSION["uid"], ...$label_ids]); } else { $sth = $pdo->prepare("SELECT id, caption, diff --git a/classes/feeditem/atom.php b/classes/feeditem/atom.php index 6de790ff9..59bf403b3 100755 --- a/classes/feeditem/atom.php +++ b/classes/feeditem/atom.php @@ -201,7 +201,7 @@ class FeedItem_Atom extends FeedItem_Common { } } - $encs = array_merge($encs, parent::get_enclosures()); + array_push($encs, ...parent::get_enclosures()); return $encs; } diff --git a/classes/feeditem/common.php b/classes/feeditem/common.php index 6a9be8aca..fde481179 100755 --- a/classes/feeditem/common.php +++ b/classes/feeditem/common.php @@ -189,7 +189,7 @@ abstract class FeedItem_Common extends FeedItem { $tmp = []; foreach ($cats as $rawcat) { - $tmp = array_merge($tmp, explode(",", $rawcat)); + array_push($tmp, ...explode(",", $rawcat)); } $tmp = array_map(function($srccat) { diff --git a/classes/feeditem/rss.php b/classes/feeditem/rss.php index 7017d04e9..132eabff5 100755 --- a/classes/feeditem/rss.php +++ b/classes/feeditem/rss.php @@ -153,7 +153,7 @@ class FeedItem_RSS extends FeedItem_Common { array_push($encs, $enc); } - $encs = array_merge($encs, parent::get_enclosures()); + array_push($encs, ...parent::get_enclosures()); return $encs; } diff --git a/classes/feeds.php b/classes/feeds.php index 8981d6f14..afcc97d81 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1938,8 +1938,8 @@ class Feeds extends Handler_Protected { $sth->execute([$cat, $owner_uid]); while ($line = $sth->fetch()) { - array_push($rv, (int)$line["parent_cat"]); - $rv = array_merge($rv, self::_get_parent_cats($line["parent_cat"], $owner_uid)); + $cat = (int) $line["parent_cat"]; + array_push($rv, $cat, ...self::_get_parent_cats($cat, $owner_uid)); } return $rv; @@ -1958,8 +1958,7 @@ class Feeds extends Handler_Protected { $sth->execute([$cat, $owner_uid]); while ($line = $sth->fetch()) { - array_push($rv, $line["id"]); - $rv = array_merge($rv, self::_get_child_cats($line["id"], $owner_uid)); + array_push($rv, $line["id"], ...self::_get_child_cats($line["id"], $owner_uid)); } return $rv; @@ -1980,16 +1979,18 @@ class Feeds extends Handler_Protected { $sth = $pdo->prepare("SELECT DISTINCT cat_id, fc.parent_cat FROM ttrss_feeds f LEFT JOIN ttrss_feed_categories fc ON (fc.id = f.cat_id) WHERE f.owner_uid = ? AND f.id IN ($feeds_qmarks)"); - $sth->execute(array_merge([$owner_uid], $feeds)); + $sth->execute([$owner_uid, ...$feeds]); $rv = []; if ($row = $sth->fetch()) { + $cat_id = (int) $row["cat_id"]; + $rv[] = $cat_id; array_push($rv, (int)$row["cat_id"]); - if ($with_parents && $row["parent_cat"]) - $rv = array_merge($rv, - self::_get_parent_cats($row["cat_id"], $owner_uid)); + if ($with_parents && $row["parent_cat"]) { + array_push($rv, ...self::_get_parent_cats($cat_id, $owner_uid)); + } } $rv = array_unique($rv); diff --git a/classes/mailer.php b/classes/mailer.php index 76c9abf8e..ac5c641eb 100644 --- a/classes/mailer.php +++ b/classes/mailer.php @@ -45,7 +45,7 @@ class Mailer { $headers = [ "From: $from_combined", "Content-Type: text/plain; charset=UTF-8" ]; - $rc = mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); + $rc = mail($to_combined, $subject, $message, implode("\r\n", [...$headers, ...$additional_headers])); if (!$rc) { $this->set_error(error_get_last()['message'] ?? T_sprintf("Unknown error while sending mail. Hooks tried: %d.", $hooks_tried)); diff --git a/classes/pluginhost.php b/classes/pluginhost.php index 4c71d0cef..acc784902 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -405,7 +405,7 @@ class PluginHost { $tmp = []; foreach (array_keys($this->hooks[$type]) as $prio) { - $tmp = array_merge($tmp, $this->hooks[$type][$prio]); + array_push($tmp, ...$this->hooks[$type][$prio]); } return $tmp; @@ -418,7 +418,7 @@ class PluginHost { */ function load_all(int $kind, int $owner_uid = null, bool $skip_init = false): void { - $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*")); + $plugins = [...(glob("plugins/*") ?: []), ...(glob("plugins.local/*") ?: [])]; $plugins = array_filter($plugins, "is_dir"); $plugins = array_map("basename", $plugins); diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index bb638f166..6cf979b0a 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -172,7 +172,7 @@ class Pref_Feeds extends Handler_Protected { if ($enable_cats) { array_push($root['items'], $cat); } else { - $root['items'] = array_merge($root['items'], $cat['items']); + array_push($root['items'], ...$cat['items']); } $sth = $this->pdo->prepare("SELECT * FROM @@ -202,7 +202,7 @@ class Pref_Feeds extends Handler_Protected { if ($enable_cats) { array_push($root['items'], $cat); } else { - $root['items'] = array_merge($root['items'], $cat['items']); + array_push($root['items'], ...$cat['items']); } } } @@ -848,7 +848,7 @@ class Pref_Feeds extends Handler_Protected { if ($qpart) { $sth = $this->pdo->prepare("UPDATE ttrss_feeds SET $qpart WHERE id IN ($feed_ids_qmarks) AND owner_uid = ?"); - $sth->execute(array_merge($feed_ids, [$_SESSION['uid']])); + $sth->execute([...$feed_ids, $_SESSION['uid']]); } } diff --git a/classes/pref/filters.php b/classes/pref/filters.php index e7c7877a7..19ec8d39e 100755 --- a/classes/pref/filters.php +++ b/classes/pref/filters.php @@ -538,7 +538,7 @@ class Pref_Filters extends Handler_Protected { $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks) AND owner_uid = ?"); - $sth->execute(array_merge($ids, [$_SESSION['uid']])); + $sth->execute([...$ids, $_SESSION['uid']]); } private function _save_rules_and_actions(int $filter_id): void { @@ -781,11 +781,11 @@ class Pref_Filters extends Handler_Protected { $sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); - $sth->execute(array_merge([$base_id], $ids)); + $sth->execute([$base_id, ...$ids]); $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); - $sth->execute(array_merge([$base_id], $ids)); + $sth->execute([$base_id, ...$ids]); $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)"); $sth->execute($ids); diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 01b58005b..ce9030cdc 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -631,10 +631,11 @@ class Pref_Prefs extends Handler_Protected { } else if ($pref_name == Prefs::USER_CSS_THEME) { - $theme_files = array_map("basename", - array_merge(glob("themes/*.php"), - glob("themes/*.css"), - glob("themes.local/*.css"))); + $theme_files = array_map("basename", [ + ...glob("themes/*.php") ?: [], + ...glob("themes/*.css") ?: [], + ...glob("themes.local/*.css") ?: [], + ]); asort($theme_files); @@ -867,10 +868,11 @@ class Pref_Prefs extends Handler_Protected { $feed_handler_whitelist = [ "Af_Comics" ]; - $feed_handlers = array_merge( - PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_FETCHED), - PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_PARSED), - PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FETCH_FEED)); + $feed_handlers = [ + ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_FETCHED), + ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_PARSED), + ...PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FETCH_FEED), + ]; $feed_handlers = array_filter($feed_handlers, fn($plugin) => in_array(get_class($plugin), $feed_handler_whitelist) === false); diff --git a/classes/rpc.php b/classes/rpc.php index dbb54cec5..bb14225be 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -77,7 +77,7 @@ class RPC extends Handler_Protected { $sth = $this->pdo->prepare("DELETE FROM ttrss_user_entries WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); - $sth->execute(array_merge($ids, [$_SESSION['uid']])); + $sth->execute([...$ids, $_SESSION['uid']]); Article::_purge_orphans(); @@ -364,7 +364,7 @@ class RPC extends Handler_Protected { WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); } - $sth->execute(array_merge($ids, [$_SESSION['uid']])); + $sth->execute([...$ids, $_SESSION['uid']]); } /** @@ -388,7 +388,7 @@ class RPC extends Handler_Protected { WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?"); } - $sth->execute(array_merge($ids, [$_SESSION['uid']])); + $sth->execute([...$ids, $_SESSION['uid']]); } function log(): void { diff --git a/classes/rssutils.php b/classes/rssutils.php index e590f1665..fe295417a 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -39,7 +39,7 @@ class RSSUtils { // check icon files once every Config::get(Config::CACHE_MAX_DAYS) days $icon_files = array_filter(glob(Config::get(Config::ICONS_DIR) . "/*.ico"), - fn($f) => filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS)); + fn(string $f) => filemtime($f) < time() - 86400 * Config::get(Config::CACHE_MAX_DAYS)); foreach ($icon_files as $icon) { $feed_id = basename($icon, ".ico"); @@ -733,7 +733,7 @@ class RSSUtils { $article_labels = Article::_get_labels($base_entry_id, $feed_obj->owner_uid); $existing_tags = Article::_get_tags($base_entry_id, $feed_obj->owner_uid); - $entry_tags = array_unique(array_merge($entry_tags, $existing_tags)); + $entry_tags = array_unique([...$entry_tags, ...$existing_tags]); } else { $base_entry_id = false; $entry_stored_hash = ""; @@ -865,7 +865,7 @@ class RSSUtils { $pluginhost->run_hooks(PluginHost::HOOK_FILTER_TRIGGERED, $feed, $feed_obj->owner_uid, $article, $matched_filters, $matched_rules, $article_filters); - $matched_filter_ids = array_map(fn($f) => $f['id'], $matched_filters); + $matched_filter_ids = array_map(fn(array $f) => $f['id'], $matched_filters); if (count($matched_filter_ids) > 0) { $filter_objs = ORM::for_table('ttrss_filters2') @@ -1190,8 +1190,7 @@ class RSSUtils { // check for manual tags (we have to do it here since they're loaded from filters) foreach ($article_filters as $f) { if ($f["type"] == "tag") { - $entry_tags = array_merge($entry_tags, - FeedItem_Common::normalize_categories(explode(",", $f["param"]))); + $entry_tags = [...$entry_tags, ...FeedItem_Common::normalize_categories(explode(",", $f["param"]))]; } } @@ -1796,12 +1795,10 @@ class RSSUtils { owner_uid = ? AND enabled = true ORDER BY order_id, title"); $sth->execute([$owner_uid]); - $check_cats = array_merge( - Feeds::_get_parent_cats($cat_id, $owner_uid), - [$cat_id]); + $check_cats = [...Feeds::_get_parent_cats($cat_id, $owner_uid), $cat_id]; $check_cats_str = join(",", $check_cats); - $check_cats_fullids = array_map(fn($a) => "CAT:$a", $check_cats); + $check_cats_fullids = array_map(fn(int $a) => "CAT:$a", $check_cats); while ($line = $sth->fetch()) { $filter_id = $line["id"]; -- cgit v1.2.3 From c301053965004f43408545f4b80cf444ce6a9587 Mon Sep 17 00:00:00 2001 From: wn_ Date: Fri, 12 Aug 2022 18:21:38 +0000 Subject: Use the null coalescing assignment operator in various places. --- classes/feedparser.php | 14 ++++---------- classes/handler/public.php | 3 +-- classes/pluginhost.php | 35 ++++++++++++----------------------- classes/pref/prefs.php | 2 +- classes/rpc.php | 7 +++---- 5 files changed, 21 insertions(+), 40 deletions(-) (limited to 'classes') diff --git a/classes/feedparser.php b/classes/feedparser.php index fc2489e2d..4b9c63f56 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -43,10 +43,8 @@ class FeedParser { foreach (libxml_get_errors() as $error) { if ($error->level == LIBXML_ERR_FATAL) { // currently only the first error is reported - if (!isset($this->error)) { - $this->error = $this->format_error($error); - } - $this->libxml_errors[] = $this->format_error($error); + $this->error ??= Errors::format_libxml_error($error); + $this->libxml_errors[] = Errors::format_libxml_error($error); } } } @@ -87,9 +85,7 @@ class FeedParser { $this->type = $this::FEED_ATOM; break; default: - if (!isset($this->error)) { - $this->error = "Unknown/unsupported feed type"; - } + $this->error ??= "Unknown/unsupported feed type"; return; } } @@ -186,9 +182,7 @@ class FeedParser { if ($this->link) $this->link = trim($this->link); } else { - if (!isset($this->error)) { - $this->error = "Unknown/unsupported feed type"; - } + $this->error ??= "Unknown/unsupported feed type"; return; } } diff --git a/classes/handler/public.php b/classes/handler/public.php index e1486e753..8ddf9bd3d 100755 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -412,8 +412,7 @@ class Handler_Public extends Handler { if (session_status() != PHP_SESSION_ACTIVE) session_start(); - if (!isset($_SESSION["login_error_msg"])) - $_SESSION["login_error_msg"] = __("Incorrect username or password"); + $_SESSION["login_error_msg"] ??= __("Incorrect username or password"); } $return = clean($_REQUEST['return']); diff --git a/classes/pluginhost.php b/classes/pluginhost.php index acc784902..4b85bc216 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -535,10 +535,7 @@ class PluginHost { $method = strtolower($method); if ($this->is_system($sender)) { - if (!isset($this->handlers[$handler])) { - $this->handlers[$handler] = []; - } - + $this->handlers[$handler] ??= []; $this->handlers[$handler][$method] = $sender; } } @@ -641,8 +638,7 @@ class PluginHost { owner_uid= ? AND name = ?"); $sth->execute([$this->owner_uid, $plugin]); - if (!isset($this->storage[$plugin])) - $this->storage[$plugin] = []; + $this->storage[$plugin] ??= []; $content = serialize($this->storage[$plugin]); @@ -673,14 +669,8 @@ class PluginHost { if ($profile_id) { $idx = get_class($sender); - if (!isset($this->storage[$idx])) { - $this->storage[$idx] = []; - } - - if (!isset($this->storage[$idx][$profile_id])) { - $this->storage[$idx][$profile_id] = []; - } - + $this->storage[$idx] ??= []; + $this->storage[$idx][$profile_id] ??= []; $this->storage[$idx][$profile_id][$name] = $value; $this->save_data(get_class($sender)); @@ -695,9 +685,7 @@ class PluginHost { function set(Plugin $sender, string $name, $value): void { $idx = get_class($sender); - if (!isset($this->storage[$idx])) - $this->storage[$idx] = []; - + $this->storage[$idx] ??= []; $this->storage[$idx][$name] = $value; $this->save_data(get_class($sender)); @@ -709,8 +697,7 @@ class PluginHost { function set_array(Plugin $sender, array $params): void { $idx = get_class($sender); - if (!isset($this->storage[$idx])) - $this->storage[$idx] = []; + $this->storage[$idx] ??= []; foreach ($params as $name => $value) $this->storage[$idx][$name] = $value; @@ -848,11 +835,13 @@ class PluginHost { function add_filter_action(Plugin $sender, string $action_name, string $action_desc): void { $sender_class = get_class($sender); - if (!isset($this->plugin_actions[$sender_class])) - $this->plugin_actions[$sender_class] = []; + $this->plugin_actions[$sender_class] ??= []; - array_push($this->plugin_actions[$sender_class], - array("action" => $action_name, "description" => $action_desc, "sender" => $sender)); + $this->plugin_actions[$sender_class][] = [ + "action" => $action_name, + "description" => $action_desc, + "sender" => $sender, + ]; } /** diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index ce9030cdc..3285ce200 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -185,7 +185,7 @@ class Pref_Prefs extends Handler_Protected { $boolean_prefs = explode(",", clean($_POST["boolean_prefs"])); foreach ($boolean_prefs as $pref) { - if (!isset($_POST[$pref])) $_POST[$pref] = 'false'; + $_POST[$pref] ??= 'false'; } $need_reload = false; diff --git a/classes/rpc.php b/classes/rpc.php index bb14225be..ef2cdfc49 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -753,12 +753,11 @@ class RPC extends Handler_Protected { function hotkeyHelp(): void { $info = self::get_hotkeys_info(); $imap = self::get_hotkeys_map(); - $omap = array(); + $omap = []; foreach ($imap[1] as $sequence => $action) { - if (!isset($omap[$action])) $omap[$action] = array(); - - array_push($omap[$action], $sequence); + $omap[$action] ??= []; + $omap[$action][] = $sequence; } ?> -- cgit v1.2.3