11<?php
22/**
3- * Class for storing dynamic properties for order object.
3+ * Dynamic property storage handler for WooCommerce order objects.
4+ *
5+ * Provides a PHP 8.2+ compatible way to store dynamic properties on order objects
6+ * while maintaining backwards compatibility with PHP 7.4+.
47 *
58 * @package SkyVerge/WooCommerce/Payment-Gateway/Classes
9+ * @since x.x.x
610 */
711
812namespace SkyVerge \WooCommerce \PluginFramework \v5_15_12 \Payment_Gateway ;
913
1014/**
11- * Class for storing dynamic properties for order object .
15+ * Dynamic property storage handler for WooCommerce order objects .
1216 *
1317 * This class provides a way to store dynamic properties on order objects without using
1418 * dynamic properties (deprecated in PHP 8.2+) while maintaining backwards compatibility
15- * with PHP 7.4+.
19+ * with PHP 7.4+. It uses WeakMap for PHP 8.0+ and falls back to dynamic properties
20+ * for PHP 7.4+.
1621 *
1722 * @since x.x.x
1823 *
19- * Example usage:
24+ * @example
2025 * ```php
2126 * // Store properties
2227 * Dynamic_Props::set($order, 'customer_id', 123);
2530 * // Retrieve properties
2631 * $customer_id = Dynamic_Props::get($order, 'customer_id');
2732 * $total = Dynamic_Props::get($order, 'payment_total');
33+ * ```
2834 */
2935class Dynamic_Props {
3036 /**
31- * Storage for PHP 8.0+ using WeakMap.
37+ * Storage container for dynamic properties using WeakMap in PHP 8.0+.
38+ *
39+ * Uses WeakMap to store properties without memory leaks, as WeakMap allows garbage
40+ * collection of its keys when they're no longer referenced elsewhere.
3241 *
33- * @var \WeakMap<object, \stdClass>|null
42+ * @since x.x.x
43+ * @var \WeakMap<object, \stdClass>|null
3444 */
3545 private static ?\WeakMap $ map = null ;
3646
3747 /**
3848 * Sets a property on the order object.
3949 *
50+ * Stores a dynamic property either using WeakMap (PHP 8.0+) or direct property
51+ * access (PHP 7.4+). The storage method is automatically determined based on
52+ * PHP version and WeakMap availability.
53+ *
54+ * @since x.x.x
55+ *
56+ * @param \WC_Order $order The order object to store data on.
57+ * @param string $key The property key.
58+ * @param mixed $value The value to store.
59+ * @return void
60+ *
61+ * @example
4062 * ```php
4163 * Dynamic_Props::set($order, 'customer_id', 123);
4264 * Dynamic_Props::set($order, 'payment_total', '99.99');
4365 * ```
44- *
45- * @param \WC_Order $order The order object to store data on.
46- * @param string $key The property key.
47- * @param mixed $value The value to store.
48- *
49- * @return void
5066 */
5167 public static function set ( \WC_Order &$ order , string $ key , mixed $ value ): void {
5268 if ( self ::use_weak_map () ) {
@@ -63,18 +79,23 @@ public static function set( \WC_Order &$order, string $key, mixed $value ): void
6379 /**
6480 * Gets a property from the order object.
6581 *
82+ * Retrieves a stored dynamic property using the appropriate storage method
83+ * based on PHP version. Supports nested property access.
84+ *
85+ * @since x.x.x
86+ *
87+ * @param \WC_Order $order The order object to retrieve data from.
88+ * @param string $key The property key.
89+ * @param string $nested_key Optional. The nested property key. Default null.
90+ * @param mixed $default Optional. Default value if not found. Default null.
91+ * @return mixed The stored value or default if not found.
92+ *
93+ * @example
6694 * ```php
6795 * $customer_id = Dynamic_Props::get($order, 'customer_id');
6896 * $total = Dynamic_Props::get($order, 'payment_total');
6997 * $token = Dynamic_Props::get($order, 'payment', 'token', 'DEFAULT_TOKEN');
7098 * ```
71- *
72- * @param \WC_Order $order The order object to retrieve data from.
73- * @param string $key The property key.
74- * @param string $nested_key The nested property key.
75- * @param mixed $default Default value if not found.
76- *
77- * @return mixed The stored value or default if not found.
7899 */
79100 public static function get ( \WC_Order $ order , string $ key , $ nested_key = null , $ default = null ): mixed {
80101 if ( self ::use_weak_map () ) {
@@ -95,9 +116,13 @@ public static function get( \WC_Order $order, string $key, $nested_key = null, $
95116 /**
96117 * Unsets a property on the order object.
97118 *
98- * @param \WC_Order $order The order object to unset data from.
99- * @param string $key The property key.
119+ * Removes a stored dynamic property using the appropriate storage method
120+ * based on PHP version.
121+ *
122+ * @since x.x.x
100123 *
124+ * @param \WC_Order $order The order object to unset data from.
125+ * @param string $key The property key to remove.
101126 * @return void
102127 */
103128 public static function unset ( \WC_Order &$ order , string $ key ): void {
@@ -112,6 +137,10 @@ public static function unset( \WC_Order &$order, string $key ): void {
112137 /**
113138 * Checks if WeakMap should be used based on PHP version.
114139 *
140+ * Determines whether to use WeakMap storage based on PHP version (8.0+)
141+ * and WeakMap class availability. Result is cached for performance.
142+ *
143+ * @since x.x.x
115144 * @return bool True if WeakMap should be used, false otherwise.
116145 */
117146 private static function use_weak_map (): bool {
@@ -127,6 +156,10 @@ private static function use_weak_map(): bool {
127156 /**
128157 * Initializes WeakMap storage if not already initialized.
129158 *
159+ * Ensures the WeakMap storage is initialized only once when needed.
160+ * This lazy initialization helps with performance and memory usage.
161+ *
162+ * @since x.x.x
130163 * @return void
131164 */
132165 private static function init_weak_map (): void {
0 commit comments