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

nette / http / 27922157098

21 Jun 2026 11:58PM UTC coverage: 82.955% (-0.2%) from 83.128%
27922157098

push

github

dg
uses #Deprecated wip

1061 of 1279 relevant lines covered (82.96%)

0.83 hits per line

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

65.15
/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 function array_key_exists, func_num_args, ini_get, is_array, is_string, time;
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
1✔
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
1✔
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
1✔
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 ??= [];                return $data[$name];
×
109
        }
110

111

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

120

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

128

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

137

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

145

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

153

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

161

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

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

187
                return $this;
1✔
188
        }
189

190

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

200

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

207

208
        /** @return ?array<string, array{T?: ?int}> */
209
        private function &getMeta(): ?array
210
        {
211
                return $_SESSION['__NF']['META'][$this->name];
1✔
212
        }
213
}
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