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 | * @var array |
||
30 | * @access private |
||
31 | * @since 1.0 |
||
32 | */ |
||
33 | private $session; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Whether to use PHP $_SESSION or WP_Session |
||
38 | * |
||
39 | * @var bool |
||
40 | * @access private |
||
41 | * @since 1.0 |
||
42 | */ |
||
43 | private $use_php_sessions = false; |
||
44 | |||
45 | /** |
||
46 | * Expiration Time |
||
47 | * |
||
48 | * @var int |
||
49 | * @access private |
||
50 | * @since 1.0 |
||
51 | */ |
||
52 | private $exp_option = false; |
||
53 | |||
54 | /** |
||
55 | * Session index prefix |
||
56 | * |
||
57 | * @var string |
||
58 | * @access private |
||
59 | * @since 1.0 |
||
60 | */ |
||
61 | private $prefix = ''; |
||
62 | |||
63 | /** |
||
64 | * Get things started |
||
65 | * |
||
66 | * @description: Defines our session constants, includes the necessary libraries and retrieves the session instance |
||
67 | * |
||
68 | * @since 1.0 |
||
69 | */ |
||
70 | public function __construct() { |
||
122 | |||
123 | /** |
||
124 | * Setup the Session instance |
||
125 | * |
||
126 | * @access public |
||
127 | * @since 1.0 |
||
128 | * @return array $this->session |
||
129 | */ |
||
130 | public function init() { |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Retrieve session ID |
||
145 | * |
||
146 | * @access public |
||
147 | * @since 1.0 |
||
148 | * @return string Session ID |
||
149 | */ |
||
150 | public function get_id() { |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Retrieve a session variable |
||
157 | * |
||
158 | * @access public |
||
159 | * @since 1.0 |
||
160 | * |
||
161 | 12 | * @param string $key Session key |
|
162 | 12 | * |
|
163 | * @return string Session variable |
||
164 | 12 | */ |
|
165 | public function get( $key ) { |
||
171 | |||
172 | /** |
||
173 | * Set a session variable |
||
174 | * |
||
175 | * @since 1.0 |
||
176 | * |
||
177 | * @param $key $_SESSION key |
||
178 | 11 | * @param $value $_SESSION variable |
|
179 | * |
||
180 | 11 | * @return mixed Session variable |
|
181 | */ |
||
182 | 11 | public function set( $key, $value ) { |
|
198 | |||
199 | /** |
||
200 | * Set Session Cookies |
||
201 | * |
||
202 | * @description: 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 site. |
||
203 | * |
||
204 | * @access public |
||
205 | * @hook |
||
206 | * @since 1.4 |
||
207 | */ |
||
208 | public function set_session_cookies() { |
||
215 | |||
216 | /** |
||
217 | * Set Cookie Variant Time |
||
218 | * |
||
219 | * @description 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) |
||
220 | * |
||
221 | * @access public |
||
222 | * @since 1.0 |
||
223 | * |
||
224 | * @return int |
||
225 | */ |
||
226 | public function set_expiration_variant_time() { |
||
230 | |||
231 | /** |
||
232 | * Set the Cookie Expiration |
||
233 | * |
||
234 | * @description Force the cookie expiration time if set, default to 24 hours |
||
235 | 2 | * |
|
236 | * @access public |
||
237 | 2 | * @since 1.0 |
|
238 | * |
||
239 | * @return int |
||
240 | 2 | */ |
|
241 | public function set_expiration_time() { |
||
245 | |||
246 | /** |
||
247 | * Starts a new session if one has not started yet. |
||
248 | * |
||
249 | * @return null |
||
250 | * Checks to see if the server supports PHP sessions or if the GIVE_USE_PHP_SESSIONS constant is defined |
||
251 | * |
||
252 | * @access public |
||
253 | * @since 1.0 |
||
254 | * @return bool $ret True if we are using PHP sessions, false otherwise |
||
255 | */ |
||
256 | public function use_php_sessions() { |
||
295 | |||
296 | /** |
||
297 | * Determines if we should start sessions |
||
298 | * |
||
299 | * @since 1.4 |
||
300 | * @return bool |
||
301 | */ |
||
302 | public function should_start_session() { |
||
332 | |||
333 | /** |
||
334 | * Maybe Start Session |
||
335 | * |
||
336 | * @description Starts a new session if one hasn't started yet. |
||
337 | * @see http://php.net/manual/en/function.session-set-cookie-params.php |
||
338 | */ |
||
339 | public function maybe_start_session() { |
||
350 | |||
351 | |||
352 | /** |
||
353 | * Get Session Expiration |
||
354 | * |
||
355 | * @description Looks at the session cookies and returns the expiration date for this session if applicable |
||
356 | */ |
||
357 | public function get_session_expiration() { |
||
370 | |||
371 | } |
||
372 | |||
373 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.