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

nette / http / 8877511590

29 Apr 2024 11:05AM UTC coverage: 81.537% (-0.2%) from 81.776%
8877511590

push

github

dg
IRequest, IResponse: added typehints, unification (BC break)

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

53 existing lines in 5 files now uncovered.

870 of 1067 relevant lines covered (81.54%)

0.82 hits per line

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

64.06
/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
        /**
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
         */
33
        public function getIterator(): \Iterator
34
        {
35
                $this->session->autoStart(false);
1✔
36
                return new \ArrayIterator($this->getData() ?? []);
1✔
37
        }
38

39

40
        /**
41
         * Sets a variable in this session section.
42
         */
43
        public function set(string $name, mixed $value, ?string $expire = null): void
1✔
44
        {
45
                if ($value === null) {
1✔
UNCOV
46
                        $this->remove($name);
×
47
                } else {
48
                        $this->session->autoStart(true);
1✔
49
                        $this->getData()[$name] = $value;
1✔
50
                        $this->setExpiration($expire, $name);
1✔
51
                }
52
        }
1✔
53

54

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

64
                $this->session->autoStart(false);
1✔
65
                return $this->getData()[$name] ?? null;
1✔
66
        }
67

68

69
        /**
70
         * Removes a variable or whole section.
71
         * @param  string|string[]|null  $name
72
         */
73
        public function remove(string|array|null $name = null): void
1✔
74
        {
75
                $this->session->autoStart(false);
1✔
76
                if (func_num_args() > 1) {
1✔
UNCOV
77
                        throw new \ArgumentCountError(__METHOD__ . '() expects at most 1 arguments, given more.');
×
78

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

90

91
        /**
92
         * Sets a variable in this session section.
93
         * @deprecated  use set() instead
94
         */
95
        public function __set(string $name, $value): void
96
        {
UNCOV
97
                trigger_error("Writing to \$session->$name is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
×
UNCOV
98
                $this->session->autoStart(true);
×
UNCOV
99
                $this->getData()[$name] = $value;
×
100
        }
101

102

103
        /**
104
         * Gets a variable from this session section.
105
         * @deprecated  use get() instead
106
         */
107
        public function &__get(string $name): mixed
108
        {
UNCOV
109
                trigger_error("Reading from \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
UNCOV
110
                $this->session->autoStart(true);
×
UNCOV
111
                $data = &$this->getData();
×
UNCOV
112
                return $data[$name];
×
113
        }
114

115

116
        /**
117
         * Determines whether a variable in this session section is set.
118
         * @deprecated  use get() instead
119
         */
120
        public function __isset(string $name): bool
121
        {
UNCOV
122
                trigger_error("Using \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
UNCOV
123
                $this->session->autoStart(false);
×
UNCOV
124
                return isset($this->getData()[$name]);
×
125
        }
126

127

128
        /**
129
         * Unsets a variable in this session section.
130
         * @deprecated  use remove() instead
131
         */
132
        public function __unset(string $name): void
133
        {
UNCOV
134
                trigger_error("Unset(\$session->$name) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
UNCOV
135
                $this->remove($name);
×
136
        }
137

138

139
        /**
140
         * Sets a variable in this session section.
141
         * @deprecated  use set() instead
142
         */
143
        public function offsetSet($name, $value): void
144
        {
UNCOV
145
                trigger_error("Writing to \$session['$name'] is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
×
UNCOV
146
                $this->__set($name, $value);
×
147
        }
148

149

150
        /**
151
         * Gets a variable from this session section.
152
         * @deprecated  use get() instead
153
         */
154
        public function offsetGet($name): mixed
155
        {
UNCOV
156
                trigger_error("Reading from \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
UNCOV
157
                return $this->get($name);
×
158
        }
159

160

161
        /**
162
         * Determines whether a variable in this session section is set.
163
         * @deprecated  use get() instead
164
         */
165
        public function offsetExists($name): bool
166
        {
UNCOV
167
                trigger_error("Using \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
×
UNCOV
168
                return $this->__isset($name);
×
169
        }
170

171

172
        /**
173
         * Unsets a variable in this session section.
174
         * @deprecated  use remove() instead
175
         */
176
        public function offsetUnset($name): void
177
        {
UNCOV
178
                trigger_error("Unset(\$session['$name']) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
×
UNCOV
179
                $this->remove($name);
×
180
        }
181

182

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

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

206
                return $this;
1✔
207
        }
208

209

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

219

220
        private function &getData()
221
        {
222
                return $_SESSION['__NF']['DATA'][$this->name];
1✔
223
        }
224

225

226
        private function &getMeta()
227
        {
228
                return $_SESSION['__NF']['META'][$this->name];
1✔
229
        }
230
}
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