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 elFinderSession 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 elFinderSession, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class elFinderSession implements elFinderSessionInterface |
||
|
|
|||
| 10 | { |
||
| 11 | protected $started = false; |
||
| 12 | |||
| 13 | protected $keys = []; |
||
| 14 | |||
| 15 | protected $base64encode = false; |
||
| 16 | |||
| 17 | protected $opts = [ |
||
| 18 | 'base64encode' => false, |
||
| 19 | 'keys' => [ |
||
| 20 | 'default' => 'elFinderCaches', |
||
| 21 | 'netvolume' => 'elFinderNetVolumes', |
||
| 22 | ], |
||
| 23 | ]; |
||
| 24 | |||
| 25 | public function __construct($opts) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | public function start() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function close() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function get($key, $empty = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function set($key, $data) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function remove($key) |
||
| 174 | |||
| 175 | protected function &getSessionRef($key) |
||
| 210 | |||
| 211 | protected function encodeData($data) |
||
| 219 | |||
| 220 | protected function decodeData($data) |
||
| 236 | |||
| 237 | protected function session_start_error($errno, $errstr) |
||
| 240 | } |
||
| 241 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.