Complex classes like Give_Session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Give_Session { |
||
25 | |||
26 | /** |
||
27 | * Holds our session data |
||
28 | * |
||
29 | * @since 1.0 |
||
30 | * @access private |
||
31 | * |
||
32 | * @var WP_Session/array |
||
33 | */ |
||
34 | private $session; |
||
35 | |||
36 | /** |
||
37 | * Whether to use PHP $_SESSION or WP_Session |
||
38 | * |
||
39 | * @since 1.0 |
||
40 | * @access private |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $use_php_sessions = false; |
||
45 | |||
46 | /** |
||
47 | * Expiration Time |
||
48 | * |
||
49 | * @since 1.0 |
||
50 | * @access private |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | private $exp_option = false; |
||
55 | |||
56 | /** |
||
57 | * Session index prefix |
||
58 | * |
||
59 | * @since 1.0 |
||
60 | * @access private |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $prefix = ''; |
||
65 | |||
66 | /** |
||
67 | * Class Constructor |
||
68 | * |
||
69 | * Defines our session constants, includes the necessary libraries and retrieves the session instance. |
||
70 | 2 | * |
|
71 | * @since 1.0 |
||
72 | 2 | * @access public |
|
73 | 2 | */ |
|
74 | public function __construct() { |
||
130 | |||
131 | /** |
||
132 | * Session Init |
||
133 | * |
||
134 | * Setup the Session instance. |
||
135 | * |
||
136 | * @since 1.0 |
||
137 | * @access public |
||
138 | * |
||
139 | * @return array Session instance |
||
140 | */ |
||
141 | public function init() { |
||
152 | |||
153 | /** |
||
154 | * Get Session ID |
||
155 | * |
||
156 | * Retrieve session ID. |
||
157 | * |
||
158 | * @since 1.0 |
||
159 | * @access public |
||
160 | * |
||
161 | * @return string Session ID. |
||
162 | */ |
||
163 | public function get_id() { |
||
166 | 12 | ||
167 | /** |
||
168 | 12 | * Get Session |
|
169 | * |
||
170 | * Retrieve session variable for a given session key. |
||
171 | * |
||
172 | * @since 1.0 |
||
173 | * @access public |
||
174 | * |
||
175 | * @param string $key Session key. |
||
176 | * |
||
177 | * @return string|array Session variable. |
||
178 | */ |
||
179 | public function get( $key ) { |
||
218 | |||
219 | /** |
||
220 | * Set Session |
||
221 | * |
||
222 | * Create a new session. |
||
223 | * |
||
224 | * @since 1.0 |
||
225 | * @access public |
||
226 | * |
||
227 | * @param string $key Session key. |
||
228 | * @param string $value Session variable. |
||
229 | * |
||
230 | * @return string Session variable. |
||
231 | */ |
||
232 | public function set( $key, $value ) { |
||
248 | |||
249 | /** |
||
250 | * Set Session Cookies |
||
251 | * |
||
252 | * Cookies are used to increase the session lifetime using the give setting. This is helpful for when a user closes their browser after making a donation and comes back to the |
||
253 | * site. |
||
254 | * |
||
255 | * @since 1.4 |
||
256 | * @access public |
||
257 | 4 | * |
|
258 | * @hook |
||
259 | 4 | */ |
|
260 | public function set_session_cookies() { |
||
267 | |||
268 | /** |
||
269 | * Set Cookie Variant Time |
||
270 | * |
||
271 | * Force the cookie expiration variant time to custom expiration option, less and hour. defaults to 23 hours (set_expiration_variant_time used in WP_Session). |
||
272 | * |
||
273 | * @since 1.0 |
||
274 | * @access public |
||
275 | * |
||
276 | * @return int |
||
277 | */ |
||
278 | public function set_expiration_variant_time() { |
||
282 | |||
283 | /** |
||
284 | 4 | * Set Cookie Expiration |
|
285 | * |
||
286 | * Force the cookie expiration time if set, default to 24 hours. |
||
287 | * |
||
288 | 4 | * @since 1.0 |
|
289 | * @access public |
||
290 | 4 | * |
|
291 | 4 | * @return int |
|
292 | 4 | */ |
|
293 | public function set_expiration_time() { |
||
297 | |||
298 | /** |
||
299 | * Starts a new session if one has not started yet. |
||
300 | * |
||
301 | * Checks to see if the server supports PHP sessions or if the GIVE_USE_PHP_SESSIONS constant is defined. |
||
302 | * |
||
303 | 2 | * @since 1.0 |
|
304 | * @access public |
||
305 | 2 | * |
|
306 | * @return bool $ret True if we are using PHP sessions, false otherwise. |
||
307 | 2 | */ |
|
308 | public function use_php_sessions() { |
||
345 | |||
346 | /** |
||
347 | * Should Start Session |
||
348 | * |
||
349 | * Determines if we should start sessions. |
||
350 | * |
||
351 | * @since 1.4 |
||
352 | * @access public |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function should_start_session() { |
||
386 | |||
387 | /** |
||
388 | * Maybe Start Session |
||
389 | * |
||
390 | * Starts a new session if one hasn't started yet. |
||
391 | * |
||
392 | * @access public |
||
393 | * |
||
394 | * @see http://php.net/manual/en/function.session-set-cookie-params.php |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | public function maybe_start_session() { |
||
409 | |||
410 | /** |
||
411 | * Get Session Expiration |
||
412 | * |
||
413 | * Looks at the session cookies and returns the expiration date for this session if applicable |
||
414 | * |
||
415 | * @access public |
||
416 | * |
||
417 | * @return string Formatted expiration date string. |
||
418 | */ |
||
419 | public function get_session_expiration() { |
||
432 | |||
433 | } |
||
434 |