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

nette / http / 21830508055

09 Feb 2026 03:10PM UTC coverage: 83.772% (+0.03%) from 83.744%
21830508055

push

github

dg
added RequestFactory::setForceHttps()

8 of 9 new or added lines in 2 files covered. (88.89%)

75 existing lines in 9 files now uncovered.

924 of 1103 relevant lines covered (83.77%)

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
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
declare(strict_types=1);
9

10
namespace Nette\Http;
11

12
use Nette;
13
use function array_key_exists, func_num_args, ini_get, is_array, time;
14

15

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

25

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

35

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

46

47
        /**
48
         * Sets a variable in this session section.
49
         */
50
        public function set(string $name, mixed $value, ?string $expire = null): void
1✔
51
        {
52
                if ($value === null) {
1✔
UNCOV
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✔
UNCOV
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 whole 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✔
UNCOV
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
        {
UNCOV
104
                $this->session->autoStart(forWrite: true);
×
UNCOV
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);
×
UNCOV
116
                $data = &$this->getData();
×
UNCOV
117
                if ($this->warnOnUndefined && !array_key_exists($name, $data ?? [])) {
×
118
                        trigger_error("The variable '$name' does not exist in session section");
×
119
                }
120

UNCOV
121
                return $data[$name];
×
122
        }
123

124

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

135

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

145

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

155

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

165

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

175

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

185

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

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

209
                return $this;
1✔
210
        }
211

212

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

222

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

229

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