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

nette / http / 22293514258

23 Feb 2026 04:58AM UTC coverage: 83.62%. Remained the same
22293514258

push

github

dg
added CLAUDE.md

924 of 1105 relevant lines covered (83.62%)

0.84 hits per line

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

70.69
/src/Http/SessionSection.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Http;
9

10
use Nette;
11
use function array_key_exists, func_num_args, ini_get, is_array, time;
12

13

14
/**
15
 * Session section.
16
 * @implements \IteratorAggregate<string, mixed>
17
 * @implements \ArrayAccess<string, mixed>
18
 */
19
class SessionSection implements \IteratorAggregate, \ArrayAccess
20
{
21
        public bool $warnOnUndefined = false;
22

23

24
        /**
25
         * Do not call directly. Use Session::getSection().
26
         */
27
        public function __construct(
1✔
28
                private readonly Session $session,
29
                private readonly string $name,
30
        ) {
31
        }
1✔
32

33

34
        /**
35
         * Returns an iterator over all section variables.
36
         * @return \Iterator<string, mixed>
37
         */
38
        public function getIterator(): \Iterator
39
        {
40
                $this->session->autoStart(forWrite: false);
1✔
41
                return new \ArrayIterator($this->getData() ?? []);
1✔
42
        }
43

44

45
        /**
46
         * Sets a variable in this session section.
47
         */
48
        public function set(string $name, mixed $value, ?string $expire = null): void
1✔
49
        {
50
                if ($value === null) {
1✔
51
                        $this->remove($name);
×
52
                } else {
53
                        $this->session->autoStart(forWrite: true);
1✔
54
                        $this->getData()[$name] = $value;
1✔
55
                        $this->setExpiration($expire, $name);
1✔
56
                }
57
        }
1✔
58

59

60
        /**
61
         * Gets a variable from this session section.
62
         */
63
        public function get(string $name): mixed
1✔
64
        {
65
                if (func_num_args() > 1) {
1✔
66
                        throw new \ArgumentCountError(__METHOD__ . '() expects 1 arguments, given more.');
×
67
                }
68

69
                $this->session->autoStart(forWrite: false);
1✔
70
                return $this->getData()[$name] ?? null;
1✔
71
        }
72

73

74
        /**
75
         * Removes a variable or whole section.
76
         * @param  string|string[]|null  $name
77
         */
78
        public function remove(string|array|null $name = null): void
1✔
79
        {
80
                $this->session->autoStart(forWrite: false);
1✔
81
                if (func_num_args() > 1) {
1✔
82
                        throw new \ArgumentCountError(__METHOD__ . '() expects at most 1 arguments, given more.');
×
83

84
                } elseif (func_num_args()) {
1✔
85
                        $data = &$this->getData();
1✔
86
                        $meta = &$this->getMeta();
1✔
87
                        foreach ((array) $name as $name) {
1✔
88
                                unset($data[$name], $meta[$name]);
1✔
89
                        }
90
                } else {
91
                        unset($_SESSION['__NF']['DATA'][$this->name], $_SESSION['__NF']['META'][$this->name]);
1✔
92
                }
93
        }
1✔
94

95

96
        /**
97
         * Sets a variable in this session section.
98
         * @deprecated  use set() instead
99
         */
100
        public function __set(string $name, mixed $value): void
101
        {
102
                $this->session->autoStart(forWrite: true);
×
103
                $this->getData()[$name] = $value;
×
104
        }
105

106

107
        /**
108
         * Gets a variable from this session section.
109
         * @deprecated  use get() instead
110
         */
111
        public function &__get(string $name): mixed
112
        {
113
                $this->session->autoStart(forWrite: true);
×
114
                $data = &$this->getData();
×
115
                if ($this->warnOnUndefined && !array_key_exists($name, $data ?? [])) {
×
116
                        trigger_error("The variable '$name' does not exist in session section");
×
117
                }
118

119
                return $data[$name];
×
120
        }
121

122

123
        /**
124
         * Determines whether a variable in this session section is set.
125
         * @deprecated  use get() instead
126
         */
127
        public function __isset(string $name): bool
128
        {
129
                $this->session->autoStart(forWrite: false);
×
130
                return isset($this->getData()[$name]);
×
131
        }
132

133

134
        /**
135
         * Unsets a variable in this session section.
136
         * @deprecated  use remove() instead
137
         */
138
        public function __unset(string $name): void
139
        {
140
                $this->remove($name);
×
141
        }
142

143

144
        /**
145
         * Sets a variable in this session section.
146
         * @deprecated  use set() instead
147
         */
148
        public function offsetSet($name, $value): void
149
        {
150
                $this->__set($name, $value);
×
151
        }
152

153

154
        /**
155
         * Gets a variable from this session section.
156
         * @deprecated  use get() instead
157
         */
158
        public function offsetGet($name): mixed
159
        {
160
                return $this->get($name);
×
161
        }
162

163

164
        /**
165
         * Determines whether a variable in this session section is set.
166
         * @deprecated  use get() instead
167
         */
168
        public function offsetExists($name): bool
169
        {
170
                return $this->__isset($name);
×
171
        }
172

173

174
        /**
175
         * Unsets a variable in this session section.
176
         * @deprecated  use remove() instead
177
         */
178
        public function offsetUnset($name): void
179
        {
180
                $this->remove($name);
×
181
        }
182

183

184
        /**
185
         * Sets the expiration of the section or specific variables.
186
         * @param  string|string[]|null  $variables  list of variables / single variable to expire
187
         */
188
        public function setExpiration(?string $expire, string|array|null $variables = null): static
1✔
189
        {
190
                $this->session->autoStart((bool) $expire);
1✔
191
                $meta = &$this->getMeta();
1✔
192
                if ($expire) {
1✔
193
                        $expire = Nette\Utils\DateTime::from($expire)->format('U');
1✔
194
                        $max = (int) ini_get('session.gc_maxlifetime');
1✔
195
                        if (
196
                                $max !== 0 // 0 - unlimited in memcache handler
1✔
197
                                && ($expire - time() > $max + 3) // 3 - bulgarian constant
1✔
198
                        ) {
199
                                trigger_error("The expiration time is greater than the session expiration $max seconds");
1✔
200
                        }
201
                }
202

203
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1✔
204
                        $meta[$variable ?? '']['T'] = $expire ?: null;
1✔
205
                }
206

207
                return $this;
1✔
208
        }
209

210

211
        /**
212
         * Removes the expiration from the section or specific variables.
213
         * @param  string|string[]|null  $variables  list of variables / single variable to expire
214
         */
215
        public function removeExpiration(string|array|null $variables = null): void
1✔
216
        {
217
                $this->setExpiration(null, $variables);
1✔
218
        }
1✔
219

220

221
        /** @return ?array<string, mixed> */
222
        private function &getData(): ?array
223
        {
224
                return $_SESSION['__NF']['DATA'][$this->name];
1✔
225
        }
226

227

228
        /** @return ?array<string, array{T?: ?int}> */
229
        private function &getMeta(): ?array
230
        {
231
                return $_SESSION['__NF']['META'][$this->name];
1✔
232
        }
233
}
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