elFinderSession   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 232
Duplicated Lines 12.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 30
loc 232
rs 7.9487
c 0
b 0
f 0
wmc 52
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A start() 0 15 4
A close() 0 9 2
D get() 0 43 15
A set() 0 19 4
D remove() 12 35 9
D getSessionRef() 18 35 10
A encodeData() 0 8 2
A decodeData() 0 16 4
A session_start_error() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
3
/**
4
 * elFinder - file manager for web.
5
 * Session Wrapper Class.
6
 *
7
 * @author Naoki Sawada
8
 **/
9
class elFinderSession implements elFinderSessionInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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)
26
    {
27
        $this->opts = array_merge($this->opts, $opts);
28
        $this->base64encode = ! empty($this->opts['base64encode']);
29
        $this->keys = $this->opts['keys'];
30
31
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function start()
38
    {
39
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
40
            if (session_status() !== PHP_SESSION_ACTIVE) {
41
                session_start();
42
            }
43
        } else {
44
            set_error_handler([$this, 'session_start_error'], E_NOTICE);
45
            session_start();
46
            restore_error_handler();
47
        }
48
        $this->started = session_id() ? true : false;
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function close()
57
    {
58
        if ($this->started) {
59
            session_write_close();
60
        }
61
        $this->started = false;
62
63
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function get($key, $empty = null)
70
    {
71
        $closed = false;
72
        if (! $this->started) {
73
            $closed = true;
74
            $this->start();
75
        }
76
77
        $data = null;
78
79
        if ($this->started) {
80
            $session = &$this->getSessionRef($key);
81
            $data = $session;
82
            if ($data && $this->base64encode) {
83
                $data = $this->decodeData($data);
84
            }
85
        }
86
87
        $checkFn = null;
88
        if (! is_null($empty)) {
89
            if (is_string($empty)) {
90
                $checkFn = 'is_string';
91
            } elseif (is_array($empty)) {
92
                $checkFn = 'is_array';
93
            } elseif (is_object($empty)) {
94
                $checkFn = 'is_object';
95
            } elseif (is_float($empty)) {
96
                $checkFn = 'is_float';
97
            } elseif (is_int($empty)) {
98
                $checkFn = 'is_int';
99
            }
100
        }
101
102
        if (is_null($data) || ($checkFn && ! $checkFn($data))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $checkFn of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103
            $session = $data = $empty;
104
        }
105
106
        if ($closed) {
107
            $this->close();
108
        }
109
110
        return $data;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function set($key, $data)
117
    {
118
        $closed = false;
119
        if (! $this->started) {
120
            $closed = true;
121
            $this->start();
122
        }
123
        $session = &$this->getSessionRef($key);
124
        if ($this->base64encode) {
125
            $data = $this->encodeData($data);
126
        }
127
        $session = $data;
128
129
        if ($closed) {
130
            $this->close();
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function remove($key)
0 ignored issues
show
Coding Style introduced by
remove uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
140
    {
141
        $closed = false;
142
        if (! $this->started) {
143
            $closed = true;
144
            $this->start();
145
        }
146
147
        list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
148 View Code Duplication
        if (is_null($name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
            if (! isset($this->keys[$cat])) {
150
                $name = $cat;
151
                $cat = 'default';
152
            }
153
        }
154 View Code Duplication
        if (isset($this->keys[$cat])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $cat = $this->keys[$cat];
156
        } else {
157
            $name = $cat.'.'.$name;
158
            $cat = $this->keys['default'];
159
        }
160
        if (is_null($name)) {
161
            unset($_SESSION[$cat]);
162
        } else {
163
            if (isset($_SESSION[$cat]) && is_array($_SESSION[$cat])) {
164
                unset($_SESSION[$cat][$name]);
165
            }
166
        }
167
168
        if ($closed) {
169
            $this->close();
170
        }
171
172
        return $this;
173
    }
174
175
    protected function &getSessionRef($key)
0 ignored issues
show
Coding Style introduced by
getSessionRef uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
176
    {
177
        $session = null;
178
        if ($this->started) {
179
            list($cat, $name) = array_pad(explode('.', $key, 2), 2, null);
180 View Code Duplication
            if (is_null($name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
                if (! isset($this->keys[$cat])) {
182
                    $name = $cat;
183
                    $cat = 'default';
184
                }
185
            }
186 View Code Duplication
            if (isset($this->keys[$cat])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
                $cat = $this->keys[$cat];
188
            } else {
189
                $name = $cat.'.'.$name;
190
                $cat = $this->keys['default'];
191
            }
192
            if (is_null($name)) {
193
                if (! isset($_SESSION[$cat])) {
194
                    $_SESSION[$cat] = null;
195
                }
196
                $session = &$_SESSION[$cat];
197
            } else {
198 View Code Duplication
                if (! isset($_SESSION[$cat]) || ! is_array($_SESSION[$cat])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
                    $_SESSION[$cat] = [];
200
                }
201 View Code Duplication
                if (! isset($_SESSION[$cat][$name])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
                    $_SESSION[$cat][$name] = null;
203
                }
204
                $session = &$_SESSION[$cat][$name];
205
            }
206
        }
207
208
        return $session;
209
    }
210
211
    protected function encodeData($data)
212
    {
213
        if ($this->base64encode) {
214
            $data = base64_encode(serialize($data));
215
        }
216
217
        return $data;
218
    }
219
220
    protected function decodeData($data)
221
    {
222
        if ($this->base64encode) {
223
            if (is_string($data)) {
224
                if (($data = base64_decode($data)) !== false) {
225
                    $data = unserialize($data);
226
                } else {
227
                    $data = null;
228
                }
229
            } else {
230
                $data = null;
231
            }
232
        }
233
234
        return $data;
235
    }
236
237
    protected function session_start_error($errno, $errstr)
0 ignored issues
show
Unused Code introduced by
The parameter $errno is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errstr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
    {
239
    }
240
}
241