Skip to content

Commit 0d39c0a

Browse files
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
1 parent 234698c commit 0d39c0a

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class WP_Locale {
1919
* Stores the translated strings for the full weekday names.
2020
*
2121
* @since 2.1.0
22+
* @since 6.2.0 Initialized to an empty array.
2223
* @var string[]
2324
*/
24-
public $weekday;
25+
public $weekday = array();
2526

2627
/**
2728
* Stores the translated strings for the one character weekday names.
@@ -32,51 +33,57 @@ class WP_Locale {
3233
* @see WP_Locale::init() for how to handle the hack.
3334
*
3435
* @since 2.1.0
36+
* @since 6.2.0 Initialized to an empty array.
3537
* @var string[]
3638
*/
37-
public $weekday_initial;
39+
public $weekday_initial = array();
3840

3941
/**
4042
* Stores the translated strings for the abbreviated weekday names.
4143
*
4244
* @since 2.1.0
45+
* @since 6.2.0 Initialized to an empty array.
4346
* @var string[]
4447
*/
45-
public $weekday_abbrev;
48+
public $weekday_abbrev = array();
4649

4750
/**
4851
* Stores the translated strings for the full month names.
4952
*
5053
* @since 2.1.0
54+
* @since 6.2.0 Initialized to an empty array.
5155
* @var string[]
5256
*/
53-
public $month;
57+
public $month = array();
5458

5559
/**
5660
* Stores the translated strings for the month names in genitive case, if the locale specifies.
5761
*
5862
* @since 4.4.0
63+
* @since 6.2.0 Initialized to an empty array.
5964
* @var string[]
6065
*/
61-
public $month_genitive;
66+
public $month_genitive = array();
6267

6368
/**
6469
* Stores the translated strings for the abbreviated month names.
6570
*
6671
* @since 2.1.0
72+
* @since 6.2.0 Initialized to an empty array.
6773
* @var string[]
6874
*/
69-
public $month_abbrev;
75+
public $month_abbrev = array();
7076

7177
/**
7278
* Stores the translated strings for 'am' and 'pm'.
7379
*
7480
* Also the capitalized versions.
7581
*
7682
* @since 2.1.0
83+
* @since 6.2.0 Initialized to an empty array.
7784
* @var string[]
7885
*/
79-
public $meridiem;
86+
public $meridiem = array();
8087

8188
/**
8289
* The text direction of the locale language.
@@ -92,9 +99,10 @@ class WP_Locale {
9299
* The thousands separator and decimal point values used for localizing numbers.
93100
*
94101
* @since 2.3.0
102+
* @since 6.2.0 Initialized to an empty array.
95103
* @var array
96104
*/
97-
public $number_format;
105+
public $number_format = array();
98106

99107
/**
100108
* The separator string used for localizing list item separator.

tests/phpunit/tests/locale.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,39 @@ public function set_up() {
1515
$this->locale = new WP_Locale();
1616
}
1717

18+
/**
19+
* @ticket 57427
20+
*
21+
* @dataProvider data_property_initializes_to_array
22+
*
23+
* @param string $name Property name to test.
24+
*/
25+
public function test_property_initializes_to_array( $name ) {
26+
$this->assertIsArray( $this->locale->$name, "WP_Locale::{$name} property should be an array" );
27+
28+
// Test a custom implementation when `init()` is not invoked in the constructor.
29+
$wp_locale = new Custom_WP_Locale();
30+
$this->assertIsArray( $wp_locale->$name, "Custom_WP_Locale::{$name} property should be an array" );
31+
}
32+
33+
/**
34+
* Data provider.
35+
*
36+
* @return array
37+
*/
38+
public function data_property_initializes_to_array() {
39+
return array(
40+
'weekday' => array( 'weekday' ),
41+
'weekday_initial' => array( 'weekday_initial' ),
42+
'weekday_abbrev' => array( 'weekday_abbrev' ),
43+
'month' => array( 'month' ),
44+
'month_genitive' => array( 'month_genitive' ),
45+
'month_abbrev' => array( 'month_abbrev' ),
46+
'meridiem' => array( 'meridiem' ),
47+
'number_format' => array( 'number_format' ),
48+
);
49+
}
50+
1851
/**
1952
* @covers WP_Locale::get_weekday
2053
*/
@@ -141,3 +174,11 @@ public function test_is_rtl() {
141174
$this->assertFalse( $this->locale->is_rtl() );
142175
}
143176
}
177+
178+
class Custom_WP_Locale extends WP_Locale {
179+
public function __construct() {
180+
// Do not initialize to test property initialization.
181+
// $this->init();
182+
$this->register_globals();
183+
}
184+
}

0 commit comments

Comments
 (0)