Complex classes like SessionWare 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 SessionWare, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class SessionWare |
||
19 | { |
||
20 | const SESSION_LIFETIME_FLASH = 300; // 5 minutes |
||
21 | const SESSION_LIFETIME_SHORT = 600; // 10 minutes |
||
22 | const SESSION_LIFETIME_NORMAL = 900; // 15 minutes |
||
23 | const SESSION_LIFETIME_DEFAULT = 1440; // 24 minutes |
||
24 | const SESSION_LIFETIME_EXTENDED = 3600; // 1 hour |
||
25 | const SESSION_LIFETIME_INFINITE = PHP_INT_MAX; // Around 1145 years (x86_64) |
||
26 | |||
27 | const TIMEOUT_CONTROL_KEY = '__SESSIONWARE_TIMEOUT_TIMESTAMP__'; |
||
28 | |||
29 | /** |
||
30 | * Default session settings. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $defaultSettings = [ |
||
35 | 'name' => null, |
||
36 | 'savePath' => null, |
||
37 | 'lifetime' => self::SESSION_LIFETIME_DEFAULT, |
||
38 | 'timeoutKey' => self::TIMEOUT_CONTROL_KEY, |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $settings; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $initialSessionParams; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $sessionName; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $sessionLifetime; |
||
60 | |||
61 | /** |
||
62 | * Middleware constructor. |
||
63 | * |
||
64 | * @param array $settings |
||
65 | * @param array $initialSessionParams |
||
66 | */ |
||
67 | public function __construct(array $settings = [], array $initialSessionParams = []) |
||
77 | |||
78 | /** |
||
79 | * Retrieve default session parameters. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getSessionParams() |
||
97 | |||
98 | /** |
||
99 | * Execute the middleware. |
||
100 | * |
||
101 | * @param ServerRequestInterface $request |
||
102 | * @param ResponseInterface $response |
||
103 | * @param callable $next |
||
104 | * |
||
105 | * @throws \InvalidArgumentException |
||
106 | * @throws \RuntimeException |
||
107 | * |
||
108 | * @return ResponseInterface |
||
109 | */ |
||
110 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
118 | |||
119 | /** |
||
120 | * Configure session settings. |
||
121 | * |
||
122 | * @param array $requestCookies |
||
123 | * |
||
124 | * @throws \InvalidArgumentException |
||
125 | * @throws \RuntimeException |
||
126 | */ |
||
127 | protected function startSession(array $requestCookies = []) |
||
|
|||
128 | { |
||
129 | if (session_status() === PHP_SESSION_DISABLED) { |
||
130 | // @codeCoverageIgnoreStart |
||
131 | throw new \RuntimeException('PHP sessions are disabled'); |
||
132 | // @codeCoverageIgnoreEnd |
||
133 | } |
||
134 | |||
135 | if (session_status() === PHP_SESSION_ACTIVE) { |
||
136 | throw new \RuntimeException('Session has already been started, review "session.auto_start" ini set'); |
||
137 | } |
||
138 | |||
139 | $this->configureSessionName(); |
||
140 | $this->configureSessionId($requestCookies); |
||
141 | $this->configureSessionSavePath(); |
||
142 | $this->configureSessionTimeout(); |
||
143 | |||
144 | // Use better session serializer when available |
||
145 | if (ini_get('session.serialize_handler') === 'php' && version_compare(PHP_VERSION, '5.5.4', '>=')) { |
||
146 | // @codeCoverageIgnoreStart |
||
147 | ini_set('session.serialize_handler', 'php_serialize'); |
||
148 | // @codeCoverageIgnoreEnd |
||
149 | } |
||
150 | |||
151 | // Prevent headers from being automatically sent to client |
||
152 | ini_set('session.use_trans_sid', false); |
||
153 | ini_set('session.use_cookies', false); |
||
154 | ini_set('session.use_only_cookies', true); |
||
155 | ini_set('session.use_strict_mode', false); |
||
156 | ini_set('session.cache_limiter', ''); |
||
157 | |||
158 | session_start(); |
||
159 | |||
160 | // Populate session with initial parameters if they don't exist |
||
161 | foreach ($this->initialSessionParams as $parameter => $value) { |
||
162 | if (!array_key_exists($parameter, $_SESSION)) { |
||
163 | $_SESSION[$parameter] = $value; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | $this->manageSessionTimeout(); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Configure session name. |
||
172 | */ |
||
173 | protected function configureSessionName() |
||
179 | |||
180 | /** |
||
181 | * Configure session identifier. |
||
182 | * |
||
183 | * @param array $requestCookies |
||
184 | */ |
||
185 | protected function configureSessionId(array $requestCookies = []) |
||
197 | |||
198 | /** |
||
199 | * Configure session save path. |
||
200 | * |
||
201 | * @throws \RuntimeException |
||
202 | */ |
||
203 | protected function configureSessionSavePath() |
||
204 | { |
||
205 | if (ini_get('session.save_handler') !== 'files') { |
||
206 | // @codeCoverageIgnoreStart |
||
207 | return; |
||
208 | // @codeCoverageIgnoreEnd |
||
209 | } |
||
210 | |||
211 | $savePath = trim($this->settings['savePath']); |
||
212 | if ($savePath === '') { |
||
213 | $savePath = sys_get_temp_dir(); |
||
214 | if (session_save_path() !== '') { |
||
215 | $savePath = rtrim(session_save_path(), DIRECTORY_SEPARATOR); |
||
216 | } |
||
217 | |||
218 | $savePathParts = explode(DIRECTORY_SEPARATOR, $savePath); |
||
219 | if ($this->sessionName !== 'PHPSESSID' && $this->sessionName !== array_pop($savePathParts)) { |
||
220 | $savePath .= DIRECTORY_SEPARATOR . $this->sessionName; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | if (!@mkdir($savePath, 0775, true) && (!is_dir($savePath) || !is_writable($savePath))) { |
||
225 | throw new \RuntimeException( |
||
226 | sprintf('Failed to create session save path "%s", or directory is not writable', $savePath) |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | session_save_path($savePath); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Configure session timeout. |
||
235 | * |
||
236 | * @throws \InvalidArgumentException |
||
237 | */ |
||
238 | protected function configureSessionTimeout() |
||
251 | |||
252 | /** |
||
253 | * Manage session timeout. |
||
254 | * |
||
255 | * @throws \InvalidArgumentException |
||
256 | */ |
||
257 | protected function manageSessionTimeout() |
||
272 | |||
273 | /** |
||
274 | * Add session cookie Set-Cookie header to response. |
||
275 | * |
||
276 | * @param ResponseInterface $response |
||
277 | * |
||
278 | * @throws \InvalidArgumentException |
||
279 | * |
||
280 | * @return ResponseInterface |
||
281 | */ |
||
282 | protected function respondWithSessionCookie(ResponseInterface $response) |
||
318 | |||
319 | /** |
||
320 | * Retrieve session timeout control key. |
||
321 | * |
||
322 | * @throws \InvalidArgumentException |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | protected function getSessionTimeoutControlKey() |
||
337 | |||
338 | /** |
||
339 | * Regenerate session id with cryptographically secure session identifier |
||
340 | */ |
||
341 | final public static function regenerateSessionId() |
||
345 | |||
346 | /** |
||
347 | * Generates cryptographically secure session identifier. |
||
348 | * |
||
349 | * @param int $length |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | final protected static function generateSessionId($length = 80) |
||
361 | } |
||
362 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: