From e5888a9a3c99db3718f6c7853e4baeb0fd7ca18b Mon Sep 17 00:00:00 2001 From: Ashley Gibson Date: Fri, 23 Aug 2024 11:49:17 +0100 Subject: [PATCH 1/5] Add can get new instance trait --- tests/bootstrap.php | 1 + .../Traits/CanGetNewInstanceTraitTest.php | 45 +++++++++++++++++ woocommerce/Traits/CanGetNewInstanceTrait.php | 50 +++++++++++++++++++ woocommerce/class-sv-wc-plugin.php | 4 ++ 4 files changed, 100 insertions(+) create mode 100644 tests/unit/Traits/CanGetNewInstanceTraitTest.php create mode 100644 woocommerce/Traits/CanGetNewInstanceTrait.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6cd225bdb..2240ae4ee 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,6 +9,7 @@ require_once PLUGIN_ROOT_DIR.'/woocommerce/class-sv-wc-plugin-exception.php'; require_once PLUGIN_ROOT_DIR.'/woocommerce/Enums/Traits/EnumTrait.php'; require_once PLUGIN_ROOT_DIR.'/woocommerce/Enums/PaymentFormContext.php'; +require_once PLUGIN_ROOT_DIR.'/woocommerce/Traits/CanGetNewInstanceTrait.php'; require_once PLUGIN_ROOT_DIR.'/woocommerce/Traits/IsSingletonTrait.php'; WP_Mock::setUsePatchwork(true); diff --git a/tests/unit/Traits/CanGetNewInstanceTraitTest.php b/tests/unit/Traits/CanGetNewInstanceTraitTest.php new file mode 100644 index 000000000..d6f96d80a --- /dev/null +++ b/tests/unit/Traits/CanGetNewInstanceTraitTest.php @@ -0,0 +1,45 @@ +getTestClass(); + $newInstance = $class::getNewInstance('value1', 'value2'); + + $this->assertSame('value1', $newInstance->arg1); + $this->assertSame('value2', $newInstance->arg2); + $this->assertInstanceOf(get_class($class), $newInstance); + } + + /** + * Anonymous Class for Testing Trait. + * + * @see testItCanGetNewInstance + */ + private function getTestClass() + { + return new class { + use CanGetNewInstanceTrait; + + public $arg1; + public $arg2; + + public function __construct($arg1 = null, $arg2 = null) + { + $this->arg1 = $arg1; + $this->arg2 = $arg2; + } + }; + } +} diff --git a/woocommerce/Traits/CanGetNewInstanceTrait.php b/woocommerce/Traits/CanGetNewInstanceTrait.php new file mode 100644 index 000000000..847802f5e --- /dev/null +++ b/woocommerce/Traits/CanGetNewInstanceTrait.php @@ -0,0 +1,50 @@ +newInstanceArgs(func_get_args()); + } +} diff --git a/woocommerce/class-sv-wc-plugin.php b/woocommerce/class-sv-wc-plugin.php index 0de1e18c1..f3dbb4e33 100644 --- a/woocommerce/class-sv-wc-plugin.php +++ b/woocommerce/class-sv-wc-plugin.php @@ -452,6 +452,10 @@ private function includes() { // common exception class require_once( $framework_path . '/class-sv-wc-plugin-exception.php' ); + // traits + require_once( $framework_path . '/Traits/CanGetNewInstanceTrait.php' ); + require_once( $framework_path . '/Traits/IsSingletonTrait.php' ); + // addresses require_once( $framework_path . '/Addresses/Address.php' ); require_once( $framework_path . '/Addresses/Customer_Address.php' ); From cb070f7602262ab13ab1ebb1f8606a7b05d84e96 Mon Sep 17 00:00:00 2001 From: Ashley Gibson Date: Fri, 23 Aug 2024 12:16:28 +0100 Subject: [PATCH 2/5] Update changelog --- woocommerce/changelog.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/woocommerce/changelog.txt b/woocommerce/changelog.txt index 6488962e6..3d1aab460 100644 --- a/woocommerce/changelog.txt +++ b/woocommerce/changelog.txt @@ -1,7 +1,8 @@ *** SkyVerge WooCommerce Plugin Framework Changelog *** 2024.nn.nn - version 5.13.1-dev.1 -* Feature - Add a trait to get a singular instance of a class IsSingletonTrait +* Feature - Add a trait to get a singular instance of a class: `IsSingletonTrait` +* Feature - Add a trait to get an instance (non-singular) of a class: `CanGetNewInstanceTrait` 2024.08.21 - version 5.13.0 * Feature - Add a trait for enum-like classes From 01934632ad2d2bc16abc045ba87c4cf3c9f1005c Mon Sep 17 00:00:00 2001 From: Nabeel Molham Date: Mon, 26 Aug 2024 13:55:45 +1000 Subject: [PATCH 3/5] Use spread operator instead of class reflections for better performance --- woocommerce/Traits/CanGetNewInstanceTrait.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/woocommerce/Traits/CanGetNewInstanceTrait.php b/woocommerce/Traits/CanGetNewInstanceTrait.php index 847802f5e..3e15592b8 100644 --- a/woocommerce/Traits/CanGetNewInstanceTrait.php +++ b/woocommerce/Traits/CanGetNewInstanceTrait.php @@ -24,8 +24,6 @@ namespace SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits; -use ReflectionClass; - defined('ABSPATH') or exit; if (trait_exists('\\SkyVerge\\WooCommerce\\PluginFramework\\v5_13_1\\Traits\\CanGetNewInstanceTrait')) { @@ -43,8 +41,8 @@ trait CanGetNewInstanceTrait * * @return static */ - public static function getNewInstance() + public static function getNewInstance(...$args) { - return (new ReflectionClass(static::class))->newInstanceArgs(func_get_args()); + return new static(...$args); } } From fce9ec719d5f46e2b64cd85dbab83f2b62d71e58 Mon Sep 17 00:00:00 2001 From: Nabeel Molham Date: Mon, 26 Aug 2024 13:56:53 +1000 Subject: [PATCH 4/5] Use spread operator instead of func_get_args --- woocommerce/Traits/IsSingletonTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/woocommerce/Traits/IsSingletonTrait.php b/woocommerce/Traits/IsSingletonTrait.php index 4d4e93ed6..447360998 100644 --- a/woocommerce/Traits/IsSingletonTrait.php +++ b/woocommerce/Traits/IsSingletonTrait.php @@ -50,9 +50,9 @@ public static function isLoaded() : bool * * @return static */ - public static function getInstance() + public static function getInstance(...$args) { - return static::$instance ??= new static(...func_get_args()); + return static::$instance ??= new static(...$args); } /** From 6f266df3d2c99caf8d2f3cbb5ecadd9fd5c5eb52 Mon Sep 17 00:00:00 2001 From: Nabeel Molham Date: Mon, 26 Aug 2024 13:56:59 +1000 Subject: [PATCH 5/5] Update unit tests --- .../Traits/CanGetNewInstanceTraitTest.php | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/unit/Traits/CanGetNewInstanceTraitTest.php b/tests/unit/Traits/CanGetNewInstanceTraitTest.php index d6f96d80a..1604e7877 100644 --- a/tests/unit/Traits/CanGetNewInstanceTraitTest.php +++ b/tests/unit/Traits/CanGetNewInstanceTraitTest.php @@ -8,11 +8,11 @@ class CanGetNewInstanceTraitTest extends TestCase { /** - * Tests that it can get new instance. + * Tests that it can get new instance with arguments. * * @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\CanGetNewInstanceTrait::getNewInstance() */ - public function testItCanGetNewInstance() + public function testItCanGetNewInstanceWithArgs() : void { $class = $this->getTestClass(); $newInstance = $class::getNewInstance('value1', 'value2'); @@ -22,6 +22,21 @@ public function testItCanGetNewInstance() $this->assertInstanceOf(get_class($class), $newInstance); } + /** + * Tests that it can get new instance without arguments. + * + * @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\CanGetNewInstanceTrait::getNewInstance() + */ + public function testItCanGetNewInstanceWithoutArgs() : void + { + $class = $this->getTestClass(); + $newInstance = $class::getNewInstance(); + + $this->assertNull($newInstance->arg1); + $this->assertNull($newInstance->arg2); + $this->assertInstanceOf(get_class($class), $newInstance); + } + /** * Anonymous Class for Testing Trait. * @@ -32,10 +47,10 @@ private function getTestClass() return new class { use CanGetNewInstanceTrait; - public $arg1; - public $arg2; + public ?string $arg1 = null; + public ?string $arg2 = null; - public function __construct($arg1 = null, $arg2 = null) + public function __construct(?string $arg1 = null, ?string $arg2 = null) { $this->arg1 = $arg1; $this->arg2 = $arg2;