Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
22 | class Give_Cache_Setting { |
||
|
|||
23 | /** |
||
24 | * Instance. |
||
25 | * |
||
26 | * @since 2.4.0 |
||
27 | * @access private |
||
28 | * @var Give_Cache_Setting |
||
29 | */ |
||
30 | static private $instance; |
||
31 | |||
32 | /** |
||
33 | * Cache key. |
||
34 | * |
||
35 | * @since 2.4.0 |
||
36 | * @access private |
||
37 | * @var string |
||
38 | */ |
||
39 | private $cache_key = 'giveAllOptions'; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Cache group. |
||
44 | * |
||
45 | * @since 2.4.0 |
||
46 | * @access private |
||
47 | * @var string |
||
48 | */ |
||
49 | private $cache_group = 'give-options'; |
||
50 | |||
51 | /** |
||
52 | * Array of cached settings |
||
53 | * |
||
54 | * @since 2.4.0 |
||
55 | * @access private |
||
56 | * @var array |
||
57 | */ |
||
58 | private $settings = array( |
||
59 | 'give_settings' => array(), |
||
60 | 'give_version' => '', |
||
61 | 'give_completed_upgrades' => array(), |
||
62 | 'currencies' => array(), |
||
63 | 'gateways' => array(), |
||
64 | ); |
||
65 | |||
66 | /** |
||
67 | * Array of cached setting db option names |
||
68 | * |
||
69 | * @since 2.4.0 |
||
70 | * @access private |
||
71 | * @var array |
||
72 | */ |
||
73 | private $db_option_ids = array( |
||
74 | 'give_settings', |
||
75 | 'give_version', |
||
76 | 'give_completed_upgrades', |
||
77 | ); |
||
78 | |||
79 | /** |
||
80 | * Array of cached setting option names |
||
81 | * |
||
82 | * @since 2.4.0 |
||
83 | * @access private |
||
84 | * @var array |
||
85 | */ |
||
86 | static private $all_option_ids; |
||
87 | |||
88 | /** |
||
89 | * Singleton pattern. |
||
90 | * |
||
91 | * @since 2.4.0 |
||
92 | * @access private |
||
93 | */ |
||
94 | private function __construct() { |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Get instance. |
||
100 | * |
||
101 | * @since 2.4.0 |
||
102 | * @access public |
||
103 | * @return Give_Cache_Setting |
||
104 | */ |
||
105 | View Code Duplication | public static function get_instance() { |
|
114 | |||
115 | /** |
||
116 | * Setup |
||
117 | * |
||
118 | * @since 2.4.0 |
||
119 | * @access private |
||
120 | */ |
||
121 | private function setup() { |
||
133 | |||
134 | /** |
||
135 | * Load plugin settings |
||
136 | * |
||
137 | * @since 2.4.0 |
||
138 | * @access private |
||
139 | */ |
||
140 | private function load_plugin_settings() { |
||
179 | |||
180 | /** |
||
181 | * Reload option when add, update or delete |
||
182 | * |
||
183 | * Note: only for internal logic |
||
184 | * |
||
185 | * @since 2.4.0 |
||
186 | * |
||
187 | * @param $option_name |
||
188 | */ |
||
189 | public function __reload_plugin_settings( $option_name ) { |
||
198 | |||
199 | /** |
||
200 | * Setup currencies list |
||
201 | * |
||
202 | * @since 2.4.0 |
||
203 | */ |
||
204 | public function __setup_currencies_list() { |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Setup gateway list |
||
220 | * |
||
221 | * @since 2.4.0 |
||
222 | */ |
||
223 | public function __setup_gateways_list() { |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Get option |
||
253 | * |
||
254 | * @since 2.4.0 |
||
255 | * @access public |
||
256 | * |
||
257 | * @param $option_name |
||
258 | * @param bool $default |
||
259 | * |
||
260 | * @return mixed |
||
261 | */ |
||
262 | public static function get_option( $option_name, $default = false ) { |
||
273 | |||
274 | /** |
||
275 | * Get plugin settings |
||
276 | * |
||
277 | * @since 2.4.0 |
||
278 | * @access public |
||
279 | */ |
||
280 | public static function get_settings() { |
||
287 | } |
||
288 | |||
290 |