Make WordPress Core

Opened 3 weeks ago

Last modified 2 weeks ago

#64522 new defect (bug)

Clarify expected behavior for invalid weekday values in WP_Locale::get_weekday()

Reported by: shishir1's profile shishir1 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.5
Component: I18N Keywords:
Focuses: Cc:

Description

While working on a pull request to prevent an undefined index notice in WP_Locale::get_weekday(), it became clear that the current behavior for invalid weekday values (e.g. 7) is intentional and covered by existing unit tests.

This ticket is to discuss whether the current behavior should remain as-is, or whether a more explicit developer-facing warning (such as using _doing_it_wrong()) would be more appropriate, particularly for newer PHP versions.

Related PR: https://github.com/WordPress/wordpress-develop/pull/10722

Feedback and guidance would be appreciated before proposing any changes.

Change History (3)

#1 @westonruter
3 weeks ago

  • Version changed from trunk to 4.5

This is related to #34688 to follow up on code introduced in [36292].

#2 @swissspidy
3 weeks ago

At first glance I don't see why we would want to replace perfectly valid error flow (PHP notice/warning) with our own validatiob & doing_it_wrong.

The current behavior already tells the developer that their code is wrong.

#3 @westonruter
2 weeks ago

With PHPStan ($61175), static analysis would catch this via:

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

    diff --git a/src/wp-includes/class-wp-locale.php b/src/wp-includes/class-wp-locale.php
    index 0fcb130b81..bd15992a5a 100644
    a b class WP_Locale { 
    262262         *
    263263         * @since 2.1.0
    264264         *
    265          * @param int $weekday_number 0 for Sunday through 6 Saturday.
     265         * @param int<0, 6> $weekday_number 0 for Sunday through 6 Saturday.
    266266         * @return string Full translated weekday.
    267267         */
    268268        public function get_weekday( $weekday_number ) {

Also, the issue with an invalid argument being passed to ::get_weekday() is also present for ::get_weekday_initial(), ::get_weekday_abbrev(), etc.

Note: See TracTickets for help on using tickets.