mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user