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

SyntaxPilot / Session / 26382219242

25 May 2026 03:50AM UTC coverage: 21.247%. Remained the same
26382219242

push

github

SyntaxPilot
Merge branch 'main' of https://github.com/SyntaxPilot/Session

6 of 7 new or added lines in 1 file covered. (85.71%)

276 of 1299 relevant lines covered (21.25%)

0.56 hits per line

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

91.18
/src/ArraySessionHandler.php
1
<?php
2
/**
3
 * Copyright (c) 2027 Nicholas English
4
 *
5
 * This file is licensed under the MIT License.
6
 * See the LICENSE file in the project root for full license information.
7
 */
8

9
declare(strict_types=1);
10

11
namespace SyntaxPilot\Session;
12

13
use InvalidArgumentException;
14
use SessionHandlerInterface;
15

16
/**
17
 * In-memory session handler.
18
 *
19
 * Useful for tests. Not suitable for normal web requests.
20
 */
21
final class ArraySessionHandler implements SessionHandlerInterface
22
{
23
    /**
24
     * @var array<string, array{data: string, last_activity: int}>
25
     */
26
    private array $storage = [];
27

28
    public function __construct(
13✔
29
        private readonly int $ttl = 1440,
30
    ) {
31
        if ($this->ttl <= 0) {
13✔
32
            throw new InvalidArgumentException('Session TTL must be greater than zero.');
×
33
        }
34
    }
35

36
    public function open(string $path, string $name): bool
2✔
37
    {
38
        return true;
2✔
39
    }
40

41
    public function close(): bool
2✔
42
    {
43
        return true;
2✔
44
    }
45

46
    public function read(string $id): string|false
12✔
47
    {
48
        if (!isset($this->storage[$id])) {
12✔
49
            return '';
8✔
50
        }
51

52
        $item = $this->storage[$id];
7✔
53

54
        if ($item['last_activity'] < time() - $this->ttl) {
7✔
NEW
55
            unset($this->storage[$id]);
×
56

57
            return '';
×
58
        }
59

60
        return $item['data'];
7✔
61
    }
62

63
    public function write(string $id, string $data): bool
11✔
64
    {
65
        $this->storage[$id] = [
11✔
66
            'data' => $data,
11✔
67
            'last_activity' => time(),
11✔
68
        ];
11✔
69

70
        return true;
11✔
71
    }
72

73
    public function destroy(string $id): bool
4✔
74
    {
75
        unset($this->storage[$id]);
4✔
76

77
        return true;
4✔
78
    }
79

80
    public function gc(int $max_lifetime): int|false
1✔
81
    {
82
        $deleted = 0;
1✔
83
        $expiresBefore = time() - $this->ttl;
1✔
84

85
        foreach ($this->storage as $id => $item) {
1✔
86
            if ($item['last_activity'] < $expiresBefore) {
1✔
87
                unset($this->storage[$id]);
1✔
88
                $deleted++;
1✔
89
            }
90
        }
91

92
        return $deleted;
1✔
93
    }
94

95
    public static function reset(): void
13✔
96
    {
97
        // Storage is per instance, so there is no shared state to reset.
98
    }
13✔
99
}
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