Skip to content

Commit 855dd2b

Browse files
committed
fix(session): initialize $data property with default empty array
Prevents TypeError when $data is accessed before initialization on a newly created session. Replaces the isset($this->data) check in readData() with an explicit $loaded flag to preserve lazy loading from the session store. Fixes #273
1 parent 85fddf5 commit 855dd2b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/Server/Session/Session.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class Session implements SessionInterface
3131
*
3232
* @var array<string, mixed>
3333
*/
34-
private array $data;
34+
private array $data = [];
35+
36+
private bool $loaded = false;
3537

3638
public function __construct(
3739
private SessionStoreInterface $store,
@@ -126,6 +128,7 @@ public function forget(string $key): void
126128
public function clear(): void
127129
{
128130
$this->data = [];
131+
$this->loaded = true;
129132
}
130133

131134
public function pull(string $key, mixed $default = null): mixed
@@ -144,6 +147,7 @@ public function all(): array
144147
public function hydrate(array $attributes): void
145148
{
146149
$this->data = $attributes;
150+
$this->loaded = true;
147151
}
148152

149153
/** @return array<string, mixed> */
@@ -157,20 +161,22 @@ public function jsonSerialize(): array
157161
*/
158162
private function readData(): array
159163
{
160-
if (isset($this->data)) {
164+
if ($this->loaded) {
161165
return $this->data;
162166
}
163167

168+
$this->loaded = true;
169+
164170
$rawData = $this->store->read($this->id);
165171

166172
if (false === $rawData) {
167-
return $this->data = [];
173+
return $this->data;
168174
}
169175

170176
$decoded = json_decode($rawData, true, flags: \JSON_THROW_ON_ERROR);
171177

172178
if (!\is_array($decoded)) {
173-
return $this->data = [];
179+
return $this->data;
174180
}
175181

176182
return $this->data = $decoded;

0 commit comments

Comments
 (0)