Skip to content

Bump PHPStan to level 2 and fix all resulting issues#1196

Open
utkarshcloudinary wants to merge 2 commits into
phpstan-level1from
phpstan-level2
Open

Bump PHPStan to level 2 and fix all resulting issues#1196
utkarshcloudinary wants to merge 2 commits into
phpstan-level1from
phpstan-level2

Conversation

@utkarshcloudinary

@utkarshcloudinary utkarshcloudinary commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Approach

Raise the PHPStan analysis level from 1 to 2 and resolve all 88 resulting errors. Work is split into two commits:

1. Bump PHPStan to level 2 and fix type/annotation issues (81 issues, safe/mechanical)

  • Correct malformed PHPDoc tags (trailing periods, invalid array()/[]/\array syntax, @var $x Type ordering).
  • Use fully-qualified class names in PHPDoc where the Cloudinary namespace shadowed WP/global classes (WP_REST_*, Cloudinary\Connect).
  • Add an Elementor stub for the optional integration and register it in phpstan.neon.dist.
  • Narrow get_component() / managers[...] union return types at call sites so type-specific method/property access resolves.
  • Add @property/@method annotations for magic __get/__call access (Settings, Setting, Relationship, Api) — the szepeviktor/phpstan-wordpress extension enforces these.
  • Fix genuine argument-count/type bugs: save_value($media), drop ignored extra args to has_param()/get_settings(), cast numeric stats before arithmetic, correct WP_Post case, make Relationship::$post_id public.
  • Suppress one intentional dynamic WP_Post property write inline.

2. Fix broken references to removed settings API (7 issues, genuine pre-existing runtime bugs)

These referenced methods deleted in the earlier settings/UI decouple refactor (3c5cd8a, 2021-08-27) and would fatal if executed:

  • class-cache.php: the Site Cache tabs called the removed Settings::create_setting() via a callback indirection. Converted add_plugin/theme/wp/content_settings() to return their params and embedded them inline in settings() (matching the working Assets pattern); the cache_point path hooks are now registered directly in setup_setting_tabs(). Removed the now-unused get_cache_settings().
  • class-cache-status.php: box_status() built a table via the removed create_setting()/render_component(); rebuilt using the component part system so it renders correctly.
  • class-page.php + class-admin.php: notice() called the non-existent Setting::get_admin_notices(); routed through the Admin component and had get_admin_notices() return the renderable notice Setting built via init_components() (consistent with render_notices()).

QA notes

  • Run static analysis and confirm no errors at level 2:
    composer phpstan
    
    Expected: [OK] No errors.
  • Run code style on the changed files (repo-wide composer lint may OOM; scope to changed files with a raised limit):
    vendor/bin/phpcs -d memory_limit=1G php/class-cache.php php/class-admin.php php/ui/component/class-cache-status.php php/ui/component/class-page.php
    
    Expected: no errors/warnings.
  • Functional smoke test of the touched runtime paths:
    • Site Cache settings page (Cache component) renders without fatals; plugin/theme/WordPress/content cache tabs display and their toggles work.
    • Cache status table renders the list of cache points with cached-item counts.
    • Settings page notices: trigger a settings error/notice and confirm it renders inline on the Cloudinary settings page.
    • Analytics events (connection test, wizard submit, first sync) still fire when the Analytics component is active.
    • WPML language-link asset switching still resolves the relationship post_id.

Raise the PHPStan analysis level from 1 to 2 and resolve the resulting
issues that are safe, mechanical fixes:

- Correct malformed PHPDoc tags (trailing periods, invalid array()/[]
  syntax, \array, $var ordering).
- Use fully-qualified class names in PHPDoc where the containing
  namespace shadowed WP/global classes (WP_REST_*, Cloudinary\Connect).
- Add an Elementor stub for the optional integration and register it.
- Replace the removed Requests_Transport_cURL type hint with resource.
- Narrow get_component()/managers[...] union return types at call sites
  so type-specific method/property access resolves.
- Add \@property/@method annotations for magic __get/__call access
  (Settings, Setting, Relationship, Api).
- Fix genuine argument-count/type bugs: save_value($media), drop ignored
  extra args to has_param()/get_settings(), cast numeric stats before
  arithmetic, correct WP_Post case, make Relationship::$post_id public.
- Suppress one intentional dynamic WP_Post property write inline.
Repair genuine runtime bugs surfaced by PHPStan level 2 that referenced
methods deleted in the earlier settings/UI decoupling refactor:

- class-cache.php: the cache settings tabs called the removed
  Settings::create_setting() via a callback indirection that would fatal.
  Convert add_plugin/theme/wp/content_settings() to return their params
  and embed them inline in settings() (matching the working Assets
  pattern), and register the cache_point path hooks directly in
  setup_setting_tabs(). Remove the now-unused get_cache_settings().
- class-cache-status.php: box_status() built a table via the removed
  create_setting()/render_component(); rebuild it using the component
  part system so it renders correctly.
- class-page.php + class-admin.php: notice() called the non-existent
  Setting::get_admin_notices(); route it through the Admin component and
  have get_admin_notices() return the renderable notice Setting built via
  init_components() (consistent with render_notices()).

Also convert the inline union-narrowing @var hints added for the level
bump to multi-line docblocks to satisfy WPCS.
@utkarshcloudinary utkarshcloudinary marked this pull request as ready for review July 8, 2026 11:25
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.

2 participants