Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PHPSessionHandler 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 PHPSessionHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class PHPSessionHandler implements \SessionHandlerInterface { |
||
35 | /** @var PHPSessionHandler */ |
||
36 | protected static $instance = null; |
||
37 | |||
38 | /** @var bool Whether PHP session handling is enabled */ |
||
39 | protected $enable = false; |
||
40 | protected $warn = true; |
||
41 | |||
42 | /** @var SessionManager|null */ |
||
43 | protected $manager; |
||
44 | |||
45 | /** @var BagOStuff|null */ |
||
46 | protected $store; |
||
47 | |||
48 | /** @var LoggerInterface */ |
||
49 | protected $logger; |
||
50 | |||
51 | /** @var array Track original session fields for later modification check */ |
||
52 | protected $sessionFieldCache = []; |
||
53 | |||
54 | protected function __construct( SessionManager $manager ) { |
||
60 | |||
61 | /** |
||
62 | * Set $this->enable and $this->warn |
||
63 | * |
||
64 | * Separate just because there doesn't seem to be a good way to test it |
||
65 | * otherwise. |
||
66 | * |
||
67 | * @param string $PHPSessionHandling See $wgPHPSessionHandling |
||
68 | */ |
||
69 | private function setEnableFlags( $PHPSessionHandling ) { |
||
87 | |||
88 | /** |
||
89 | * Test whether the handler is installed |
||
90 | * @return bool |
||
91 | */ |
||
92 | public static function isInstalled() { |
||
95 | |||
96 | /** |
||
97 | * Test whether the handler is installed and enabled |
||
98 | * @return bool |
||
99 | */ |
||
100 | public static function isEnabled() { |
||
103 | |||
104 | /** |
||
105 | * Install a session handler for the current web request |
||
106 | * @param SessionManager $manager |
||
107 | */ |
||
108 | public static function install( SessionManager $manager ) { |
||
142 | |||
143 | /** |
||
144 | * Set the manager, store, and logger |
||
145 | * @private Use self::install(). |
||
146 | * @param SessionManager $manager |
||
147 | * @param BagOStuff $store |
||
148 | * @param LoggerInterface $store |
||
149 | */ |
||
150 | public function setManager( |
||
164 | |||
165 | /** |
||
166 | * Workaround for PHP5 bug |
||
167 | * |
||
168 | * PHP5 has a bug in handling boolean return values for |
||
169 | * SessionHandlerInterface methods, it expects 0 or -1 instead of true or |
||
170 | * false. See <https://wiki.php.net/rfc/session.user.return-value>. |
||
171 | * |
||
172 | * PHP7 and HHVM are not affected. |
||
173 | * |
||
174 | * @todo When we drop support for Zend PHP 5, this can be removed. |
||
175 | * @return bool|int |
||
176 | * @codeCoverageIgnore |
||
177 | */ |
||
178 | protected static function returnSuccess() { |
||
181 | |||
182 | /** |
||
183 | * Workaround for PHP5 bug |
||
184 | * @see self::returnSuccess() |
||
185 | * @return bool|int |
||
186 | * @codeCoverageIgnore |
||
187 | */ |
||
188 | protected static function returnFailure() { |
||
191 | |||
192 | /** |
||
193 | * Initialize the session (handler) |
||
194 | * @private For internal use only |
||
195 | * @param string $save_path Path used to store session files (ignored) |
||
196 | * @param string $session_name Session name (ignored) |
||
197 | * @return bool|int Success (see self::returnSuccess()) |
||
198 | */ |
||
199 | public function open( $save_path, $session_name ) { |
||
208 | |||
209 | /** |
||
210 | * Close the session (handler) |
||
211 | * @private For internal use only |
||
212 | * @return bool|int Success (see self::returnSuccess()) |
||
213 | */ |
||
214 | public function close() { |
||
221 | |||
222 | /** |
||
223 | * Read session data |
||
224 | * @private For internal use only |
||
225 | * @param string $id Session id |
||
226 | * @return string Session data |
||
227 | */ |
||
228 | public function read( $id ) { |
||
246 | |||
247 | /** |
||
248 | * Write session data |
||
249 | * @private For internal use only |
||
250 | * @param string $id Session id |
||
251 | * @param string $dataStr Session data. Not that you should ever call this |
||
252 | * directly, but note that this has the same issues with code injection |
||
253 | * via user-controlled data as does PHP's unserialize function. |
||
254 | * @return bool|int Success (see self::returnSuccess()) |
||
255 | */ |
||
256 | public function write( $id, $dataStr ) { |
||
355 | |||
356 | /** |
||
357 | * Destroy a session |
||
358 | * @private For internal use only |
||
359 | * @param string $id Session id |
||
360 | * @return bool|int Success (see self::returnSuccess()) |
||
361 | */ |
||
362 | public function destroy( $id ) { |
||
375 | |||
376 | /** |
||
377 | * Execute garbage collection. |
||
378 | * @private For internal use only |
||
379 | * @param int $maxlifetime Maximum session life time (ignored) |
||
380 | * @return bool|int Success (see self::returnSuccess()) |
||
381 | * @codeCoverageIgnore See T135576 |
||
382 | */ |
||
383 | public function gc( $maxlifetime ) { |
||
391 | } |
||
392 |
This check looks for the bodies of
elseif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseif
bodies can be removed. If you have an empty elseif but statements in theelse
branch, consider inverting the condition.