Skip to content

Stub sealed array excluding valid cases - #485

Merged
szepeviktor merged 2 commits into
php-stubs:masterfrom
PeterSmith8nss:master
Sep 1, 2026
Merged

szepeviktor merged 2 commits into
php-stubs:masterfrom
PeterSmith8nss:master

Conversation

@PeterSmith8nss

Copy link
Copy Markdown
Contributor

Array shapes generated for standard stubs are treated as sealed arrays by PHPStan 2,2. In some cases, the stub generator does not allow for all the elements that are allowed in those arrays, e.g. they are incorrectly sealed.

Two examples:

  • get_categories - standard description only includes the first of the possible elements of $args, so PHPStan will flag an error if any of the other elements are provided
  • add_settings_field - callback args may include other elements in addition to the two listed (see tradesouthwest example), these being of the designer's choice

I've modified the function map as follows:

  • get_categories - added the argument parameter and set this as an unsealed array (as full list of elements would have been very long)
  • add_settings_field - added to the function map, with $args as an unsealed array which it needs to be.

Fix two WordPress function where a shortened list of array arguments gives the wrong sealed array

This comment was marked as low quality.

Correct errors in previous submission
@szepeviktor

Copy link
Copy Markdown
Member

@PeterSmith8nss I am so sorry. There is no label_for!

@PeterSmith8nss

Copy link
Copy Markdown
Contributor Author

Latest commit has add_settings_field $args defined as 'args' => 'array{label_for?:string,class?:string,...}' which matches the definition:
$args
arrayoptional
Extra arguments that get passed to the callback function.

    label_for string
    When supplied, the setting title will be wrapped in a <label> element, its for attribute populated with this value.
    class string
    CSS Class to be added to the <tr> element when the field is output.

Default:array()

And include "label_for"

@szepeviktor

Copy link
Copy Markdown
Member

@szepeviktor
szepeviktor merged commit 57fa7f0 into php-stubs:master Sep 1, 2026
8 checks passed
Comment thread functionMap.php
'get_calendar' => ['($args is array{display: false, ...} ? string : void)'],
'get_category_parents' => [null, 'deprecated' => 'array{}'],
'get_categories' => ["(\$args is array{fields: 'count', ...} ? list<numeric-string> : (\$args is array{fields: 'names'|'slugs', ...} ? list<string> : (\$args is array{fields: 'id=>name'|'id=>slug', ...} ? array<int, string> : (\$args is array{fields: 'id=>parent', ...} ? array<int, int> : (\$args is array{fields: 'ids'|'tt_ids', ...} ? list<int> : array<int, \WP_Term>)))))", 'args' => 'array{taxonomy?:string,...}'],
'get_categories' => ["(\$args is array{fields: 'count', ...} ? list<numeric-string> : (\$args is array{fields: 'names'|'slugs', ...} ? list<string> : (\$args is array{fields: 'id=>name'|'id=>slug', ...} ? array<int, string> : (\$args is array{fields: 'id=>parent', ...} ? array<int, int> : (\$args is array{fields: 'ids'|'tt_ids', ...} ? list<int> : array<int, \WP_Term>)))))", 'args' => 'string|array{taxonomy?:string,...}'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

php-stubs/wordpress-stubs typically does not allow query strings.

// It's common for an args parameter to accept a query var string or array with `string|array`.
// Remove the accepted string type for these so we get the strongest typing we can manage.
$tagVariableType = str_replace(['|string', 'string|'], '', $tagVariableType);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you.
Do you thing about 8407351 ?

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.

4 participants