From 6cd08dc7bdebb496db13865c3c7e1391d45fc388 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 21 Nov 2019 11:52:47 -0300 Subject: [PATCH] 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. --- includes/class-wc-data-store.php | 5 +++-- tests/framework/class-wc-dummy-data-store.php | 9 +++++++++ tests/unit-tests/crud/data-store.php | 13 +++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-data-store.php b/includes/class-wc-data-store.php index c7e03d12486..f6723b132d6 100644 --- a/includes/class-wc-data-store.php +++ b/includes/class-wc-data-store.php @@ -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 ); } } } diff --git a/tests/framework/class-wc-dummy-data-store.php b/tests/framework/class-wc-dummy-data-store.php index 05680731a65..cf503d5e3c6 100644 --- a/tests/framework/class-wc-dummy-data-store.php +++ b/tests/framework/class-wc-dummy-data-store.php @@ -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 ); + } } /** diff --git a/tests/unit-tests/crud/data-store.php b/tests/unit-tests/crud/data-store.php index f5f70bb44df..8b64d365839 100644 --- a/tests/unit-tests/crud/data-store.php +++ b/tests/unit-tests/crud/data-store.php @@ -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. */ /**