Passed
Push — master ( 8242d2...71bfa3 )
by Arnold
05:32
created

GlobalSession::status()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Jasny\Session;
6
7
use Jasny\Session\Flash\FlashBag;
8
use Jasny\Session\Flash\FlashTrait;
9
10
/**
11
 * Wrapper round $_SESSION.
12
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
13
class GlobalSession implements SessionInterface
14
{
15
    use FlashTrait;
16
17
    protected array $options;
0 ignored issues
show
introduced by
Property Jasny\Session\GlobalSession::$options type has no value type specified in iterable type array.
Loading history...
18
19
    /**
20
     * Session constructor.
21
     *
22
     * @param array<string,mixed> $options  Passed to session_start()
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
23
     * @param FlashBag            $flashes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
24
     */
25
    public function __construct(array $options = [], ?FlashBag $flashes = null)
0 ignored issues
show
introduced by
Method Jasny\Session\GlobalSession::__construct() has parameter $flashes with no value type specified in iterable type Jasny\Session\Flash\FlashBag.
Loading history...
26
    {
27
        $this->options = $options;
28
        $this->flashes = $flashes ?? new FlashBag();
29
    }
30
31
32
    /**
33
     * Start the session.
34
     * @see session_start()
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
35
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
36
    public function start(): void
37
    {
38
        session_start($this->options);
39
    }
40
41
    /**
42
     * Write session data and end session.
43
     * @see session_write_close()
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public function stop(): void
46
    {
47
        session_write_close();
48
    }
49
50
    /**
51
     * Discard session array changes and finish session.
52
     * @see session_abort()
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
53
     *
54
     * Only a shallow clone is done to save the original data. If the session data contains objects, make sure that
55
     *   `__clone()` is overwritten, so it does a deep clone.
56
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
57
    public function abort(): void
58
    {
59
        session_abort();
60
    }
61
62
63
    /**
64
     * Get the sessions status.
65
     * @see session_status()
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function status(): int
68
    {
69
        return session_status();
70
    }
71
72
73
    /**
74
     * Clear all data from the session.
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public function clear(): void
77
    {
78
        $this->assertStarted();
79
80
        $_SESSION = [];
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
85
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
86
     */
87
    public function offsetExists($offset)
0 ignored issues
show
introduced by
Parameter #1 $offset (string) of method Jasny\Session\GlobalSession::offsetExists() should be contravariant with parameter $offset (mixed) of method ArrayAccess<mixed,mixed>::offsetExists()
Loading history...
introduced by
Return type (mixed) of method Jasny\Session\GlobalSession::offsetExists() should be covariant with return type (bool) of method ArrayAccess<mixed,mixed>::offsetExists()
Loading history...
88
    {
89
        $this->assertStarted();
90
91
        return array_key_exists($offset, $_SESSION);
92
    }
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
95
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
96
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
97
     */
98
    public function offsetGet($offset)
0 ignored issues
show
introduced by
Parameter #1 $offset (string) of method Jasny\Session\GlobalSession::offsetGet() should be contravariant with parameter $offset (mixed) of method ArrayAccess<mixed,mixed>::offsetGet()
Loading history...
99
    {
100
        $this->assertStarted();
101
102
        return $_SESSION[$offset];
103
    }
104
105
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
106
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
109
    public function offsetSet($offset, $value)
0 ignored issues
show
introduced by
Method Jasny\Session\GlobalSession::offsetSet() has no return typehint specified.
Loading history...
introduced by
Parameter #1 $offset (string) of method Jasny\Session\GlobalSession::offsetSet() should be contravariant with parameter $offset (mixed) of method ArrayAccess<mixed,mixed>::offsetSet()
Loading history...
110
    {
111
        $this->assertStarted();
112
113
        $_SESSION[$offset] = $value;
114
    }
115
116
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
117
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function offsetUnset($offset)
0 ignored issues
show
introduced by
Method Jasny\Session\GlobalSession::offsetUnset() has no return typehint specified.
Loading history...
introduced by
Parameter #1 $offset (string) of method Jasny\Session\GlobalSession::offsetUnset() should be contravariant with parameter $offset (mixed) of method ArrayAccess<mixed,mixed>::offsetUnset()
Loading history...
120
    {
121
        $this->assertStarted();
122
123
        unset($_SESSION[$offset]);
124
    }
125
126
    /**
127
     * Assert that there is an active session.
128
     *
129
     * @throws \RuntimeException
130
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
131
    protected function assertStarted()
0 ignored issues
show
introduced by
Method Jasny\Session\GlobalSession::assertStarted() has no return typehint specified.
Loading history...
132
    {
133
        if (session_status() !== \PHP_SESSION_ACTIVE) {
134
            throw new \RuntimeException("Session not started");
135
        }
136
    }
137
}
138