Use the spread operator instead of call_user_func_array() in WC_Data_Store

This commit replaces a call to call_user_func_array() in WC_Data_Store::__call() with argument unpacking using the spread operator which was introduced in PHP 5.6. This change should improve WooCommerce performance a tiny bit since WC_Data_Store::__call() is called somewhat frequently and call_user_func_array() has a bad performance reputation. I added one unit test to make sure this change doesn't break the functionality of the altered method.
This commit is contained in:
Rodrigo Primo 2019-11-21 11:52:47 -03:00
parent fa875a4d25
commit 6cd08dc7bd
3 changed files with 25 additions and 2 deletions

View File

@ -202,8 +202,9 @@ class WC_Data_Store {
*/
public function __call( $method, $parameters ) {
if ( is_callable( array( $this->instance, $method ) ) ) {
$object = array_shift( $parameters );
return call_user_func_array( array( $this->instance, $method ), array_merge( array( &$object ), $parameters ) );
$object = array_shift( $parameters );
$parameters = array_merge( array( &$object ), $parameters );
return $this->instance->$method( ...$parameters );
}
}
}

View File

@ -27,6 +27,15 @@ class WC_Dummy_Data_Store_CPT implements WC_Object_Data_Store_Interface {
public function delete_meta( &$data, $meta ) { }
public function add_meta( &$data, $meta ) { }
public function update_meta( &$data, $meta ) { }
/**
* Method used to test WC_Data_Store::__call().
*
* @return array
*/
public function custom_method( $first_param, $second_param, $third_param ) {
return array( $first_param, $second_param, $third_param );
}
}
/**

View File

@ -69,10 +69,23 @@ class WC_Tests_Data_Store extends WC_Unit_Test_Case {
*/
public function test_store_sub_type() {
$this->load_dummy_store();
$store = WC_Data_Store::load( 'dummy-sub' );
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
}
/**
* Test WC_Data_Store::__call().
*/
public function test_data_store_method_overloading() {
$this->load_dummy_store();
$store = WC_Data_Store::load( 'dummy-sub' );
$this->assertEquals(
array( 'first param', 'second param', 'third param' ),
$store->custom_method( 'first param', 'second param', 'third param', 'asdfsdf' )
);
}
/* Helper Functions. */
/**