Make WordPress Core

Opened 6 years ago

Last modified 2 years ago

#43372 new defect (bug)

$wp_query->max_num_pages return value as float

Reported by: ironghost63's profile ironghost63 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: trivial Version: 4.9.4
Component: Query Keywords:
Focuses: Cc:

Description

As a page number, Integer would make more sense than Float.

This is not a big problem but kinda annoying when you do strict comparison on this value. since no one will define a page number as float.

Change History (6)

#1 @ironghost63
6 years ago

  • Severity changed from normal to trivial

#2 follow-up: @birgire
6 years ago

@ironghost63 Welcome to WordPress Trac and thank you for the report.

But there is more!

The example:

$q = new WP_Query( ['posts_per_page' => 2 ] );
var_dump( $q->post_count );
var_dump( $q->found_posts );
var_dump( $q->max_num_pages );

displays:

int(2)
string(1) "2"
double(1)

So these counting properties of WP_Query have three different types!

Within the WP_Query class, the inline docs says it's an integer:

/**
 * The amount of pages.
 *
 * @since 2.1.0
 * @var int
 */
public $max_num_pages = 0;

The calculated value comes from:

$this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );

but from the PHP docs:

The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer.

The inline docs for found_posts also says:

/**
 * The amount of found posts for the current query.
 *
 * If limit clause was not used, equals $post_count.
 *
 * @since 2.1.0
 * @var int
 */
public $found_posts = 0;

but the data query

$this->found_posts = $wpdb->get_var(...);

returns a string.

So it looks like either we need to adjust the inline docs or adjust the return types.

#3 @SergeyBiryukov
2 years ago

#55137 was marked as a duplicate.

#4 @johnbillion
2 years ago

I noticed this while working on #47280. All the tests use assertEquals instead of assertSame for this reason. The property is indeed documented as an int but is a float.

#5 in reply to: ↑ 2 @SergeyBiryukov
2 years ago

Also noticed this in comment:19:ticket:38266.

Replying to birgire:

So it looks like either we need to adjust the inline docs or adjust the return types.

I think it makes sense to do the latter here, to adjust the return types to match the documented types.

#6 @audrasjb
2 years ago

The same goes for other related queries:

  • WP_Network_Query
  • WP_Comment_Query
  • WP_Site_Query
Note: See TracTickets for help on using tickets.