From 9d5e3e3dbb32b7e0da1be8af21d6a5ba49dd7962 Mon Sep 17 00:00:00 2001 From: Dat Hoang Date: Tue, 29 Aug 2023 12:43:43 +0700 Subject: [PATCH] Update Base_Constant to return the singleton object for same static calls (#7064) --- ...se-constant-return-same-object-static-call | 4 ++++ includes/constants/class-base-constant.php | 20 ++++++++++++++----- tests/unit/test-class-base-constant.php | 6 ++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 changelog/update-base-constant-return-same-object-static-call diff --git a/changelog/update-base-constant-return-same-object-static-call b/changelog/update-base-constant-return-same-object-static-call new file mode 100644 index 00000000000..d15f76ec43e --- /dev/null +++ b/changelog/update-base-constant-return-same-object-static-call @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Update Base_Constant to return the singleton object for same static calls. diff --git a/includes/constants/class-base-constant.php b/includes/constants/class-base-constant.php index 7d396c8c1aa..6c2a177dbe1 100644 --- a/includes/constants/class-base-constant.php +++ b/includes/constants/class-base-constant.php @@ -27,12 +27,19 @@ abstract class Base_Constant implements \JsonSerializable { protected $value; /** - * Class constructor. + * Static objects cache. * - * @param mixed $value Constant from class. + * @var array + */ + protected static $object_cache = []; + + /** + * Class constructor. Keep it private to only allow initializing from __callStatic() + * + * @param string $value Constant from class. * @throws \InvalidArgumentException */ - public function __construct( $value ) { + private function __construct( string $value ) { if ( $value instanceof static ) { $value = $value->get_value(); } else { @@ -61,7 +68,7 @@ public function get_value() { * @return bool */ final public function equals( $variable = null ): bool { - return $variable instanceof Base_Constant && $this->get_value() === $variable->get_value() && static::class === \get_class( $variable ); + return $this === $variable; } /** @@ -92,7 +99,10 @@ public static function search( string $value ) { * @throws \InvalidArgumentException */ public static function __callStatic( $name, $arguments ) { - return new static( $name ); + if ( ! isset( static::$object_cache[ $name ] ) ) { + static::$object_cache[ $name ] = new static( $name ); + } + return static::$object_cache[ $name ]; } /** diff --git a/tests/unit/test-class-base-constant.php b/tests/unit/test-class-base-constant.php index 7d390c0b4bb..c4ebf7e25cf 100644 --- a/tests/unit/test-class-base-constant.php +++ b/tests/unit/test-class-base-constant.php @@ -12,6 +12,11 @@ */ class Base_Constant_Test extends WCPAY_UnitTestCase { + public function test_base_constant_retun_single_object_for_multiple_same_static_calls() { + $instance_1 = Payment_Method::BASC(); + $instance_2 = Payment_Method::BASC(); + $this->assertTrue( $instance_1 === $instance_2 ); + } public function test_base_constant_will_create_constant() { $class = Payment_Method::BASC(); $this->assertInstanceOf( Payment_Method::class, $class ); @@ -52,4 +57,5 @@ public function test_class_will_throw_exception_if_searched_by_value_that_does_n $this->expectException( \InvalidArgumentException::class ); Payment_Method::search( 'foo' ); } + }