Complex classes like Geodir_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 Geodir_Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Geodir_Session { |
||
11 | /** |
||
12 | * Holds our session data. |
||
13 | * |
||
14 | * @var array |
||
15 | * @access private |
||
16 | * @since 1.5.7 |
||
17 | */ |
||
18 | private $session; |
||
19 | |||
20 | /** |
||
21 | * Whether to use PHP $_SESSION or WP_Session. |
||
22 | * |
||
23 | * @var bool |
||
24 | * @access private |
||
25 | * @since 1.5.7 |
||
26 | */ |
||
27 | private $use_php_sessions = false; |
||
28 | |||
29 | /** |
||
30 | * Session index prefix. |
||
31 | * |
||
32 | * @var string |
||
33 | * @access private |
||
34 | * @since 1.5.7 |
||
35 | */ |
||
36 | private $prefix = ''; |
||
37 | |||
38 | /** |
||
39 | * Get things started. |
||
40 | * |
||
41 | * Defines our WP_Session constants, includes the necessary libraries and |
||
42 | * retrieves the WP Session instance. |
||
43 | * |
||
44 | * @since 1.5.7 |
||
45 | */ |
||
46 | public function __construct() { |
||
81 | |||
82 | /** |
||
83 | * Setup the WP_Session instance. |
||
84 | * |
||
85 | * @access public |
||
86 | * @since 1.5.7 |
||
87 | * @return void |
||
88 | */ |
||
89 | public function init() { |
||
98 | |||
99 | /** |
||
100 | * Retrieve session ID. |
||
101 | * |
||
102 | * @access public |
||
103 | * @since 1.5.7 |
||
104 | * @return string Session ID |
||
105 | */ |
||
106 | public function get_id() { |
||
109 | |||
110 | /** |
||
111 | * Retrieve a session variable. |
||
112 | * |
||
113 | * @access public |
||
114 | * @since 1.5.7 |
||
115 | * @param string $key Session key |
||
116 | * @return string Session variable |
||
117 | */ |
||
118 | 26 | public function get( $key ) { |
|
122 | |||
123 | /** |
||
124 | * Set a session variable |
||
125 | * |
||
126 | * @since 1.5.7 |
||
127 | * |
||
128 | * @param string $key Session key |
||
129 | * @param integer $value Session variable |
||
130 | * @return string Session variable |
||
131 | */ |
||
132 | 3 | public function set( $key, $value ) { |
|
133 | 3 | $key = sanitize_key( $key ); |
|
134 | |||
135 | 3 | if ( is_array( $value ) ) { |
|
136 | 1 | $this->session[ $key ] = maybe_serialize( $value ); |
|
137 | 1 | } else { |
|
138 | 2 | $this->session[ $key ] = $value; |
|
139 | } |
||
140 | |||
141 | 3 | if ( $this->use_php_sessions ) { |
|
142 | $_SESSION['gd' . $this->prefix ] = $this->session; |
||
143 | } |
||
144 | |||
145 | 3 | return $this->session[ $key ]; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Unset a session variable. |
||
150 | * |
||
151 | * @since 1.5.7 |
||
152 | * |
||
153 | * @param string|array $key Session key. |
||
154 | * @param integer $value Session variable. |
||
155 | * @return string Session variable. |
||
156 | */ |
||
157 | 10 | public function un_set( $key ) { |
|
184 | |||
185 | /** |
||
186 | * Check a session variable is set or not. |
||
187 | * |
||
188 | * @since 1.5.7 |
||
189 | * |
||
190 | * @param string $key Session key. |
||
191 | * @param integer $value Session variable. |
||
192 | * @return string Session variable. |
||
193 | */ |
||
194 | public function is_set( $key ) { |
||
207 | |||
208 | /** |
||
209 | * Force the cookie expiration variant time to 23 hours |
||
210 | * |
||
211 | * @access public |
||
212 | * @since 1.5.7 |
||
213 | * @param int $exp Default expiration (1 hour) |
||
214 | * @return int |
||
215 | */ |
||
216 | public function set_expiration_variant_time( $exp ) { |
||
219 | |||
220 | /** |
||
221 | * Force the cookie expiration time to 24 hours |
||
222 | * |
||
223 | * @access public |
||
224 | * @since 1.5.7 |
||
225 | * @param int $exp Default expiration (1 hour) |
||
226 | * @return int |
||
227 | */ |
||
228 | public function set_expiration_time( $exp ) { |
||
231 | |||
232 | /** |
||
233 | * Starts a new session if one hasn't started yet. |
||
234 | * |
||
235 | * @return boolean |
||
236 | * Checks to see if the server supports PHP sessions |
||
237 | * or if the GEODIR_USE_PHP_SESSIONS constant is defined |
||
238 | * |
||
239 | * @access public |
||
240 | * @since 1.5.7 |
||
241 | * @return boolean $ret True if we are using PHP sessions, false otherwise |
||
242 | */ |
||
243 | public function use_php_sessions() { |
||
274 | |||
275 | /** |
||
276 | * Starts a new session if one hasn't started yet. |
||
277 | */ |
||
278 | public function maybe_start_session() { |
||
283 | |||
284 | /** |
||
285 | * Determines if a user has set the GEODIR_USE_CART_COOKIE |
||
286 | * |
||
287 | * @since 1.5.7 |
||
288 | * @return bool If the store should use the geodir_items_in_cart cookie to help avoid caching |
||
289 | */ |
||
290 | public function use_cart_cookie() { |
||
299 | } |
||
300 | |||
303 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.