Make WordPress Core

Changeset 63381


Ignore:
Timestamp:
08/28/2026 12:45:55 AM (7 weeks ago)
Author:
westonruter
Message:

Code Quality: Resolve the encapsedStringPart.nonString error.

The get_attachment_fields_to_edit() function documented every value of its returned array as a field definition array, but the attachment_fields_to_edit filter may also add a _final entry holding raw HTML, which get_media_item() interpolates into a string. Declare that structure in a @phpstan-return tag so the _final entry is typed as a string while the remaining keys stay typed as field definition arrays, and widen the plain @return to a union so the array shape remains a proper subtype of it.

The menu_order key is listed explicitly in the shape because get_media_item() unsets it. Unsetting a key that an unsealed array shape does not declare collapses the shape into a plain union of its value types, which would reintroduce the imprecision for every other field.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13078.
Follow-up to r61594, r63020.

Props dpantazis.
See #65817.

Location:
trunk
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpstan.neon.dist

    r63379 r63381  
    4343        - tests/phpstan/baselines/empty.property.neon
    4444        - tests/phpstan/baselines/empty.variable.neon
    45         - tests/phpstan/baselines/encapsedStringPart.nonString.neon
    4645        - tests/phpstan/baselines/foreach.nonIterable.neon
    4746        - tests/phpstan/baselines/function.alreadyNarrowedType.neon
  • trunk/src/wp-admin/includes/media.php

    r63351 r63381  
    13991399 * @param WP_Post $post
    14001400 * @param array   $errors
    1401  * @return array<string, array<string, mixed>> The attachment fields.
     1401 * @return array<string, array<string, mixed>|string> The attachment fields, keyed by field name. Each value is a
     1402 *                                                    field definition array, except for the `_final` key, which the
     1403 *                                                    `attachment_fields_to_edit` filter may set to raw HTML that is
     1404 *                                                    rendered after all other fields.
     1405 *
     1406 * @phpstan-return array{
     1407 *     _final?: string,
     1408 *     menu_order?: array<string, mixed>, // Listed explicitly so that unsetting it in get_media_item() does not collapse this shape.
     1409 *     ...<string, array<string, mixed>>
     1410 * }
    14021411 */
    14031412function get_attachment_fields_to_edit( $post, $errors = null ) {
Note: See TracChangeset for help on using the changeset viewer.