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
This commit is contained in:
wn_ 2022-08-12 15:31:19 +00:00
parent a63c949a55
commit 3487c922b3
17 changed files with 71 additions and 72 deletions

View File

@ -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();

View File

@ -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]);
}
/**

View File

@ -5,13 +5,13 @@ class Counters {
* @return array<int, array<string, int|string>>
*/
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<int, array<string, int|string>>
*/
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,

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);

View File

@ -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));

View File

@ -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);

View File

@ -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']]);
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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 {

View File

@ -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"];

View File

@ -19,7 +19,10 @@ class Af_Comics extends Plugin {
require_once __DIR__ . "/filter_base.php";
$filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
$filters = [
...(glob(__DIR__ . "/filters.local/*.php") ?: []),
...(glob(__DIR__ . "/filters/*.php") ?: []),
];
$names = [];
foreach ($filters as $file) {

View File

@ -376,11 +376,11 @@ class Af_RedditImgur extends Plugin {
}
if ($post_is_nsfw && count($apply_nsfw_tags) > 0) {
$article["tags"] = array_merge($article["tags"], $apply_nsfw_tags);
array_push($article["tags"], ...$apply_nsfw_tags);
}
if (count($link_flairs) > 0) {
$article["tags"] = array_merge($article["tags"], FeedItem_Common::normalize_categories($link_flairs));
array_push($article["tags"], ...FeedItem_Common::normalize_categories($link_flairs));
}
$article["num_comments"] = $num_comments;
@ -937,7 +937,7 @@ class Af_RedditImgur extends Plugin {
$src_domain = parse_url($src, PHP_URL_HOST);
if ($src_domain)
foreach (array_merge($this->domain_blacklist, $also_blacklist) as $domain) {
foreach ([...$this->domain_blacklist, ...$also_blacklist] as $domain) {
if (strstr($src_domain, $domain) !== false) {
return true;
}

View File

@ -84,10 +84,10 @@ class Cache_Starred_Images extends Plugin {
Debug::log("expiring {$this->cache->get_dir()} and {$this->cache_status->get_dir()}...");
$files = array_merge(
glob($this->cache->get_dir() . "/*-*"),
glob($this->cache_status->get_dir() . "/*.status")
);
$files = [
...(glob($this->cache->get_dir() . "/*-*") ?: []),
...(glob($this->cache_status->get_dir() . "/*.status") ?: []),
];
asort($files);