Passed
Push — master ( a55a47...8b3711 )
by Mikael
34s
created

Session::dumpSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\Session;
4
5
/**
6
 * Class for wrapping sessions.
7
 */
8
class Session implements SessionInterface
9
{
10
    /**
11
     * Set a session name.
12
     *
13
     * @param string $name to set as session name.
14
     *
15
     * @return self
16
     */
17 2
    public function name($name)
18
    {
19 2
        session_name($name);
20 2
        return $this;
21
    }
22
23
24
25
    /**
26
     * Start the session (ignore when on cli).
27
     *
28
     * @return self
29
     */
30
    public function start()
31
    {
32
        if (php_sapi_name() !== 'cli') {
33
            session_start();
34
        }
35
        return $this;
36
    }
37
38
39
40
    /**
41
     * Check if a value is set in the session.
42
     *
43
     * @param string $key   in session variable.
44
     *
45
     * @return boolean true if $key is set, else false.
46
     *
47
     * @SuppressWarnings(PHPMD.Superglobals)
48
     */
49 2
    public function has($key)
50
    {
51 2
        return isset($_SESSION) && isset($_SESSION[$key]);
52
    }
53
54
55
56
    /**
57
     * Get a value from the session.
58
     *
59
     * @param string $key     in session variable.
60
     * @param mixed  $default default value to return when key is not set
61
     *                        in the session.
62
     *
63
     * @return mixed value from session and null if not set.
64
     *
65
     * @SuppressWarnings(PHPMD.Superglobals)
66
     */
67 2
    public function get($key, $default = null)
68
    {
69 2
        return $this->has($key)
70 2
            ? $_SESSION[$key]
71 2
            : $default;
72
    }
73
74
75
76
    /**
77
     * Read a value from the session and unset it, this is a form of read
78
     * once flash memory using the session.
79
     *
80
     * @param string $key     in session variable.
81
     * @param mixed  $default default value to return when key is not set
82
     *                        in the session.
83
     *
84
     * @return mixed value from session and null if not set.
85
     */
86 1
    public function getOnce($key, $default = null)
87
    {
88 1
        $read = $this->get($key, $default);
89 1
        $this->delete($key);
90 1
        return $read;
91
    }
92
    
93
    
94
    
95
    /**
96
     * var_dumps the session.
97
     *
98
     * @return array array from session
99
     *
100
     * @SuppressWarnings(PHPMD.Superglobals)
101
     */
102
    public function dumpSession()
103
    {
104
        $data = var_dump($_SESSION);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($_SESSION); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Bug introduced by
Are you sure the assignment to $data is correct as var_dump($_SESSION) (which targets var_dump()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
        return $data;
106
    }
107
108
109
110
    /**
111
     * Set values in session.
112
     *
113
     * @param string $key   in session variable.
114
     * @param mixed  $value to set in session.
115
     *
116
     * @return self
117
     *
118
     * @SuppressWarnings(PHPMD.Superglobals)
119
     */
120 2
    public function set($key, $value)
121
    {
122 2
        $_SESSION[$key] = $value;
123 2
        return $this;
124
    }
125
126
127
128
    /**
129
     * Unset session value of this key.
130
     *
131
     * @param string $key in session variable.
132
     *
133
     * @return self
134
     *
135
     * @SuppressWarnings(PHPMD.Superglobals)
136
     */
137 1
    public function delete($key)
138
    {
139 1
        if (isset($_SESSION[$key])) {
140 1
            unset($_SESSION[$key]);
141 1
        }
142 1
        return $this;
143
    }
144
145
146
147
    /**
148
     * Destroy the session.
149
     *
150
     * @return void
151
     *
152
     * @SuppressWarnings(PHPMD.Superglobals)
153
     */
154
    public function destroy()
155
    {
156
        // Unset all of the session variables.
157
        $_SESSION = array();
158
159
        // Delete the session cookie.
160
        if (ini_get("session.use_cookies")) {
161
            $params = session_get_cookie_params();
162
            setcookie(
163
                session_name(),
164
                '',
165
                time() - 42000,
166
                $params["path"],
167
                $params["domain"],
168
                $params["secure"],
169
                $params["httponly"]
170
            );
171
        }
172
173
        // Finally, destroy the session.
174
        session_destroy();
175
    }
176
}
177