Complex classes like 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 Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Session |
||
9 | { |
||
10 | use Sapi\OptionalTrait; |
||
11 | |||
12 | private $sessionState; |
||
13 | private $name; |
||
14 | private $id; |
||
15 | private static $instance; |
||
16 | |||
17 | 1 | private function __construct() |
|
20 | |||
21 | /** |
||
22 | * @codeCoverageIgnore |
||
23 | */ |
||
24 | private function __clone() |
||
27 | |||
28 | /** |
||
29 | * @param bool $deleteOldSession |
||
30 | * @return bool |
||
31 | */ |
||
32 | 2 | public function regenerate($deleteOldSession = false) |
|
33 | { |
||
34 | 2 | $success = false; |
|
35 | 2 | if ($this->isStarted()) { |
|
36 | 1 | $success = $this->sessionRegenerateId((bool)$deleteOldSession); |
|
37 | } |
||
38 | 2 | return $success; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param bool|false $deleteOldSession |
||
43 | * @return bool |
||
44 | * @codeCoverageIgnore |
||
45 | */ |
||
46 | protected function sessionRegenerateId($deleteOldSession = false) |
||
50 | |||
51 | /** |
||
52 | * Retrieve session system - start session if not started |
||
53 | * @return Session |
||
54 | */ |
||
55 | 6 | final public static function getInstance() |
|
56 | { |
||
57 | 6 | if (!isset(self::$instance)) { |
|
58 | 1 | $class = get_called_class(); |
|
59 | 1 | self::$instance = new $class; |
|
60 | } |
||
61 | 6 | return self::$instance; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Define session name |
||
66 | * @param string $name |
||
67 | * @throws \FMUP\Exception if session name defined contain only numbers |
||
68 | * @return $this |
||
69 | */ |
||
70 | 4 | public function setName($name) |
|
71 | { |
||
72 | 4 | if (!$this->isStarted()) { |
|
73 | 3 | if (is_numeric($name)) { |
|
74 | 1 | throw new Exception('Session name could not contain only numbers'); |
|
75 | } |
||
76 | 2 | $this->name = (string)$name; |
|
77 | } |
||
78 | 3 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Retrieve session name |
||
83 | * @return string|null |
||
84 | */ |
||
85 | 5 | public function getName() |
|
86 | { |
||
87 | 5 | if ($this->isStarted() && is_null($this->name)) { |
|
88 | 1 | $this->name = $this->sessionName(); |
|
89 | } |
||
90 | 5 | return $this->name; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | * @return string |
||
96 | * @codeCoverageIgnore |
||
97 | */ |
||
98 | protected function sessionName($name = null) |
||
102 | |||
103 | /** |
||
104 | * Define session id |
||
105 | * @param string $id |
||
106 | * @return $this |
||
107 | * @throws Exception |
||
108 | */ |
||
109 | 4 | public function setId($id) |
|
110 | { |
||
111 | 4 | if (!$this->isStarted()) { |
|
112 | 3 | if (!preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $id)) { |
|
113 | 1 | throw new Exception('Session name is not valid'); |
|
114 | } |
||
115 | 2 | $this->id = (string)$id; |
|
116 | } |
||
117 | 3 | return $this; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param string|null $sessionId |
||
122 | * @return string |
||
123 | * @codeCoverageIgnore |
||
124 | */ |
||
125 | protected function sessionId($sessionId = null) |
||
129 | |||
130 | /** |
||
131 | * Retrieve session name |
||
132 | * @return string|null |
||
133 | */ |
||
134 | 5 | public function getId() |
|
135 | { |
||
136 | 5 | if ($this->isStarted() && is_null($this->id)) { |
|
137 | 1 | $this->id = $this->sessionId(); |
|
138 | } |
||
139 | 5 | return $this->id; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Check whether session is started |
||
144 | * @return bool |
||
145 | */ |
||
146 | 8 | public function isStarted() |
|
147 | { |
||
148 | 8 | if (is_null($this->sessionState)) { |
|
149 | 8 | $this->sessionState = version_compare($this->phpVersion(), '5.4.0', '>=') |
|
150 | 6 | ? $this->sessionStatus() === PHP_SESSION_ACTIVE |
|
151 | 2 | : $this->sessionId() !== ''; |
|
152 | } |
||
153 | 8 | return $this->sessionState; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | * @codeCoverageIgnore |
||
159 | */ |
||
160 | protected function phpVersion() |
||
164 | |||
165 | /** |
||
166 | * @return int |
||
167 | * @codeCoverageIgnore |
||
168 | */ |
||
169 | protected function sessionStatus() |
||
173 | |||
174 | /** |
||
175 | * Start session if not started and return if session is started |
||
176 | * @return bool |
||
177 | */ |
||
178 | 4 | public function start() |
|
179 | { |
||
180 | 4 | if (!$this->isStarted() && $this->getSapi()->get() != Sapi::CLI) { |
|
181 | 3 | if ($this->getId()) { |
|
|
|||
182 | 1 | $this->sessionId($this->getId()); |
|
183 | } |
||
184 | 3 | if ($this->getName()) { |
|
185 | 1 | $this->sessionName($this->getName()); |
|
186 | } |
||
187 | 3 | $this->sessionState = $this->sessionStart(); |
|
188 | } |
||
189 | |||
190 | 4 | return (bool)$this->sessionState; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | * @codeCoverageIgnore |
||
196 | */ |
||
197 | protected function sessionStart() |
||
201 | |||
202 | /** |
||
203 | * Retrieve all session values defined |
||
204 | * @return array |
||
205 | */ |
||
206 | 2 | public function getAll() |
|
210 | |||
211 | /** |
||
212 | * Define all session values |
||
213 | * @param array $values |
||
214 | * @return $this |
||
215 | */ |
||
216 | 2 | public function setAll(array $values = array()) |
|
217 | { |
||
218 | 2 | if ($this->start()) { |
|
219 | 1 | $_SESSION = $values; |
|
220 | } |
||
221 | 2 | return $this; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Retrieve a session value |
||
226 | * @param string $name |
||
227 | * @return mixed |
||
228 | * @codeCoverageIgnore |
||
229 | */ |
||
230 | public function get($name) |
||
235 | |||
236 | /** |
||
237 | * Check whether a specific information exists in session |
||
238 | * @param string $name |
||
239 | * @return bool |
||
240 | * @codeCoverageIgnore |
||
241 | */ |
||
242 | public function has($name) |
||
247 | |||
248 | /** |
||
249 | * Define a specific value in session |
||
250 | * @param string $name |
||
251 | * @param mixed $value |
||
252 | * @return $this |
||
253 | * @codeCoverageIgnore |
||
254 | */ |
||
255 | public function set($name, $value) |
||
263 | |||
264 | /** |
||
265 | * Forget all values in session without destructing it |
||
266 | * @return $this |
||
267 | * @codeCoverageIgnore |
||
268 | */ |
||
269 | public function clear() |
||
276 | |||
277 | /** |
||
278 | * Delete a specific information from session |
||
279 | * @param string $name |
||
280 | * @return $this |
||
281 | * @codeCoverageIgnore |
||
282 | */ |
||
283 | public function remove($name) |
||
290 | |||
291 | /** |
||
292 | * Destroy current session |
||
293 | * @return bool success or failure on session destruction |
||
294 | * @codeCoverageIgnore |
||
295 | */ |
||
296 | public function destroy() |
||
307 | } |
||
308 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: