Passed
Push — master ( 3c979b...72c793 )
by Kirill
03:02
created

SessionSection::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Session;
13
14
use Spiral\Core\Container\InjectableInterface;
15
16
/**
17
 * Represents part of _SESSION array.
18
 */
19
final class SessionSection implements SessionSectionInterface, InjectableInterface
20
{
21
    /** @var SessionInterface */
22
    private $session;
23
24
    /**
25
     * Reference to _SESSION segment.
26
     *
27
     * @var array
28
     */
29
    private $name;
30
31
    /**
32
     * @param SessionInterface $session
33
     * @param string|null      $name
34
     */
35
    public function __construct(SessionInterface $session, string $name = null)
36
    {
37
        $this->session = $session;
38
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name can also be of type string. However, the property $name is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
    }
40
41
    /**
42
     * Shortcut for get.
43
     *
44
     * @param string $name
45
     * @return mixed|null
46
     */
47
    public function __get(string $name)
48
    {
49
        return $this->get($name);
50
    }
51
52
    /**
53
     * @param string $name
54
     * @param mixed  $value
55
     */
56
    public function __set(string $name, $value): void
57
    {
58
        $this->set($name, $value);
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return bool
65
     */
66
    public function __isset(string $name)
67
    {
68
        return $this->has($name);
69
    }
70
71
    /**
72
     * @param string $name
73
     */
74
    public function __unset(string $name): void
75
    {
76
        $this->delete($name);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function getName(): string
83
    {
84
        return $this->name;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function getIterator()
91
    {
92
        return new \ArrayIterator($this->getAll());
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function getAll(): array
99
    {
100
        $this->resumeSection();
101
102
        return $_SESSION[$this->name];
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function set(string $name, $value): void
109
    {
110
        $this->resumeSection();
111
112
        $_SESSION[$this->name][$name] = $value;
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function has(string $name): bool
119
    {
120
        $this->resumeSection();
121
122
        return array_key_exists($name, $_SESSION[$this->name]);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function get(string $name, $default = null)
129
    {
130
        if (!$this->has($name)) {
131
            return $default;
132
        }
133
134
        return $_SESSION[$this->name][$name];
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function pull(string $name, $default = null)
141
    {
142
        $value = $this->get($name, $default);
143
        $this->delete($name);
144
145
        return $value;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function delete(string $name): void
152
    {
153
        $this->resumeSection();
154
        unset($_SESSION[$this->name][$name]);
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160
    public function clear(): void
161
    {
162
        $this->resumeSection();
163
        $_SESSION[$this->name] = [];
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169
    public function offsetExists($offset): bool
170
    {
171
        return $this->has($offset);
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177
    public function offsetGet($offset)
178
    {
179
        return $this->get($offset);
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185
    public function offsetSet($offset, $value): void
186
    {
187
        $this->set($offset, $value);
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193
    public function offsetUnset($offset): void
194
    {
195
        $this->delete($offset);
196
    }
197
198
    /**
199
     * Ensure that session have proper section.
200
     */
201
    private function resumeSection(): void
202
    {
203
        $this->session->resume();
204
205
        if (!isset($_SESSION[$this->name])) {
206
            $_SESSION[$this->name] = [];
207
        }
208
    }
209
}
210