Skip to content

Commit 31b09c2

Browse files
committed
Code Quality: Add PHPStan array shapes.
Document the known keys of arrays that functions accept or return as array shapes, such as the HTTP API response, which is defined once on `WP_Http` and reused by the remote request functions, and the widget, robots, and admin helper arrays. This lets static analysis check how the values are used. Developed in: #13894 Props swissspidy, westonruter, marian1, johnbillion. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65777 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 00a656a commit 31b09c2

16 files changed

Lines changed: 180 additions & 19 deletions

‎src/wp-admin/includes/media.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,8 @@ function image_align_input_fields( $post, $checked = '' ) {
12371237
* @param WP_Post $post
12381238
* @param bool|string $check
12391239
* @return array<string, string> An array of data for the image size input fields.
1240+
*
1241+
* @phpstan-return array{label: string, input: 'html', html: string}
12401242
*/
12411243
function image_size_input_fields( $post, $check = '' ) {
12421244
/**
@@ -1937,6 +1939,8 @@ function get_media_item( $attachment_id, $args = null ) {
19371939
* @param int $attachment_id
19381940
* @param array $args
19391941
* @return array<string, string> An array containing the media item and its metadata.
1942+
*
1943+
* @phpstan-return array{item: string, meta: string}
19401944
*/
19411945
function get_compat_media_markup( $attachment_id, $args = null ) {
19421946
$post = get_post( $attachment_id );

‎src/wp-admin/includes/ms.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,8 @@ function network_edit_site_nav( $args = array() ) {
11871187
* @since 4.9.0
11881188
*
11891189
* @return array Help tab arguments.
1190+
*
1191+
* @phpstan-return array{id: non-falsy-string, title: string, content: non-falsy-string}
11901192
*/
11911193
function get_site_screen_help_tab_args() {
11921194
return array(

‎src/wp-admin/includes/nav-menu.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,16 @@ function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
13481348
* @since 3.0.0
13491349
*
13501350
* @return string[] Array of column titles keyed by their column name.
1351+
*
1352+
* @phpstan-return array{
1353+
* _title: string,
1354+
* cb: non-falsy-string,
1355+
* link-target: string,
1356+
* title-attribute: string,
1357+
* css-classes: string,
1358+
* xfn: string,
1359+
* description: string,
1360+
* }
13511361
*/
13521362
function wp_nav_menu_manage_columns() {
13531363
return array(

‎src/wp-admin/includes/post.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,8 @@ function _fix_attachment_links( $post ) {
12201220
*
12211221
* @param string $type The post_type you want the statuses for. Default 'post'.
12221222
* @return string[] An array of all the statuses for the supplied post type.
1223+
*
1224+
* @phpstan-return list<string>
12231225
*/
12241226
function get_available_post_statuses( $type = 'post' ) {
12251227
$statuses = wp_count_posts( $type );

‎src/wp-includes/class-wp-dependencies.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ class WP_Dependencies {
7373
*
7474
* @since 2.8.0
7575
*
76-
* @var (int|false)[]
76+
* @var array<string, int|false>
77+
*
78+
* @phpstan-var array<non-empty-string, int|false>
7779
*/
7880
public $groups = array();
7981

‎src/wp-includes/class-wp-http.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
* Debugging includes several actions, which pass different variables for debugging the HTTP API.
3030
*
3131
* @since 2.7.0
32+
*
33+
* @phpstan-type HTTP_Response array{
34+
* headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
35+
* body: string,
36+
* response: array{code: int|false, message: string|false},
37+
* cookies: WP_Http_Cookie[],
38+
* filename?: string|null,
39+
* http_response?: WP_HTTP_Requests_Response|null,
40+
* }
3241
*/
3342
#[AllowDynamicProperties]
3443
class WP_Http {
@@ -166,6 +175,8 @@ class WP_Http {
166175
* @type string|null $filename Optional. Filename of the response.
167176
* @type WP_HTTP_Requests_Response|null $http_response Response object.
168177
* }
178+
*
179+
* @phpstan-return HTTP_Response|WP_Error
169180
*/
170181
public function request( $url, $args = array() ) {
171182
$defaults = array(
@@ -647,6 +658,8 @@ private function _dispatch_request( $url, $args ) {
647658
* @param string|array $args Optional. Override the defaults.
648659
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
649660
* A WP_Error instance upon error. See WP_Http::response() for details.
661+
*
662+
* @phpstan-return HTTP_Response|WP_Error
650663
*/
651664
public function post( $url, $args = array() ) {
652665
$defaults = array( 'method' => 'POST' );
@@ -665,6 +678,8 @@ public function post( $url, $args = array() ) {
665678
* @param string|array $args Optional. Override the defaults.
666679
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
667680
* A WP_Error instance upon error. See WP_Http::response() for details.
681+
*
682+
* @phpstan-return HTTP_Response|WP_Error
668683
*/
669684
public function get( $url, $args = array() ) {
670685
$defaults = array( 'method' => 'GET' );
@@ -683,6 +698,8 @@ public function get( $url, $args = array() ) {
683698
* @param string|array $args Optional. Override the defaults.
684699
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
685700
* A WP_Error instance upon error. See WP_Http::response() for details.
701+
*
702+
* @phpstan-return HTTP_Response|WP_Error
686703
*/
687704
public function head( $url, $args = array() ) {
688705
$defaults = array( 'method' => 'HEAD' );

‎src/wp-includes/class-wp-widget.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ class WP_Widget {
109109
* @param array $args Display arguments including 'before_title', 'after_title',
110110
* 'before_widget', and 'after_widget'.
111111
* @param array $instance The settings for the particular instance of the widget.
112+
*
113+
* @phpstan-param array{
114+
* name?: string,
115+
* id?: string,
116+
* description?: string,
117+
* class?: string,
118+
* before_widget: string,
119+
* after_widget: string,
120+
* before_title: string,
121+
* after_title: string,
122+
* before_sidebar?: string,
123+
* after_sidebar?: string,
124+
* show_in_rest?: bool,
125+
* widget_id?: string,
126+
* widget_name?: string,
127+
* } $args
112128
*/
113129
public function widget( $args, $instance ) {
114130
die( 'function WP_Widget::widget() must be overridden in a subclass.' );

‎src/wp-includes/feed.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ function atom_enclosure() {
599599
*
600600
* @param string $data Input string.
601601
* @return array array(type, value)
602+
*
603+
* @phpstan-return array{'html'|'text'|'xhtml', string}
602604
*/
603605
function prep_atom_text_construct( $data ) {
604606
if ( ! str_contains( $data, '<' ) && ! str_contains( $data, '&' ) ) {

‎src/wp-includes/formatting.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ function wpautop( $text, $br = true ) {
611611
*
612612
* @param string $input The text which has to be formatted.
613613
* @return string[] Array of the formatted text.
614+
*
615+
* @phpstan-return non-empty-list<string>
614616
*/
615617
function wp_html_split( $input ) {
616618
return preg_split( get_html_split_regex(), $input, -1, PREG_SPLIT_DELIM_CAPTURE );

‎src/wp-includes/functions.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8394,6 +8394,7 @@ function wp_unique_prefixed_id( $prefix = '' ) {
83948394
* @param string $prefix Optional. A prefix to prepend to the generated ID. Default empty string.
83958395
* @return string The generated unique ID for the array.
83968396
*
8397+
* @phpstan-param non-empty-array $data
83978398
* @phpstan-return ( $prefix is lowercase-string ? lowercase-string&non-falsy-string : non-falsy-string )
83988399
*/
83998400
function wp_unique_id_from_values( array $data, string $prefix = '' ): string {

0 commit comments

Comments
 (0)