Repository navigation
Code Quality: Resolve the offsetAccess.nonOffsetAccessible PHPStan errors. - #16240
mukeshpanchal27 wants to merge 3 commits into
Conversation
`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>
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
🟢 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
$updateasbool|array<string, mixed>and explains thatget_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
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
|
🤖 Comment by Claude Opus 5.5 (medium effort)
DetailsHow
|
| 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-typealias for the transient (e.g.Theme_Update_Transient, asobject{ last_checked: int, checked?: array<string, string>, response?: array<array-key, Theme_Update_Data>, … }), reused forWP_Theme::$updateand by the 17 places in core that callget_site_transient( 'update_themes' ), several of which read->responsewith the same unchecked pattern. - In
get_theme_updates(), requireis_object( $current ) && isset( $current->response ) && is_array( $current->response ), skip entries unlessis_array( $data ) && isset( $data['new_version'] ), and cast the key with(string) $stylesheet. Silently skipping bad entries matches howwp_update_themes()already treats a non-object transient. - In
wp_update_themes(), checkisset( $response['themes'] ) && is_array( $response['themes'] )(and likewise forno_updateandtranslations) 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.
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
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
✅ Committed in r65791 (5601a21).
Trac ticket: https://core.trac.wordpress.org/ticket/66739
Description
This PR resolves all 5
offsetAccess.nonOffsetAccessiblePHPStan errors and deletestests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon.All five errors have one cause.
WP_Theme::$updatewas documented as@var bool, butget_theme_updates()stores the theme's update data array in it:Callers then read
->update['new_version']from that array. PHPStan trusted thebooltype and reported each read asCannot access offset 'new_version' on bool:src/wp-admin/includes/class-wp-debug-data.phpsrc/wp-admin/update-core.phpThe property can hold three values:
falsetrueWP_MS_Themes_List_Table::prepare_items(), when an update is availableget_theme_updates(), from theresponseof theupdate_themessite transientThe property is now documented as
bool|array<string, mixed>, and the docblock describes all three values. That entry always containsnew_version, and usuallytheme,url,package,requires, andrequires_phpas well. Themes updated via theupdate_themes_{$hostname}filter may include other keys.A
@phpstan-vargives 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. Sincenew_versionis now known to be astringrather thanmixed, it also resolves 7 level-10argument.typeerrors where it is passed toprintf()andsprintf()inupdate-core.phpandclass-wp-debug-data.php.No runtime code changes.
get_theme_updates()does not validate the contents of theupdate_themestransient, 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.distreports that no errors remain and deletes the file.composer phpstan:defaultandcomposer phpstan:strictboth report no errors.argument.typeerrors on thenew_versionreads inupdate-core.phpandclass-wp-debug-data.phpare gone.class-wp-theme.php. The one warning, on line 401, is on an unchanged line and also appears ontrunk.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.