From 4ee4ba4e6b8090b1b45c1932eda7737d64408333 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 2 Dec 2026 22:34:52 -0800 Subject: [PATCH 1/2] refactor(php82): use ??=, str_contains, str_starts_with, arrow functions and first-class callables Signed-off-by: Thomas Vincent --- includes/controller.php | 10 ++++------ includes/functions.php | 12 +++++------- includes/render.php | 16 ++++------------ monitor.php | 4 +--- setup.php | 6 +++--- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/includes/controller.php b/includes/controller.php index 345af6e..8de2510 100644 --- a/includes/controller.php +++ b/includes/controller.php @@ -468,7 +468,7 @@ function monitorGetSelectedEvents(): array { $keys = explode(',', (string) $raw); } - $keys = array_map('trim', $keys); + $keys = array_map(trim(...), $keys); return array_values(array_intersect($keys, array_keys($monitor_events))); } @@ -494,11 +494,9 @@ function monitorGetSelectedTemplates(): array { $keys = explode(',', (string) $raw); } - $keys = array_filter(array_map('trim', $keys), static function ($k) { - return ctype_digit((string) $k); - }); + $keys = array_filter(array_map(trim(...), $keys), static fn($k) => ctype_digit((string) $k)); - return array_values(array_map('strval', array_map('intval', $keys))); + return array_values(array_map(strval(...), array_map(intval(...), $keys))); } /** @@ -1637,7 +1635,7 @@ function monitorPanelServiceChecks(array $host, array $config): string { $rows = ''; foreach ($tests as $t) { - $never_run = ($t['check_time'] == '' || substr((string) $t['check_time'], 0, 4) === '0000'); + $never_run = ($t['check_time'] == '' || str_starts_with((string) $t['check_time'], '0000')); if ($t['enabled'] == '') { $cls = 'deviceUnmonitored'; diff --git a/includes/functions.php b/includes/functions.php index 9f47b48..612d1ea 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -286,9 +286,9 @@ function monitorEventFilterSql(array $events): string { return ''; } - $thold_ids = (isset($thold_hosts) && cacti_sizeof($thold_hosts)) ? implode(',', array_map('intval', $thold_hosts)) : '0'; - $servcheck_ids = (isset($servcheck_hosts) && cacti_sizeof($servcheck_hosts)) ? implode(',', array_map('intval', $servcheck_hosts)) : '0'; - $muted = (isset($_SESSION['monitor_muted_hosts']) && cacti_sizeof($_SESSION['monitor_muted_hosts'])) ? implode(',', array_map('intval', $_SESSION['monitor_muted_hosts'])) : '0'; + $thold_ids = (isset($thold_hosts) && cacti_sizeof($thold_hosts)) ? implode(',', array_map(intval(...), $thold_hosts)) : '0'; + $servcheck_ids = (isset($servcheck_hosts) && cacti_sizeof($servcheck_hosts)) ? implode(',', array_map(intval(...), $servcheck_hosts)) : '0'; + $muted = (isset($_SESSION['monitor_muted_hosts']) && cacti_sizeof($_SESSION['monitor_muted_hosts'])) ? implode(',', array_map(intval(...), $_SESSION['monitor_muted_hosts'])) : '0'; $map = [ 'down' => "(h.status = 1 AND h.id NOT IN ($muted))", @@ -387,9 +387,7 @@ function renderWhereJoin(string &$sql_where, string &$sql_join): void { $tpls = explode(',', (string) $tpl_raw); } - $tpls = array_map('intval', array_filter(array_map('trim', $tpls), static function ($t) { - return ctype_digit((string) $t); - })); + $tpls = array_map(intval(...), array_filter(array_map(trim(...), $tpls), static fn($t) => ctype_digit((string) $t))); if (cacti_sizeof($tpls)) { $awhere .= ($awhere == '' ? '' : ' AND ') . 'h.host_template_id IN (' . implode(',', $tpls) . ')'; @@ -404,7 +402,7 @@ function renderWhereJoin(string &$sql_where, string &$sql_join): void { } elseif ($events_raw === '' || $events_raw === null) { $events = []; } else { - $events = array_map('trim', explode(',', (string) $events_raw)); + $events = array_map(trim(...), explode(',', (string) $events_raw)); } $event_sql = monitorEventFilterSql($events); diff --git a/includes/render.php b/includes/render.php index 1e13c57..eb2ca8b 100644 --- a/includes/render.php +++ b/includes/render.php @@ -343,9 +343,7 @@ function renderSite(): string { foreach ($hosts as $host) { $gid = (string) $host['site_id']; - if (!isset($groups[$gid])) { - $groups[$gid] = ['label' => $host['site_name'], 'hosts' => []]; - } + $groups[$gid] ??= ['label' => $host['site_name'], 'hosts' => []]; $groups[$gid]['hosts'][] = $host; } @@ -450,9 +448,7 @@ function renderTemplate(): string { $gid = (string) $host['host_template_id']; $label = $host['host_template_name'] != '' ? $host['host_template_name'] : __('Non-Templated Devices', 'monitor'); - if (!isset($groups[$gid])) { - $groups[$gid] = ['label' => $label, 'hosts' => []]; - } + $groups[$gid] ??= ['label' => $label, 'hosts' => []]; $groups[$gid]['hosts'][] = $host; } @@ -735,9 +731,7 @@ function monitorRenderTreeCards(array $tree_list): string { foreach ($hosts as $host) { $gid = 'tree_' . $host['graph_tree_id']; - if (!isset($groups[$gid])) { - $groups[$gid] = ['label' => $host['graph_tree_name'], 'hosts' => []]; - } + $groups[$gid] ??= ['label' => $host['graph_tree_name'], 'hosts' => []]; $groups[$gid]['hosts'][] = $host; } @@ -778,9 +772,7 @@ function monitorRenderTreeCards(array $tree_list): string { // Default-order the tree cards by device count (busiest first); any saved // drag order the user set still wins in monitorRenderGroupCards(). - uasort($groups, static function (array $a, array $b): int { - return cacti_sizeof($b['hosts']) <=> cacti_sizeof($a['hosts']); - }); + uasort($groups, static fn(array $a, array $b): int => cacti_sizeof($b['hosts']) <=> cacti_sizeof($a['hosts'])); return monitorRenderGroupCards('tree', $groups, $maxlen); } diff --git a/monitor.php b/monitor.php index 7c2c3e5..c309120 100644 --- a/monitor.php +++ b/monitor.php @@ -146,9 +146,7 @@ $_SESSION['names'] = 0; -if (!isset($_SESSION['monitor_muted_hosts'])) { - $_SESSION['monitor_muted_hosts'] = []; -} +$_SESSION['monitor_muted_hosts'] ??= []; include_once __DIR__ . '/includes/functions.php'; include_once __DIR__ . '/includes/render.php'; diff --git a/setup.php b/setup.php index 9a90123..170f4dc 100644 --- a/setup.php +++ b/setup.php @@ -328,7 +328,7 @@ function monitor_check_upgrade() { $info = plugin_monitor_version(); $current = $info['version']; - $old = db_fetch_cell_prepared('SELECT version FROM plugin_config WHERE directory = ?', array('monitor')); + $old = db_fetch_cell_prepared('SELECT version FROM plugin_config WHERE directory = ?', ['monitor']); if ($current != $old) { monitor_setup_table(); @@ -484,7 +484,7 @@ function monitor_device_action_execute($action) { * @return array The unmodified $devices array. */ function monitor_device_remove($devices) { - $ids = array_map('intval', array_values($devices)); + $ids = array_map(intval(...), array_values($devices)); if (cacti_sizeof($ids)) { $in = implode(',', array_fill(0, count($ids), '?')); @@ -1460,7 +1460,7 @@ function monitor_prune_files(): void { $whitelist = isset($manifest['whitelist']) && is_array($manifest['whitelist']) ? $manifest['whitelist'] : []; $protected = function (string $rel) use ($whitelist): bool { - if (strncmp($rel, '.git', 4) === 0 || strncmp($rel, '.md', 3) === 0) { + if (str_starts_with($rel, '.git') || str_starts_with($rel, '.md')) { return true; } From 0525b9a880150984e1f32bc850b8abf53061c5a7 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Thu, 3 Dec 2026 02:29:30 -0800 Subject: [PATCH 2/2] test(monitor): cover status and device template filter normalization Signed-off-by: Thomas Vincent --- tests/Unit/MonitorRequestActionTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Unit/MonitorRequestActionTest.php b/tests/Unit/MonitorRequestActionTest.php index c1be123..4329c3e 100644 --- a/tests/Unit/MonitorRequestActionTest.php +++ b/tests/Unit/MonitorRequestActionTest.php @@ -62,3 +62,23 @@ expect($contents)->toContain('switch (monitorRequestAction()) {') ->and($contents)->not->toContain("switch (get_nfilter_request_var('action'))"); }); + +it('keeps registered status filters in request order', function () { + $previous = $GLOBALS['monitor_events'] ?? null; + $GLOBALS['monitor_events'] = ['down' => 'Down', 'up' => 'Up']; + try { + test_set_request(['events' => ' up , unknown,down ']); + expect(monitorGetSelectedEvents())->toBe(['up', 'down']); + } finally { + $GLOBALS['monitor_events'] = $previous; + } +}); + +it('normalizes numeric device template filters', function ($raw, array $expected) { + test_set_request(['template' => $raw]); + expect(monitorGetSelectedTemplates())->toBe($expected); +})->with([ + 'comma separated' => [' 03,none, 5, -1', ['3', '5']], + 'multiple selection' => [[' 1', '2 ', ''], ['1', '2']], + 'all templates' => ['', []], +]);