• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 20687263522

04 Jan 2026 03:56AM UTC coverage: 74.209%. Remained the same
20687263522

push

github

web-flow
[CI] Add dead code rector rules (#337)

# Description

Add dead code rector rules.

## Types of changes

- [ ] Bug fix _(non-breaking change which fixes an issue)_
    <!-- Target the lowest major affected branch -->
- [X] New feature _(non-breaking change which adds functionality)_
    <!-- Target master -->
- [ ] Deprecation _(breaking change which removes functionality)_
    <!-- Target master -->
- [ ] Breaking change _(fix or feature that would cause existing
functionality
  to change)_
<!-- Target master, unless this is a bug fix in which case let's chat
-->
- [ ] Documentation improvement
    <!-- Target appropriate branch -->

8419 of 11345 relevant lines covered (74.21%)

8.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.17
/src/Valkyrja/Session/Manager/NullSession.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Session\Manager;
15

16
use Override;
17
use Random\RandomException;
18
use Valkyrja\Session\Manager\Contract\SessionContract as Contract;
19
use Valkyrja\Session\Throwable\Exception\InvalidCsrfToken;
20
use Valkyrja\Session\Throwable\Exception\InvalidSessionId;
21
use Valkyrja\Session\Throwable\Exception\SessionStartFailure;
22

23
use function bin2hex;
24
use function hash_equals;
25
use function is_string;
26
use function preg_match;
27
use function random_bytes;
28

29
class NullSession implements Contract
30
{
31
    /**
32
     * The session id.
33
     *
34
     * @var string
35
     */
36
    protected string $id = '';
37

38
    /**
39
     * The session name.
40
     *
41
     * @var string
42
     */
43
    protected string $name = '';
44

45
    /**
46
     * The session data.
47
     *
48
     * @var array<string, mixed>
49
     */
50
    protected array $data = [];
51

52
    /**
53
     * @throws InvalidSessionId
54
     * @throws SessionStartFailure
55
     */
56
    public function __construct(
6✔
57
        string|null $sessionId = null,
58
        string|null $sessionName = null
59
    ) {
60
        // If a session id is provided
61
        if (is_string($sessionId)) {
6✔
62
            $this->validateId($sessionId);
×
63

64
            // Set the id
65
            $this->id = $sessionId;
×
66
        }
67

68
        // If a session name is provided
69
        if (is_string($sessionName)) {
6✔
70
            // Set the name
71
            $this->name = $sessionName;
×
72
        }
73

74
        // Start the session
75
        $this->start();
6✔
76
    }
77

78
    /**
79
     * @inheritDoc
80
     */
81
    #[Override]
1✔
82
    public function start(): void
83
    {
84
    }
1✔
85

86
    /**
87
     * @inheritDoc
88
     */
89
    #[Override]
×
90
    public function getId(): string
91
    {
92
        return $this->id;
×
93
    }
94

95
    /**
96
     * @inheritDoc
97
     */
98
    #[Override]
×
99
    public function setId(string $id): void
100
    {
101
        $this->validateId($id);
×
102

103
        $this->id = $id;
×
104
    }
105

106
    /**
107
     * @inheritDoc
108
     */
109
    #[Override]
×
110
    public function getName(): string
111
    {
112
        return $this->name;
×
113
    }
114

115
    /**
116
     * @inheritDoc
117
     */
118
    #[Override]
×
119
    public function setName(string $name): void
120
    {
121
        $this->name = $name;
×
122
    }
123

124
    /**
125
     * @inheritDoc
126
     */
127
    #[Override]
×
128
    public function isActive(): bool
129
    {
130
        return true;
×
131
    }
132

133
    /**
134
     * @inheritDoc
135
     */
136
    #[Override]
×
137
    public function has(string $id): bool
138
    {
139
        return isset($this->data[$id]);
×
140
    }
141

142
    /**
143
     * @inheritDoc
144
     */
145
    #[Override]
×
146
    public function get(string $id, mixed $default = null): mixed
147
    {
148
        return $this->data[$id] ?? $default;
×
149
    }
150

151
    /**
152
     * @inheritDoc
153
     */
154
    #[Override]
×
155
    public function set(string $id, $value): void
156
    {
157
        $this->data[$id] = $value;
×
158
    }
159

160
    /**
161
     * @inheritDoc
162
     */
163
    #[Override]
×
164
    public function remove(string $id): bool
165
    {
166
        if (! $this->has($id)) {
×
167
            return false;
×
168
        }
169

170
        unset($this->data[$id]);
×
171

172
        return true;
×
173
    }
174

175
    /**
176
     * @inheritDoc
177
     */
178
    #[Override]
×
179
    public function all(): array
180
    {
181
        return $this->data;
×
182
    }
183

184
    /**
185
     * @inheritDoc
186
     *
187
     * @throws RandomException
188
     */
189
    #[Override]
×
190
    public function generateCsrfToken(string $id): string
191
    {
192
        $token = bin2hex(random_bytes(64));
×
193

194
        $this->set($id, $token);
×
195

196
        return $token;
×
197
    }
198

199
    /**
200
     * @inheritDoc
201
     */
202
    #[Override]
×
203
    public function validateCsrfToken(string $id, string $token): void
204
    {
205
        if (! $this->isCsrfTokenValid($id, $token)) {
×
206
            throw new InvalidCsrfToken("CSRF token id: `$id` has invalid token of `$token` provided");
×
207
        }
208
    }
209

210
    /**
211
     * @inheritDoc
212
     */
213
    #[Override]
×
214
    public function isCsrfTokenValid(string $id, string $token): bool
215
    {
216
        if (! $this->has($id)) {
×
217
            return false;
×
218
        }
219

220
        /** @var mixed $sessionToken */
221
        $sessionToken = $this->get($id);
×
222

223
        if (is_string($sessionToken) && hash_equals($token, $sessionToken)) {
×
224
            $this->remove($id);
×
225

226
            return true;
×
227
        }
228

229
        return false;
×
230
    }
231

232
    /**
233
     * @inheritDoc
234
     */
235
    #[Override]
×
236
    public function clear(): void
237
    {
238
        $this->data = [];
×
239
    }
240

241
    /**
242
     * @inheritDoc
243
     */
244
    #[Override]
×
245
    public function destroy(): void
246
    {
247
        $this->data = [];
×
248
    }
249

250
    /**
251
     * Validate an id.
252
     *
253
     * @param string $id The id
254
     */
255
    protected function validateId(string $id): void
×
256
    {
257
        if (! preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $id)) {
×
258
            throw new InvalidSessionId(
×
259
                "The session id, '$id', is invalid! "
×
260
                . 'Session id can only contain alpha numeric characters, dashes, commas, '
×
261
                . 'and be at least 1 character in length but up to 128 characters long.'
×
262
            );
×
263
        }
264
    }
265
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc