Skip to content

refactor: clear PHPStan level 6 findings and declare native types on every function - #970

Merged
TheWitness merged 3 commits into
developfrom
refactor/phpstan-level6-types
Dec 5, 2026
Merged

TheWitness merged 3 commits into
developfrom
refactor/phpstan-level6-types

Conversation

@TheWitness

Copy link
Copy Markdown
Member

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:

    • global declarations for $config and the includes/arrays.php arrays.
    • Variables initialised on every path.
    • PHPDoc corrected to match what callers really pass.
    • Conditions PHPStan proves constant removed, without changing the SQL or output they produce.
  • 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:

    • Cacti core hook dispatch on both 1.2.x and develop, for the callbacks registered in setup.php.
    • Request values, which can be null when the variable isn't set.
    • Nullable columns and db_fetch_cell() results, which get unions or nullable types.

    mixed is used only where values really are heterogeneous: RPN stack values, readings that may be U, '' or null, decoded JSON, and the mailer() result.

  • Renames to remove name collisions:

    • notify_lists.php: hosts(), tholds() and templates() → thold_notify_list_hosts(), thold_notify_list_tholds() and thold_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 different thold_daemon_debug() in thold_process.php.
  • thold_error_handler() now returns true on every path. PHP treated the old null return the same way, so behaviour doesn't change.

  • CI: the PHPStan step now excludes only plugins/thold/tests/*. The step is still gated off while CACTI is 1.2.x.

Differences from #952

Verification

All runs in WSL on PHP 8.3.36.

PHPStan 2.3.7 at level 6, with Cacti develop's .phpstan.neon and the CI sed applied:

phpstan analyze --level 6 plugins/thold

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 -l passes on every PHP file, and git diff --check is clean.

Pest, on Cacti 1.2.x:

Review: an independent pass checked every hook callback against both cores' dispatch, the handling of nullable and false DB values, void/never usage, by-reference callers and the renames, and found no runtime TypeError paths.

Not verified locally: the integration jobs (real install, poller, daemon and upgrade checks). CI on this PR covers them.

…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>
Copilot AI balanced review requested due to automatic review settings December 5, 2026 12:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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
TheWitness force-pushed the refactor/phpstan-level6-types branch from f9410b2 to 881b263 Compare December 5, 2026 12:42
@TheWitness

Copy link
Copy Markdown
Member Author

CI status:

  • Integration tests: the one failing test on every PHP leg is GraphWizardTest "a template, a graph template and a device hand the new graph on to the graph creation form". It also fails on develop itself, and test(wizard): share the wizard stand-ins between the two wizard test files; drop skips for fixed defects #969 fixes it. Every step before Pest passes: install, the daemon queue, worker and graph-alert checks, the log key upgrade check, and php -l. Locally with test(wizard): share the wizard stand-ins between the two wizard test files; drop skips for fixed defects #969 applied the suite gives 2401 passed, 0 failed, and the changed-line coverage gate (tests/bin/patch-coverage.php) gives 100.00% (53/53).
  • SonarCloud duplication on new code: 5.3% (limit 3%). It was 13% before 881b263, which keeps the original $ok = ... && $ok; chains and rewrites only the eight statements PHPStan flags. What remains is 50 of 937 new lines: about 24 are those eight rewrites inside the near-identical action branches of notify_lists.php form_actions(), and the rest are function headers that sit inside already-duplicated blocks in setup.php, evaluate_*.php, thold_notify.php, thold_process.php and polling.php. Getting under 3% would mean deduplicating form_actions(), which is out of scope here.

…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>
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
5.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@TheWitness
TheWitness merged commit 9af0b70 into develop Dec 5, 2026
6 of 7 checks passed
@TheWitness
TheWitness deleted the refactor/phpstan-level6-types branch December 5, 2026 13:56
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants