Skip to content

Code Quality: Resolve the offsetAccess.nonOffsetAccessible PHPStan errors. - #16240

Closed
mukeshpanchal27 wants to merge 3 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/offset-access-nonoffsetaccessible-baseline
Closed

mukeshpanchal27 wants to merge 3 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/offset-access-nonoffsetaccessible-baseline

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

✅ Committed in r65791 (5601a21).


Trac ticket: https://core.trac.wordpress.org/ticket/66739

Description

This PR resolves all 5 offsetAccess.nonOffsetAccessible PHPStan errors and deletes tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon.

All five errors have one cause. WP_Theme::$update was documented as @var bool, but get_theme_updates() stores the theme's update data array in it:

$update_themes[ $stylesheet ]->update = $data;

Callers then read ->update['new_version'] from that array. PHPStan trusted the bool type and reported each read as Cannot access offset 'new_version' on bool:

  • 4 in src/wp-admin/includes/class-wp-debug-data.php
  • 1 in src/wp-admin/update-core.php

The property can hold three values:

Value Set by
false Default
true WP_MS_Themes_List_Table::prepare_items(), when an update is available
The theme's update data get_theme_updates(), from the response of the update_themes site transient

The property is now documented as bool|array<string, mixed>, and the docblock describes all three values. That entry always contains new_version, and usually theme, url, package, requires, and requires_php as well. Themes updated via the update_themes_{$hostname} filter may include other keys.

A @phpstan-var gives the array its shape:

@phpstan-var bool|array{
    new_version: string,
    theme?: string,
    url?: string,
    package?: string,
    requires?: string|false,
    requires_php?: string|false,
    ...
}

The shape is open (...) because filters and the API can add keys. Since new_version is now known to be a string rather than mixed, it also resolves 7 level-10 argument.type errors where it is passed to printf() and sprintf() in update-core.php and class-wp-debug-data.php.

No runtime code changes.

get_theme_updates() does not validate the contents of the update_themes transient, which leaves 7 level-10 errors in that function. That is out of scope here; the findings and a possible follow-up are in #16240 (comment).

Testing

  • composer phpstan:baselines -- --identifier=offsetAccess.nonOffsetAccessible --config=phpstan.neon.dist reports that no errors remain and deletes the file.
  • composer phpstan:default and composer phpstan:strict both report no errors.
  • At level 10, the argument.type errors on the new_version reads in update-core.php and class-wp-debug-data.php are gone.
  • PHPCS reports no errors in class-wp-theme.php. The one warning, on line 401, is on an unchanged line and also appears on trunk.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Finding the cause of the errors, editing the docblock, running PHPStan and PHPCS, and drafting this PR. I reviewed the changes.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

`get_theme_updates()` stores the theme's update data array in `WP_Theme::$update`, but the property was documented as `bool`. PHPStan therefore reported every read of `->update['new_version']` as `offsetAccess.nonOffsetAccessible`.

Document the property as `bool|array<string, mixed>`, which covers both the `true` set by `WP_MS_Themes_List_Table` and the array set by `get_theme_updates()`. This resolves all five errors and removes the `offsetAccess.nonOffsetAccessible` baseline.

See #66739.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mukesh27, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the documented type correction and related PHPStan baseline removal.

No open issues 🎉

What changed in this PR

This PR corrects the documented type of WP_Theme::$update to reflect its existing usage, resolving five PHPStan offset-access errors without changing runtime behavior.

Changes:

  • Documents $update as bool|array<string, mixed> and explains that get_theme_updates() supplies update data.
  • Deletes the PHPStan baseline for the five resolved errors.
File Description
tests/​phpstan/​baselines/​offsetAccess.nonOffsetAccessible.neon Removes the baseline suppressing the resolved errors.
src/​wp-includes/​class-wp-theme.php Corrects the property’s documented type and adds a function reference.

Review effort: Balanced

westonruter and others added 2 commits November 25, 2026 11:13
Describe the three states the property can hold: false by default, true
when WP_MS_Themes_List_Table marks the theme as updatable, and the theme's
entry from the update_themes site transient when set by
get_theme_updates().

Add a `@phpstan-var` array shape for that entry, so that `new_version` is
known to be a string rather than mixed. This resolves the level-10
`argument.type` errors where it is passed to printf() and sprintf() in
update-core.php and WP_Debug_Data.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: 8b5b7697-1a6d-4766-9824-3c2bf5b102d6
@westonruter

Copy link
Copy Markdown
Member

🤖 Comment by Claude Opus 5.5 (medium effort)

get_theme_updates() assigns whatever is in the update_themes site transient's response straight to WP_Theme::$update without validating it, so it has 7 PHPStan errors at level 10. Core always writes an array of arrays keyed by stylesheet there, but the transient is filterable and its contents aren't guaranteed. Fixing it seems out of scope for this PR, so it could be done as a follow-up.

Details

How update_themes is populated

Only wp_update_themes() writes the transient in core. It is deleted in src/wp-admin/includes/theme.php:135 and src/wp-includes/theme.php:144. There are two writes:

  1. src/wp-includes/update.php:712: before the API request, the previous value (or a new stdClass if it was not an object) is saved with last_checked updated, to prevent repeated blocking requests if the request hangs.
  2. src/wp-includes/update.php:875: after the request, a new stdClass is saved with:
    • last_checked (int) and checked (stylesheet ⇒ installed version).
    • When the decoded body is an array: response = $response['themes'], no_update = $response['no_update'], and translations = $response['translations']. These keys are not checked for existence or type.
    • For each theme with an Update URI header, the update_themes_{$hostname} filter result is cast to an array and stored in response[ $stylesheet ] or no_update[ $stylesheet ]. Results without version are skipped, and new_version defaults to version.

Because the body is decoded with json_decode( …, true ), each response entry is an array keyed by stylesheet on the core path. However:

  • pre_set_site_transient_update_themes and site_transient_update_themes are widely used by third-party theme updaters, and whatever they inject reaches WP_Theme::$update unchecked.
  • If the API body decodes to an array lacking themes, response becomes null and a PHP warning is raised.
  • get_site_transient() returns false when the transient is missing, and in principle any value from an object cache.

Level-10 errors in get_theme_updates()

Line Identifier Cause
633 property.nonObject $current is mixed
639 foreach.nonIterable response is mixed
640 argument.type, offsetAccess.invalidOffset key is mixed
641 offsetAccess.invalidOffset key is mixed
641 assign.propertyType $data is mixed
644 return.type array<WP_Theme> vs. array<string, WP_Theme>

The return.type error reflects a real inaccuracy: a theme directory with a numeric name (e.g. 2024) becomes an int key in PHP, so the documented array<string, WP_Theme> is already wrong at runtime.

Possible follow-up

  • Add a @phpstan-type alias for the transient (e.g. Theme_Update_Transient, as object{ last_checked: int, checked?: array<string, string>, response?: array<array-key, Theme_Update_Data>, … }), reused for WP_Theme::$update and by the 17 places in core that call get_site_transient( 'update_themes' ), several of which read ->response with the same unchecked pattern.
  • In get_theme_updates(), require is_object( $current ) && isset( $current->response ) && is_array( $current->response ), skip entries unless is_array( $data ) && isset( $data['new_version'] ), and cast the key with (string) $stylesheet. Silently skipping bad entries matches how wp_update_themes() already treats a non-object transient.
  • In wp_update_themes(), check isset( $response['themes'] ) && is_array( $response['themes'] ) (and likewise for no_update and translations) before assigning.
  • Correct the return type to WP_Theme[] / array<array-key, WP_Theme>, or keep string keys and note that numeric directory names become integers.

wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
The property was documented as a boolean when it was declared on the class in r33957, which only accounted for `WP_MS_Themes_List_Table` setting it to true. Since r12066, however, `get_theme_updates()` has stored the theme's entry from the `update_themes` site transient in it, and callers read `new_version` from that array, which PHPStan reported as an offset access on a boolean. The docblock now describes the default, the flag, and the update data, and its type includes the array. This empties the `offsetAccess.nonOffsetAccessible` baseline, so it is removed.

For static analysis the update data is given an open array shape in which `new_version` is always a string, so the values passed to `printf()` and `sprintf()` are no longer mixed at level 10. The contents of the transient are still not validated by `get_theme_updates()`, which is left for a follow-up.

Developed in #16240.
Follow-up to r12066, r33957, r63022.

Props mukesh27, westonruter.
See #66739.


git-svn-id: https://develop.svn.wordpress.org/trunk@65791 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 25, 2026
The property was documented as a boolean when it was declared on the class in r33957, which only accounted for `WP_MS_Themes_List_Table` setting it to true. Since r12066, however, `get_theme_updates()` has stored the theme's entry from the `update_themes` site transient in it, and callers read `new_version` from that array, which PHPStan reported as an offset access on a boolean. The docblock now describes the default, the flag, and the update data, and its type includes the array. This empties the `offsetAccess.nonOffsetAccessible` baseline, so it is removed.

For static analysis the update data is given an open array shape in which `new_version` is always a string, so the values passed to `printf()` and `sprintf()` are no longer mixed at level 10. The contents of the transient are still not validated by `get_theme_updates()`, which is left for a follow-up.

Developed in WordPress/wordpress-develop#16240.
Follow-up to r12066, r33957, r63022.

Props mukesh27, westonruter.
See #66739.

Built from https://develop.svn.wordpress.org/trunk@65791


git-svn-id: http://core.svn.wordpress.org/trunk@64820 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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