Skip to content

Commit 4e77779

Browse files
committed
Docs: Allow numeric strings for date parts and meta IDs.
The `wp_checkdate()` and `*_metadata_by_mid()` functions already validate their numeric arguments with `is_numeric()` and cast them to integers, and core callers pass strings: date parts cut from a date string, and meta IDs read with `$wpdb->get_col()`. Their docblocks still documented `int` only, which caused 17 PHPStan `argument.type` errors. The arguments are now documented as `int|string`, with PHPStan narrowing them to `int|numeric-string`. The `checkdate()` ranges are deliberately not applied to `wp_checkdate()`, since the function's purpose is to validate input that may be out of range. Developed in #15472. Follow-up to r18494, r20435, r38699, r61172. Props pwtyler, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65269 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f59de42 commit 4e77779

3 files changed

Lines changed: 21 additions & 91 deletions

File tree

‎src/wp-includes/functions.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7631,11 +7631,15 @@ function wp_is_stream( $path ) {
76317631
*
76327632
* @link https://www.php.net/manual/en/function.checkdate.php
76337633
*
7634-
* @param int $month Month number.
7635-
* @param int $day Day number.
7636-
* @param int $year Year number.
7637-
* @param string $source_date The date to filter.
7634+
* @param int|string $month Month number.
7635+
* @param int|string $day Day number.
7636+
* @param int|string $year Year number.
7637+
* @param string $source_date The date to filter.
76387638
* @return bool True if valid date, false if not valid date.
7639+
*
7640+
* @phpstan-param int|numeric-string $month
7641+
* @phpstan-param int|numeric-string $day
7642+
* @phpstan-param int|numeric-string $year
76397643
*/
76407644
function wp_checkdate( $month, $day, $year, $source_date ) {
76417645
$checkdate = false;

‎src/wp-includes/meta.php‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,9 @@ function metadata_exists( $meta_type, $object_id, $meta_key ) {
840840
*
841841
* @global wpdb $wpdb WordPress database abstraction object.
842842
*
843-
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
844-
* 'user', or any other object type with an associated meta table.
845-
* @param int $meta_id ID for a specific meta row.
843+
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
844+
* 'user', or any other object type with an associated meta table.
845+
* @param int|string $meta_id ID for a specific meta row.
846846
* @return stdClass|false {
847847
* Metadata object, or boolean `false` if the metadata doesn't exist.
848848
*
@@ -856,6 +856,8 @@ function metadata_exists( $meta_type, $object_id, $meta_key ) {
856856
* @type string $term_id Optional. The object ID when the meta type is 'term'.
857857
* @type string $user_id Optional. The object ID when the meta type is 'user'.
858858
* }
859+
*
860+
* @phpstan-param int|numeric-string $meta_id
859861
*/
860862
function get_metadata_by_mid( $meta_type, $meta_id ) {
861863
global $wpdb;
@@ -923,10 +925,12 @@ function get_metadata_by_mid( $meta_type, $meta_id ) {
923925
*
924926
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
925927
* 'user', or any other object type with an associated meta table.
926-
* @param int $meta_id ID for a specific meta row.
928+
* @param int|string $meta_id ID for a specific meta row.
927929
* @param string $meta_value Metadata value. Must be serializable if non-scalar.
928930
* @param string|false $meta_key Optional. You can provide a meta key to update it. Default false.
929931
* @return bool True on successful update, false on failure.
932+
*
933+
* @phpstan-param int|numeric-string $meta_id
930934
*/
931935
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
932936
global $wpdb;
@@ -1048,10 +1052,12 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
10481052
*
10491053
* @global wpdb $wpdb WordPress database abstraction object.
10501054
*
1051-
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1052-
* 'user', or any other object type with an associated meta table.
1053-
* @param int $meta_id ID for a specific meta row.
1055+
* @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1056+
* 'user', or any other object type with an associated meta table.
1057+
* @param int|string $meta_id ID for a specific meta row.
10541058
* @return bool True on successful delete, false on failure.
1059+
*
1060+
* @phpstan-param int|numeric-string $meta_id
10551061
*/
10561062
function delete_metadata_by_mid( $meta_type, $meta_id ) {
10571063
global $wpdb;

‎tests/phpstan/baselines/argument.type.neon‎

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ parameters:
218218
identifier: argument.type
219219
count: 1
220220
path: ../../../src/wp-admin/includes/ms.php
221-
-
222-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
223-
identifier: argument.type
224-
count: 1
225-
path: ../../../src/wp-admin/includes/ms.php
226221
-
227222
message: '#^Parameter \#3 \$value of function update_blog_status expects string, int given\.$#'
228223
identifier: argument.type
@@ -333,11 +328,6 @@ parameters:
333328
identifier: argument.type
334329
count: 1
335330
path: ../../../src/wp-admin/includes/user.php
336-
-
337-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
338-
identifier: argument.type
339-
count: 1
340-
path: ../../../src/wp-admin/includes/user.php
341331
-
342332
message: '#^Parameter \#4 \$is_public of function wp_install expects bool, int given\.$#'
343333
identifier: argument.type
@@ -688,26 +678,11 @@ parameters:
688678
identifier: argument.type
689679
count: 1
690680
path: ../../../src/wp-includes/class-wp-customize-manager.php
691-
-
692-
message: '#^Parameter \#1 \$month of function wp_checkdate expects int, \(string\|false\) given\.$#'
693-
identifier: argument.type
694-
count: 1
695-
path: ../../../src/wp-includes/class-wp-customize-manager.php
696681
-
697682
message: '#^Parameter \#1 \$postarr of function wp_insert_post expects array\{ID\?\: int, post_author\?\: int, post_date\?\: string, post_date_gmt\?\: string, post_content\?\: string, post_content_filtered\?\: string, post_title\?\: string, post_excerpt\?\: string, \.\.\., \.\.\.\}, array\<int\<min, \-1\>\|int\<1, max\>\|string\|false\> given\.$#'
698683
identifier: argument.type
699684
count: 1
700685
path: ../../../src/wp-includes/class-wp-customize-manager.php
701-
-
702-
message: '#^Parameter \#2 \$day of function wp_checkdate expects int, \(string\|false\) given\.$#'
703-
identifier: argument.type
704-
count: 1
705-
path: ../../../src/wp-includes/class-wp-customize-manager.php
706-
-
707-
message: '#^Parameter \#3 \$year of function wp_checkdate expects int, \(string\|false\) given\.$#'
708-
identifier: argument.type
709-
count: 1
710-
path: ../../../src/wp-includes/class-wp-customize-manager.php
711686
-
712687
message: '#^Parameter \#1 \$postarr of function wp_insert_post expects array\{ID\?\: int, post_author\?\: int, post_date\?\: string, post_date_gmt\?\: string, post_content\?\: string, post_content_filtered\?\: string, post_title\?\: string, post_excerpt\?\: string, \.\.\., \.\.\.\}, array\<string\> given\.$#'
713688
identifier: argument.type
@@ -818,11 +793,6 @@ parameters:
818793
identifier: argument.type
819794
count: 1
820795
path: ../../../src/wp-includes/comment.php
821-
-
822-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
823-
identifier: argument.type
824-
count: 1
825-
path: ../../../src/wp-includes/comment.php
826796
-
827797
message: '#^Parameter \#1 \$gmt_time of function spawn_cron expects int, float given\.$#'
828798
identifier: argument.type
@@ -883,11 +853,6 @@ parameters:
883853
identifier: argument.type
884854
count: 2
885855
path: ../../../src/wp-includes/functions.php
886-
-
887-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
888-
identifier: argument.type
889-
count: 1
890-
path: ../../../src/wp-includes/functions.php
891856
-
892857
message: '#^Parameter \#3 \$number of function _n expects int, string given\.$#'
893858
identifier: argument.type
@@ -993,26 +958,6 @@ parameters:
993958
identifier: argument.type
994959
count: 1
995960
path: ../../../src/wp-includes/ms-load.php
996-
-
997-
message: '#^Parameter \#1 \$month of function wp_checkdate expects int, \(string\|false\) given\.$#'
998-
identifier: argument.type
999-
count: 1
1000-
path: ../../../src/wp-includes/ms-site.php
1001-
-
1002-
message: '#^Parameter \#2 \$day of function wp_checkdate expects int, \(string\|false\) given\.$#'
1003-
identifier: argument.type
1004-
count: 1
1005-
path: ../../../src/wp-includes/ms-site.php
1006-
-
1007-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
1008-
identifier: argument.type
1009-
count: 1
1010-
path: ../../../src/wp-includes/ms-site.php
1011-
-
1012-
message: '#^Parameter \#3 \$year of function wp_checkdate expects int, \(string\|false\) given\.$#'
1013-
identifier: argument.type
1014-
count: 1
1015-
path: ../../../src/wp-includes/ms-site.php
1016961
-
1017962
message: '#^Parameter \#1 \$post_id of function wp_delete_post expects int, string given\.$#'
1018963
identifier: argument.type
@@ -1088,11 +1033,6 @@ parameters:
10881033
identifier: argument.type
10891034
count: 2
10901035
path: ../../../src/wp-includes/post.php
1091-
-
1092-
message: '#^Parameter \#1 \$month of function wp_checkdate expects int, string given\.$#'
1093-
identifier: argument.type
1094-
count: 1
1095-
path: ../../../src/wp-includes/post.php
10961036
-
10971037
message: '#^Parameter \#1 \$post of function clean_post_cache expects int\|WP_Post, stdClass given\.$#'
10981038
identifier: argument.type
@@ -1123,21 +1063,6 @@ parameters:
11231063
identifier: argument.type
11241064
count: 3
11251065
path: ../../../src/wp-includes/post.php
1126-
-
1127-
message: '#^Parameter \#2 \$day of function wp_checkdate expects int, string given\.$#'
1128-
identifier: argument.type
1129-
count: 1
1130-
path: ../../../src/wp-includes/post.php
1131-
-
1132-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
1133-
identifier: argument.type
1134-
count: 2
1135-
path: ../../../src/wp-includes/post.php
1136-
-
1137-
message: '#^Parameter \#3 \$year of function wp_checkdate expects int, string given\.$#'
1138-
identifier: argument.type
1139-
count: 1
1140-
path: ../../../src/wp-includes/post.php
11411066
-
11421067
message: '#^Parameter \#1 \$response of method WP_REST_Server\:\:response_to_data\(\) expects WP_REST_Response, WP_HTTP_Response given\.$#'
11431068
identifier: argument.type
@@ -1273,11 +1198,6 @@ parameters:
12731198
identifier: argument.type
12741199
count: 2
12751200
path: ../../../src/wp-includes/taxonomy.php
1276-
-
1277-
message: '#^Parameter \#2 \$meta_id of function delete_metadata_by_mid expects int, string\|null given\.$#'
1278-
identifier: argument.type
1279-
count: 1
1280-
path: ../../../src/wp-includes/taxonomy.php
12811201
-
12821202
message: '#^Parameter \#2 \$taxonomy of function wp_update_term_count expects string, null given\.$#'
12831203
identifier: argument.type

0 commit comments

Comments
 (0)