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

nette / http / 42152022906

06 Nov 2026 02:27AM UTC coverage: 82.372% (+0.4%) from 82.005%
42152022906

push

github

dg
IRequest::getFile() accepts an array of keys like Request::getFile() (BC break) [Closes #214]

1028 of 1248 relevant lines covered (82.37%)

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

100

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

110

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

118

119
        public function __unset(string $name): void
120
        {
121
                trigger_error("Unset(\$session->$name) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
122
                $this->remove($name);
×
123
        }
×
124

125

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

133

134
        public function offsetGet($name): mixed
135
        {
136
                trigger_error("Reading from \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
137
                return $this->get($name);
×
138
        }
139

140

141
        public function offsetExists($name): bool
142
        {
143
                trigger_error("Using \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
144
                return $this->__isset($name);
×
145
        }
146

147

148
        public function offsetUnset($name): void
149
        {
150
                trigger_error("Unset(\$session['$name']) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
151
                $this->remove($name);
×
152
        }
×
153

154

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

175
                $time = $seconds === null ? null : time() + $seconds;
1 ✔
176
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1 ✔
177
                        $meta[$variable ?? '']['T'] = $time;
1 ✔
178
                }
179

180
                return $this;
1 ✔
181
        }
182

183

184
        /**
185
         * Removes the expiration from the whole section or from specific variables.
186
         * @param  string|string[]|null  $variables  variable name(s) to clear expiration for; null applies to the whole section
187
         */
188
        public function removeExpiration(string|array|null $variables = null): void
189
        {
190
                $this->setExpiration(null, $variables);
1 ✔
191
        }
1 ✔
192

193

194
        /** @return ?array<string, mixed> */
195
        private function &getData(): ?array
196
        {
197
                return $_SESSION['__NF']['DATA'][$this->name];
1 ✔
198
        }
199

200

201
        /** @return ?array<string, array{T?: ?int}> */
202
        private function &getMeta(): ?array
203
        {
204
                return $_SESSION['__NF']['META'][$this->name];
1 ✔
205
        }
206
}
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