I18N: Initialize WP_Locale array properties.

Initializing the `WP_Locale` array properties to an empty array at the class definition point. Why?

* Ensure the properties initialize to an `array` data type at instantiation (rather than `null`).

This initialization is needed to ensure the properties are not `null` if another class inherits from `WP_Locale` but does not run `WP_Locale::init()` from the constructor. In this case, the initialization prevents

{{{
Warning: array_values() expects parameter 1 to be array, null given
}}}

when Core uses any of the properties.

* Good design practice.

The code and documentation are clearly expecting these properties to be an `array` data type. Setting each to a default `array()` state further helps to clearly communicate the code design.

Follow-up to [37889], [36292], [31078], [3676], [6589].

Props tyxla, SergeyBiryukov, azaozz, hellofromTonya, mukesh27.
See #57427.

git-svn-id: https://develop.svn.wordpress.org/trunk@55047 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork 2023-01-10 13:59:00 +00:00
parent 234698c551
commit 0d39c0aa1e
2 changed files with 57 additions and 8 deletions

View File

@ -19,9 +19,10 @@ class WP_Locale {
* Stores the translated strings for the full weekday names.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $weekday;
public $weekday = array();
/**
* Stores the translated strings for the one character weekday names.
@ -32,41 +33,46 @@ class WP_Locale {
* @see WP_Locale::init() for how to handle the hack.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $weekday_initial;
public $weekday_initial = array();
/**
* Stores the translated strings for the abbreviated weekday names.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $weekday_abbrev;
public $weekday_abbrev = array();
/**
* Stores the translated strings for the full month names.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $month;
public $month = array();
/**
* Stores the translated strings for the month names in genitive case, if the locale specifies.
*
* @since 4.4.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $month_genitive;
public $month_genitive = array();
/**
* Stores the translated strings for the abbreviated month names.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $month_abbrev;
public $month_abbrev = array();
/**
* Stores the translated strings for 'am' and 'pm'.
@ -74,9 +80,10 @@ class WP_Locale {
* Also the capitalized versions.
*
* @since 2.1.0
* @since 6.2.0 Initialized to an empty array.
* @var string[]
*/
public $meridiem;
public $meridiem = array();
/**
* The text direction of the locale language.
@ -92,9 +99,10 @@ class WP_Locale {
* The thousands separator and decimal point values used for localizing numbers.
*
* @since 2.3.0
* @since 6.2.0 Initialized to an empty array.
* @var array
*/
public $number_format;
public $number_format = array();
/**
* The separator string used for localizing list item separator.

View File

@ -15,6 +15,39 @@ class Tests_Locale extends WP_UnitTestCase {
$this->locale = new WP_Locale();
}
/**
* @ticket 57427
*
* @dataProvider data_property_initializes_to_array
*
* @param string $name Property name to test.
*/
public function test_property_initializes_to_array( $name ) {
$this->assertIsArray( $this->locale->$name, "WP_Locale::{$name} property should be an array" );
// Test a custom implementation when `init()` is not invoked in the constructor.
$wp_locale = new Custom_WP_Locale();
$this->assertIsArray( $wp_locale->$name, "Custom_WP_Locale::{$name} property should be an array" );
}
/**
* Data provider.
*
* @return array
*/
public function data_property_initializes_to_array() {
return array(
'weekday' => array( 'weekday' ),
'weekday_initial' => array( 'weekday_initial' ),
'weekday_abbrev' => array( 'weekday_abbrev' ),
'month' => array( 'month' ),
'month_genitive' => array( 'month_genitive' ),
'month_abbrev' => array( 'month_abbrev' ),
'meridiem' => array( 'meridiem' ),
'number_format' => array( 'number_format' ),
);
}
/**
* @covers WP_Locale::get_weekday
*/
@ -141,3 +174,11 @@ class Tests_Locale extends WP_UnitTestCase {
$this->assertFalse( $this->locale->is_rtl() );
}
}
class Custom_WP_Locale extends WP_Locale {
public function __construct() {
// Do not initialize to test property initialization.
// $this->init();
$this->register_globals();
}
}