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

nette / http / 22837209177

09 Mar 2026 03:32AM UTC coverage: 83.513% (-0.1%) from 83.62%
22837209177

push

github

dg
added CLAUDE.md

932 of 1116 relevant lines covered (83.51%)

0.84 hits per line

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

70.0
/src/Http/SessionSection.php
1
<?php declare(strict_types=1);
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, is_string, 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
        /** Emits a warning when accessing an undefined variable in this section */
22
        public bool $warnOnUndefined = false;
23

24

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

34

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

45

46
        /**
47
         * Sets a variable in this session section. Passing null removes it.
48
         * The optional $expire sets per-variable expiration as a time string (e.g. '30 seconds').
49
         */
50
        public function set(string $name, mixed $value, ?string $expire = null): void
1✔
51
        {
52
                if ($value === null) {
1✔
53
                        $this->remove($name);
×
54
                } else {
55
                        $this->session->autoStart(forWrite: true);
1✔
56
                        $this->getData()[$name] = $value;
1✔
57
                        $this->setExpiration($expire, $name);
1✔
58
                }
59
        }
1✔
60

61

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

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

75

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

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

97

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

108

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

122
                return $data[$name];
×
123
        }
124

125

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

136

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

146

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

157

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

167

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

177

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

187

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

208
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1✔
209
                        $meta[$variable ?? '']['T'] = $expire ?: null;
1✔
210
                }
211

212
                return $this;
1✔
213
        }
214

215

216
        /**
217
         * Removes the expiration from the whole section or from specific variables.
218
         * @param  string|string[]|null  $variables  variable name(s) to clear expiration for; null applies to the whole section
219
         */
220
        public function removeExpiration(string|array|null $variables = null): void
1✔
221
        {
222
                $this->setExpiration(null, $variables);
1✔
223
        }
1✔
224

225

226
        /** @return ?array<string, mixed> */
227
        private function &getData(): ?array
228
        {
229
                return $_SESSION['__NF']['DATA'][$this->name];
1✔
230
        }
231

232

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