Repository navigation
refactor: clear PHPStan level 6 findings and declare native types on every function - #970
Merged
Merged
Conversation
…every function - Clear every PHPStan level 6 finding in the plugin's production code (287 on develop): global declarations for $config and the includes/arrays.php arrays, variables initialised on every path, PHPDoc that matches what callers pass, and conditions PHPStan proves constant removed without changing the SQL or output they produce. - Rename functions that collide with Cacti core or another thold script: notify_lists.php hosts(), tholds() and templates() are now thold_notify_list_hosts(), thold_notify_list_tholds() and thold_notify_list_templates() (golden fixtures renamed to match), and thold_daemon.php's thold_daemon_debug() is thold_daemon_parent_debug(). - Declare native parameter and return types on all 392 functions, closures and arrow functions, using PHP 8.1 types only. Types follow every caller, including Cacti core hook dispatch on 1.2.x and develop; parameters that receive request values, nullable columns or db_fetch_cell() results take unions or nullable types. - thold_error_handler() returns true on every path (PHP treats the old null the same way). - CI: the PHPStan step excludes only plugins/thold/tests/* instead of no plugin path at all. Include paths keep the existing chdir() + relative form; __DIR__-based paths would resolve a symlinked plugin directory to the wrong Cacti tree. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-cutting native typing changes need human confirmation of integration results on Cacti 1.2.x and develop because key runtime paths could not be exercised in the review environment.
No open issues 🎉
What changed in this PR
This PR strengthens static analysis across Cacti’s Thresholds plugin by addressing PHPStan level 6 findings and adding PHP 8.1-compatible native function types, separately from the defect fixes already merged.
Changes:
- Adds native types, corrects PHPDoc, declares globals, and initializes variables.
- Removes function-name collisions and aligns golden-test references and fixtures.
- Narrows CI’s PHPStan exclusion to plugin tests and documents the refactor.
| File | Description |
|---|---|
thold.php |
Types administration functions and initializes UI state. |
thold_templates.php |
Types template operations and resolves analysis findings. |
thold_process.php |
Types worker helpers and updates reconnect configuration access. |
thold_notify.php |
Types notification-worker helpers. |
thold_graph.php |
Types graph views and simplifies filter construction. |
thold_daemon.php |
Renames parent logging helper and types daemon functions. |
tests/Unit/FilterFormGoldenTest.php |
References renamed notification-list functions. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-tholds-selected.golden |
Renamed threshold selected-state fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-tholds-edge.golden |
Renamed threshold edge-case fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-tholds-defaults.golden |
Renamed threshold defaults fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-templates-selected.golden |
Renamed template selected-state fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-templates-edge.golden |
Renamed template edge-case fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-templates-defaults.golden |
Renamed template defaults fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-hosts-selected.golden |
Renamed device selected-state fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-hosts-edge.golden |
Renamed device edge-case fixture. |
tests/Fixtures/filter-markup/notify-lists-php-thold-notify-list-hosts-defaults.golden |
Renamed device defaults fixture. |
poller_thold.php |
Types poller entry-point helpers. |
notify_queue.php |
Types queue UI functions and initializes state. |
includes/webapi.php |
Types wizard functions and initializes graph-selection arrays. |
includes/threshold_api.php |
Types threshold operations and initializes save context. |
includes/templates.php |
Types template helpers while preserving nullable returns. |
includes/tab.php |
Declares the tab renderer’s return type. |
includes/settings.php |
Types settings helpers and declares shared globals. |
includes/sampling.php |
Types sampling and daemon-queue helpers. |
includes/queries.php |
Types query and permission helpers. |
includes/polling.php |
Types polling hooks and normalizes mail arguments. |
includes/notify.php |
Types notification, substitution, and delivery helpers. |
includes/notify_queue.php |
Types queue processing and worker-lifecycle functions. |
includes/logging.php |
Types logging helpers and makes handled-error returns explicit. |
includes/expression.php |
Types expression helpers and removes redundant conditions. |
includes/evaluate.php |
Types shared evaluation and notification functions. |
includes/evaluate_time.php |
Types time-based evaluation and simplifies conditions. |
includes/evaluate_hilow.php |
Types high/low evaluation and normalizes mail arguments. |
includes/evaluate_baseline.php |
Types baseline evaluation and normalizes mail arguments. |
includes/display.php |
Types formatting and form helpers. |
includes/database.php |
Types database setup and upgrade functions. |
includes/baseline.php |
Types baseline statistics and reference helpers. |
includes/arrays.php |
Declares configuration access explicitly. |
cli/upgrade.php |
Declares the configuration global. |
cli/upgrade_legacy.php |
Types legacy migration and CLI helpers. |
cli/thresholds.php |
Types CLI threshold-creation helpers. |
cli/import.php |
Types CLI display helpers and declares configuration access. |
CHANGELOG.md |
Documents typing, renames, and analysis changes. |
.github/workflows/plugin-ci-workflow.yml |
Limits PHPStan exclusion to plugin tests. |
Review effort: Balanced
Keep the original `$ok = db_execute_prepared(...) && $ok;` form and use the `if (!db_execute_prepared(...))` form only for the eight statements PHPStan level 6 reports as "Right side of && is always true". Same queries, same result, far fewer changed lines in form_actions(). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
TheWitness
force-pushed
the
refactor/phpstan-level6-types
branch
from
December 5, 2026 12:42
f9410b2 to
881b263
Compare
Member
Author
|
CI status:
|
…files; drop skips for fixed defects #962 and #964 merged together, and each defines file-scope stand-ins for the same Cacti core functions. Whichever file loads first serves both, and they recorded different things, so develop fails "a template, a graph template and a device hand the new graph on to the graph creation form" in CI on every PHP version. - ThresholdWizardAuthorizationTest's html_graph_custom_data(), encode_data_query_index() and form_save_button() stand-ins now match GraphWizardTest's; its two assertions read the device and device template from the first two recorded parameters. - GraphWizardTest gets the is_device_allowed() stand-in the wizard needs since #964, so the file also passes on its own. - Remove the skips #962 added for #954, #956, #957, #958, #959 and #960, which #961, #963, #964 and #965 fixed. All eleven scenarios pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
xmacan
approved these changes
Dec 5, 2026
This was referenced Dec 5, 2026
Closed
TheWitness
pushed a commit
that referenced
this pull request
Dec 5, 2026
…chained hooks (#973) * fix: declare PHP 8.1 as the minimum and accept non-array payloads in chained hooks The native types added in #970 do not parse on PHP 7.4, which Cacti 1.2.29 to 1.2.31 still accept, and setup.php loads on every page. Declare php = 8.1 in INFO and the README, and make plugin_thold_install(), plugin_thold_check_config() and thold_check_dependencies() refuse on an older PHP that can still read the file, logging the reason. Cacti hands each plugin the previous plugin's return value, so a plugin earlier in the chain that returns nothing made the array-typed callbacks throw a TypeError. The navigation, device action and clog regex callbacks now start from an empty array; the data source table, graph options, device save, user save and data source remove callbacks return a non-array unchanged. Fixes #972 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs: limit the PHP 7.4 remark in the README to Cacti 1.2.29 through 1.2.31 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix: accept a non-array payload in the data source and graph action array hooks Core chains data_source_action_array and graphs_action_array with api_plugin_hook_function(), so a plugin earlier in the chain that returns nothing leaves null for these callbacks. Start from an empty array, as the device action callback does. The other registered hooks are either typed nullable already, take no payload, or are called with api_plugin_hook() and a fresh array, which does not chain. Refs #972 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Restarts the PHPStan level 6 work from the closed #952 as a PHPStan-only change on current
develop, and declares native parameter and return types on every function header.PHPStan level 6: 287 production findings on
develop→ 0. The fixes:globaldeclarations for$configand theincludes/arrays.phparrays.Native types on all 392 functions, closures and arrow functions, using PHP 8.1 types only (no standalone
null/true/false, no DNF types). The files stay in coercive mode, so each type was chosen from every caller:setup.php.nullwhen the variable isn't set.db_fetch_cell()results, which get unions or nullable types.mixedis used only where values really are heterogeneous: RPN stack values, readings that may beU,''ornull, decoded JSON, and themailer()result.Renames to remove name collisions:
notify_lists.php:hosts(),tholds()andtemplates()→thold_notify_list_hosts(),thold_notify_list_tholds()andthold_notify_list_templates(). These collided with Cacti core. The golden fixtures are renamed to match; their content is byte-identical.thold_daemon.php:thold_daemon_debug()→thold_daemon_parent_debug(). It collided with the differentthold_daemon_debug()inthold_process.php.thold_error_handler()now returnstrueon every path. PHP treated the oldnullreturn the same way, so behaviour doesn't change.CI: the PHPStan step now excludes only
plugins/thold/tests/*. The step is still gated off whileCACTIis1.2.x.Differences from #952
chdir()+ relative form. Fix #944-#947 defects and PHPStan level 6 findings #952 switched them to__DIR__-based paths, but__DIR__resolves symlinks, so a plugin directory symlinked into Cacti would include the wrong tree. The one exception isthold_db_reconnect()inthold_process.php, which now includes$config['base_path'] . '/include/config.php'. It loads the same file as before.autocreate()keeps returningnull(typed?int), whichThresholdTemplatesTestexpects. Fix #944-#947 defects and PHPStan level 6 findings #952 changed it to return 0.Verification
All runs in WSL on PHP 8.3.36.
PHPStan 2.3.7 at level 6, with Cacti develop's
.phpstan.neonand the CIsedapplied:Result: 0 errors (was 287).
Native types: a PhpParser check over every production file found 392 functions, closures and arrow functions, 0 with a missing parameter or return type, and 0 using PHP 8.2-only types.
Lint and whitespace:
php -lpasses on every PHP file, andgit diff --checkis clean.Pest, on Cacti
1.2.x:GraphWizardTestorder-dependence ondevelopthat test(wizard): share the wizard stand-ins between the two wizard test files; drop skips for fixed defects #969 fixes.Review: an independent pass checked every hook callback against both cores' dispatch, the handling of nullable and
falseDB values,void/neverusage, by-reference callers and the renames, and found no runtimeTypeErrorpaths.Not verified locally: the integration jobs (real install, poller, daemon and upgrade checks). CI on this PR covers them.