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

nette / http / 40214702155

25 Oct 2026 04:58PM UTC coverage: 82.088% (-0.03%) from 82.117%
40214702155

push

github

dg
uses #Deprecated wip

1022 of 1245 relevant lines covered (82.09%)

0.82 hits per line

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

57.58
/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 function func_num_args, ini_get, is_array, is_string;
11

12

13
/**
14
 * Session section.
15
 * @implements \IteratorAggregate<string, mixed>
16
 * @implements \ArrayAccess<string, mixed>
17
 */
18
class SessionSection implements \IteratorAggregate, \ArrayAccess
19
{
20
        /**
21
         * Do not call directly. Use Session::getSection().
22
         */
23
        public function __construct(
1 ✔
24
                private readonly Session $session,
25
                private readonly string $name,
26
        ) {
27
        }
1 ✔
28

29

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

40

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

56

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

66
                $this->session->autoStart(forWrite: false);
1 ✔
67
                return $this->getData()[$name] ?? null;
1 ✔
68
        }
69

70

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

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

92

93
        #[\Deprecated('use set() instead')]
94
        public function __set(string $name, mixed $value): void
95
        {
96
                trigger_error("Writing to \$session->$name is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
×
97
                $this->session->autoStart(forWrite: true);
×
98
                $this->getData()[$name] = $value;
×
99
        }
×
100

101

102
        #[\Deprecated('use get() instead')]
103
        public function &__get(string $name): mixed
104
        {
105
                trigger_error("Reading from \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
106
                $this->session->autoStart(forWrite: true);
×
107
                $data = &$this->getData();
×
108
                $data ??= [];
×
109
                return $data[$name];
×
110
        }
111

112

113
        #[\Deprecated('use get() instead')]
114
        public function __isset(string $name): bool
115
        {
116
                trigger_error("Using \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
117
                $this->session->autoStart(forWrite: false);
×
118
                return isset($this->getData()[$name]);
×
119
        }
120

121

122
        #[\Deprecated('use remove() instead')]
123
        public function __unset(string $name): void
124
        {
125
                trigger_error("Unset(\$session->$name) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
126
                $this->remove($name);
×
127
        }
×
128

129

130
        #[\Deprecated('use set() instead')]
131
        public function offsetSet($name, $value): void
132
        {
133
                trigger_error("Writing to \$session['$name'] is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
×
134
                assert(is_string($name));
135
                $this->__set($name, $value);
×
136
        }
×
137

138

139
        #[\Deprecated('use get() instead')]
140
        public function offsetGet($name): mixed
141
        {
142
                trigger_error("Reading from \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
143
                return $this->get($name);
×
144
        }
145

146

147
        #[\Deprecated('use get() instead')]
148
        public function offsetExists($name): bool
149
        {
150
                trigger_error("Using \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
151
                return $this->__isset($name);
×
152
        }
153

154

155
        #[\Deprecated('use remove() instead')]
156
        public function offsetUnset($name): void
157
        {
158
                trigger_error("Unset(\$session['$name']) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
159
                $this->remove($name);
×
160
        }
×
161

162

163
        /**
164
         * Sets the expiration time for the whole section or for specific variables.
165
         * Pass null to clear the expiration.
166
         * @param  string|string[]|null  $variables  variable name(s) to apply the expiration to; null applies to the whole section
167
         */
168
        public function setExpiration(?string $expire, string|array|null $variables = null): static
169
        {
170
                $seconds = Helpers::expirationToSeconds($expire);
1 ✔
171
                $this->session->autoStart($seconds !== null);
1 ✔
172
                $meta = &$this->getMeta();
1 ✔
173
                if ($seconds !== null) {
1 ✔
174
                        $max = (int) ini_get('session.gc_maxlifetime');
1 ✔
175
                        if (
176
                                $max !== 0 // 0 - unlimited in memcache handler
1 ✔
177
                                && ($seconds > $max + 3) // 3 - bulgarian constant
1 ✔
178
                        ) {
179
                                trigger_error("The expiration time is greater than the session expiration $max seconds");
1 ✔
180
                        }
181
                }
182

183
                $time = $seconds === null ? null : time() + $seconds;
1 ✔
184
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1 ✔
185
                        $meta[$variable ?? '']['T'] = $time;
1 ✔
186
                }
187

188
                return $this;
1 ✔
189
        }
190

191

192
        /**
193
         * Removes the expiration from the whole section or from specific variables.
194
         * @param  string|string[]|null  $variables  variable name(s) to clear expiration for; null applies to the whole section
195
         */
196
        public function removeExpiration(string|array|null $variables = null): void
197
        {
198
                $this->setExpiration(null, $variables);
1 ✔
199
        }
1 ✔
200

201

202
        /** @return ?array<string, mixed> */
203
        private function &getData(): ?array
204
        {
205
                return $_SESSION['__NF']['DATA'][$this->name];
1 ✔
206
        }
207

208

209
        /** @return ?array<string, array{T?: ?int}> */
210
        private function &getMeta(): ?array
211
        {
212
                return $_SESSION['__NF']['META'][$this->name];
1 ✔
213
        }
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc