Skip to content

Commit 00a656a

Browse files
committed
Code Quality: Add PHPStan integer ranges and literal types.
Narrow integer return and parameter types to ranges such as `non-negative-int` and `positive-int`, and string and integer types to the literal values a function can actually take or return, so that static analysis can catch more mistakes. Developed in: #13893 Props swissspidy, westonruter, marian1. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65776 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9fedd3c commit 00a656a

22 files changed

Lines changed: 131 additions & 40 deletions

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @since 2.0.0
1313
*
1414
* @return int The link ID on success. The value 0 on failure.
15+
*
16+
* @phpstan-return non-negative-int
1517
*/
1618
function add_link() {
1719
return edit_link();
@@ -24,6 +26,8 @@ function add_link() {
2426
*
2527
* @param int $link_id Optional. ID of the link to edit. Default 0.
2628
* @return int The link ID on success. The value 0 on failure.
29+
*
30+
* @phpstan-return non-negative-int
2731
*/
2832
function edit_link( $link_id = 0 ) {
2933
if ( ! current_user_can( 'manage_links' ) ) {
@@ -174,7 +178,7 @@ function get_link_to_edit( $link ) {
174178
* @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false.
175179
* @return int|WP_Error The link ID on success. The value 0 or WP_Error on failure.
176180
*
177-
* @phpstan-return ( $wp_error is false ? int : int|WP_Error )
181+
* @phpstan-return ( $wp_error is false ? non-negative-int : non-negative-int|WP_Error )
178182
*/
179183
function wp_insert_link( $linkdata, $wp_error = false ) {
180184
global $wpdb;
@@ -189,10 +193,14 @@ function wp_insert_link( $linkdata, $wp_error = false ) {
189193
$parsed_args = wp_parse_args( $linkdata, $defaults );
190194
$parsed_args = wp_unslash( sanitize_bookmark( $parsed_args, 'db' ) );
191195

192-
$link_id = $parsed_args['link_id'];
196+
$link_id = (int) $parsed_args['link_id'];
193197
$link_name = $parsed_args['link_name'];
194198
$link_url = $parsed_args['link_url'];
195199

200+
if ( $link_id < 0 ) {
201+
return 0;
202+
}
203+
196204
$update = false;
197205
if ( ! empty( $link_id ) ) {
198206
$update = true;
@@ -301,6 +309,8 @@ function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
301309
*
302310
* @param array $linkdata Link data to update. See wp_insert_link() for accepted arguments.
303311
* @return int The updated link ID on success. The value 0 on failure.
312+
*
313+
* @phpstan-return non-negative-int
304314
*/
305315
function wp_update_link( $linkdata ) {
306316
$link_id = (int) $linkdata['link_id'];

‎src/wp-admin/includes/class-custom-image-header.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ public function process_default_headers() {
318318
*
319319
* @param string $type The header type. One of 'default' (for the Uploaded Images control)
320320
* or 'uploaded' (for the Uploaded Images control).
321+
*
322+
* @phpstan-param 'default'|'uploaded' $type
321323
*/
322324
public function show_header_selector( $type = 'default' ) {
323325
if ( 'default' === $type ) {

‎src/wp-admin/includes/class-wp-importer.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ public function min_whitespace( $text ) {
280280
*
281281
* @since 3.0.0
282282
*
283-
* @global wpdb $wpdb WordPress database abstraction object.
284-
* @global int[] $wp_actions Stores the number of times each action was triggered.
283+
* @global wpdb $wpdb WordPress database abstraction object.
284+
* @global array<string, positive-int> $wp_actions Stores the number of times each action was triggered.
285285
*/
286286
public function stop_the_insanity() {
287287
global $wpdb, $wp_actions;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function wp_create_categories( $categories, $post_id = 0 ) {
119119
* depending on param `$wp_error`.
120120
*
121121
* @phpstan-return (
122-
* $wp_error is false ? int : int|WP_Error
122+
* $wp_error is false ? non-negative-int : non-negative-int|WP_Error
123123
* )
124124
*/
125125
function wp_insert_category( $catarr, $wp_error = false ) {
@@ -188,6 +188,8 @@ function wp_insert_category( $catarr, $wp_error = false ) {
188188
*
189189
* @param array $catarr The 'cat_ID' value is required. All other keys are optional.
190190
* @return int|false The ID number of the new or updated Category on success. Zero or FALSE on failure.
191+
*
192+
* @phpstan-return non-negative-int|false
191193
*/
192194
function wp_update_category( $catarr ) {
193195
$cat_id = (int) $catarr['cat_ID'];

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,8 @@ function _get_plugin_from_callback( $callback ) {
12901290
* Often this is the object that's the focus of the current screen,
12911291
* for example a `WP_Post` or `WP_Comment` object.
12921292
* @return int Number of meta_boxes.
1293+
*
1294+
* @phpstan-return non-negative-int
12931295
*/
12941296
function do_meta_boxes( $screen, $context, $data_object ) {
12951297
global $wp_meta_boxes;

‎src/wp-includes/bookmark.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,15 @@ function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
8080
* @param string $field The name of the data field to return.
8181
* @param int $bookmark The bookmark ID to get field.
8282
* @param string $context Optional. The context of how the field will be used. Default 'display'.
83-
* @return string|WP_Error
83+
* @return mixed The sanitized field value, or an empty string if the bookmark or field does not exist.
84+
*
85+
* @phpstan-param 'link_id'|'link_url'|'link_name'|'link_image'|'link_target'|'link_description'|'link_visible'|'link_owner'|'link_rating'|'link_updated'|'link_rel'|'link_notes'|'link_rss'|'link_category' $field
86+
* @phpstan-return ( $field is 'link_category' ? array<int, non-negative-int>|'' : ( $field is 'link_id'|'link_rating' ? int|'' : mixed ) )
8487
*/
8588
function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
8689
$bookmark = (int) $bookmark;
8790
$bookmark = get_bookmark( $bookmark );
8891

89-
if ( is_wp_error( $bookmark ) ) {
90-
return $bookmark;
91-
}
92-
9392
if ( ! is_object( $bookmark ) ) {
9493
return '';
9594
}
@@ -403,6 +402,9 @@ function sanitize_bookmark( $bookmark, $context = 'display' ) {
403402
* @param string $context How to filter the field value. Accepts 'raw', 'edit', 'db',
404403
* 'display', 'attribute', or 'js'. Default 'display'.
405404
* @return mixed The filtered value.
405+
*
406+
* @phpstan-param 'link_id'|'link_url'|'link_name'|'link_image'|'link_target'|'link_description'|'link_visible'|'link_owner'|'link_rating'|'link_updated'|'link_rel'|'link_notes'|'link_rss'|'link_category' $field
407+
* @phpstan-return ($field is 'link_category' ? array<int, non-negative-int> : ($field is 'link_id'|'link_rating' ? int : mixed))
406408
*/
407409
function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
408410
$int_fields = array( 'link_id', 'link_rating' );

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class WP_Locale {
119119
*
120120
* @since 6.2.0
121121
* @var string|null
122+
*
123+
* @phpstan-var 'characters_excluding_spaces'|'characters_including_spaces'|'words'|null
122124
*/
123125
public $word_count_type;
124126

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class wpdb {
125125
* @since 0.71
126126
*
127127
* @var int
128+
* @phpstan-var non-negative-int
128129
*/
129130
public $insert_id = 0;
130131

@@ -3121,6 +3122,8 @@ public function get_var( $query = null, $x = 0, $y = 0 ) {
31213122
* )
31223123
* : null
31233124
* )
3125+
*
3126+
* @phpstan-param non-negative-int $y
31243127
*/
31253128
public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
31263129
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";

‎src/wp-includes/comment.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,8 @@ function wp_unspam_comment( $comment_id ) {
19931993
*
19941994
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object
19951995
* @return string|false Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
1996+
*
1997+
* @phpstan-return 'approved'|'spam'|'trash'|'unapproved'|false
19961998
*/
19971999
function wp_get_comment_status( $comment_id ) {
19982000
$comment = get_comment( $comment_id );

‎src/wp-includes/formatting.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,7 @@ function format_to_edit( $content, $rich_text = false ) {
28352835
* @param int $threshold Digit places number needs to be to not have zeros added.
28362836
* @return string Adds leading zeros to number if needed.
28372837
*
2838+
* @phpstan-param non-negative-int $threshold
28382839
* @phpstan-return (
28392840
* $threshold is 0
28402841
* ? lowercase-string&non-empty-string&numeric-string
@@ -3006,6 +3007,8 @@ function urldecode_deep( $value ) {
30063007
* @param string $email_address Email address.
30073008
* @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
30083009
* @return string Converted email address.
3010+
*
3011+
* @phpstan-param 0|1 $hex_encoding
30093012
*/
30103013
function antispambot( $email_address, $hex_encoding = 0 ) {
30113014
$obfuscated = '';

0 commit comments

Comments
 (0)