Skip to content

Implement short inline syntax for array parameters and return values #15

Closed
@johnbillion

Description

@johnbillion

Neither PHPStan nor Psalm support the hash-style notation used by WordPress for its array parameters and array return values.

@param array $args {
    Array of arguments used to display the help tab.

    @type string   $title    Title for the tab. Default false.
    @type string   $id       Tab ID. Must be HTML-safe and should be unique for this menu.
                             It is NOT allowed to contain any empty spaces. Default false.
    @type string   $content  Optional. Help tab content in plain text or HTML. Default empty string.
    @type callable $callback Optional. A callback to generate the tab content. Default false.
    @type int      $priority Optional. The priority of the tab, used for ordering. Default 10.
}

This means there's currently no way to inform static analysis tools of the types of the array elements passed to or returned by functions such as wp_remote_post() and classes such as WP_Query.

It would be great to:

  1. Post-process the stubs after they're generated and convert hash-style array notation in the docblocks into short inline syntax. Example:
    - * @param array $args {
    - *     Array of arguments used to display the help tab.
    - *
    - *     @type string   $title    Title for the tab. Default false.
    - *     @type string   $id       Tab ID. Must be HTML-safe and should be unique for this menu.
    - *                              It is NOT allowed to contain any empty spaces. Default false.
    - *     @type string   $content  Optional. Help tab content in plain text or HTML. Default empty string.
    - *     @type callable $callback Optional. A callback to generate the tab content. Default false.
    - *     @type int      $priority Optional. The priority of the tab, used for ordering. Default 10.
    - * }
    + * @param array{
    + *     title?: string,
    + *     id?: string,
    + *     content?: string,
    + *     callback?: callable,
    + *     priority?: int,
    + * } $args
  2. Create a mapping from function parameters such as wp_remote_post() $args to their canonical function parameter such as WP_Http::request() $args and then apply the same docblock from the canonical function in its wrapper functions. This allows for parameter type checking in wrapper functions which are more commonly used. This likely needs to happen as a separate second step as it will be a manual process to create the mapping. WordPress core isn't well documented or consistently documented in this regard.

Approach

It's possible that johnbillion/wp-parser-lib could be used to aid the transformation as it's able to tokenise the hash style array notation.

Considerations

Unfortunately the short inline syntax supported by PHPStan and Psalm don't allow for human-readable documentation of the array elements. This means the stubs will be better for PHPStan and Psalm but worse for humans. Maybe two sets of stubs needs to be created, one with the human-readable docblock from core and one with the short inline syntax for use in static analysis.

Refs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions