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

nette / http / 3976765176

pending completion
3976765176

push

github

David Grudl
SessionSection: get() & remove() checks for extra arguments

4 of 4 new or added lines in 1 file covered. (100.0%)

841 of 1023 relevant lines covered (82.21%)

0.82 hits per line

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

95.59
/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

14

15
/**
16
 * Session section.
17
 */
18
class SessionSection implements \IteratorAggregate, \ArrayAccess
19
{
20
        use Nette\SmartObject;
21

22
        /** @var bool */
23
        public $warnOnUndefined = false;
24

25
        /** @var Session */
26
        private $session;
27

28
        /** @var string */
29
        private $name;
30

31
        /** @var array|null  session data storage */
32
        private $data;
33

34
        /** @var array|bool  session metadata storage */
35
        private $meta = false;
36

37

38
        /**
39
         * Do not call directly. Use Session::getSection().
40
         */
41
        public function __construct(Session $session, string $name)
1✔
42
        {
43
                $this->session = $session;
1✔
44
                $this->name = $name;
1✔
45
        }
1✔
46

47

48
        private function start(): void
49
        {
50
                if ($this->meta === false) {
1✔
51
                        $this->session->start();
1✔
52
                        $this->data = &$_SESSION['__NF']['DATA'][$this->name];
1✔
53
                        $this->meta = &$_SESSION['__NF']['META'][$this->name];
1✔
54
                }
55
        }
1✔
56

57

58
        /**
59
         * Returns an iterator over all section variables.
60
         */
61
        public function getIterator(): \Iterator
62
        {
63
                $this->start();
1✔
64
                return new \ArrayIterator($this->data ?? []);
1✔
65
        }
66

67

68
        /**
69
         * Sets a variable in this session section.
70
         */
71
        public function set(string $name, $value, string $expiration = null): void
1✔
72
        {
73
                $this->start();
1✔
74
                $this->data[$name] = $value;
1✔
75
                $this->setExpiration($expiration, $name);
1✔
76
        }
1✔
77

78

79
        /**
80
         * Gets a variable from this session section.
81
         * @return mixed
82
         */
83
        public function get(string $name)
1✔
84
        {
85
                if (func_num_args() > 1) {
1✔
86
                        throw new \ArgumentCountError(__METHOD__ . '() expects 1 arguments, given more.');
×
87
                }
88
                $this->start();
1✔
89
                return $this->data[$name];
1✔
90
        }
91

92

93
        /**
94
         * Removes a variable or whole section.
95
         * @param  string|array|null  $name
96
         */
97
        public function remove($name = null): void
1✔
98
        {
99
                $this->start();
1✔
100
                if (func_num_args()) {
1✔
101
                        if (func_num_args() > 1) {
1✔
102
                                throw new \ArgumentCountError(__METHOD__ . '() expects at most 1 arguments, given more.');
×
103
                        }
104
                        foreach ((array) $name as $name) {
1✔
105
                                unset($this->data[$name], $this->meta[$name]);
1✔
106
                        }
107
                } else {
108
                        $this->data = $this->meta = null;
1✔
109
                }
110
        }
1✔
111

112

113
        /**
114
         * Sets a variable in this session section.
115
         */
116
        public function __set(string $name, $value): void
1✔
117
        {
118
                $this->start();
1✔
119
                $this->data[$name] = $value;
1✔
120
        }
1✔
121

122

123
        /**
124
         * Gets a variable from this session section.
125
         * @return mixed
126
         */
127
        public function &__get(string $name)
1✔
128
        {
129
                $this->start();
1✔
130
                if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
1✔
131
                        trigger_error("The variable '$name' does not exist in session section");
×
132
                }
133

134
                return $this->data[$name];
1✔
135
        }
136

137

138
        /**
139
         * Determines whether a variable in this session section is set.
140
         */
141
        public function __isset(string $name): bool
1✔
142
        {
143
                if ($this->session->exists()) {
1✔
144
                        $this->start();
1✔
145
                }
146
                return isset($this->data[$name]);
1✔
147
        }
148

149

150
        /**
151
         * Unsets a variable in this session section.
152
         */
153
        public function __unset(string $name): void
1✔
154
        {
155
                $this->start();
1✔
156
                unset($this->data[$name], $this->meta[$name]);
1✔
157
        }
1✔
158

159

160
        /**
161
         * Sets a variable in this session section.
162
         */
163
        public function offsetSet($name, $value): void
164
        {
165
                $this->__set($name, $value);
1✔
166
        }
1✔
167

168

169
        /**
170
         * Gets a variable from this session section.
171
         * @return mixed
172
         */
173
        public function offsetGet($name)
174
        {
175
                return $this->__get($name);
1✔
176
        }
177

178

179
        /**
180
         * Determines whether a variable in this session section is set.
181
         */
182
        public function offsetExists($name): bool
183
        {
184
                return $this->__isset($name);
1✔
185
        }
186

187

188
        /**
189
         * Unsets a variable in this session section.
190
         */
191
        public function offsetUnset($name): void
192
        {
193
                $this->__unset($name);
1✔
194
        }
1✔
195

196

197
        /**
198
         * Sets the expiration of the section or specific variables.
199
         * @param  ?string  $time
200
         * @param  string|string[]  $variables  list of variables / single variable to expire
201
         * @return static
202
         */
203
        public function setExpiration($time, $variables = null)
1✔
204
        {
205
                $this->start();
1✔
206
                if ($time) {
1✔
207
                        $time = Nette\Utils\DateTime::from($time)->format('U');
1✔
208
                        $max = (int) ini_get('session.gc_maxlifetime');
1✔
209
                        if (
210
                                $max !== 0 // 0 - unlimited in memcache handler
1✔
211
                                && ($time - time() > $max + 3) // 3 - bulgarian constant
1✔
212
                        ) {
213
                                trigger_error("The expiration time is greater than the session expiration $max seconds");
1✔
214
                        }
215
                }
216

217
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1✔
218
                        $this->meta[$variable]['T'] = $time ?: null;
1✔
219
                }
220
                return $this;
1✔
221
        }
222

223

224
        /**
225
         * Removes the expiration from the section or specific variables.
226
         * @param  string|string[]  $variables  list of variables / single variable to expire
227
         */
228
        public function removeExpiration($variables = null): void
229
        {
230
                $this->start();
1✔
231
                foreach (is_array($variables) ? $variables : [$variables] as $variable) {
1✔
232
                        unset($this->meta[$variable]['T']);
1✔
233
                }
234
        }
1✔
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