Skip to content

Commit de5d16c

Browse files
NinerianOpencodeGitHub Copilot
committed
refactor: extract getSessionInstance helper and add default to getSessionBucket
- Add getSessionInstance(string $instance, array $default = []): array to retrieve $_SESSION[$instance] as a typed array with fallback, removing the duplicated fetch pattern in setSessionData and setSessionVar - Add $default parameter to getSessionBucket() so callers can control the fallback value (null for readers, [] for writers) Co-Authored-By: Opencode <opencode@noreply.opencode.ai> Co-Authored-By: GitHub Copilot <copilot@noreply.github.com>
1 parent 6b23810 commit de5d16c

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

src/SessionHandling/SessionHandlerTrait.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,7 @@ public function setSessionData(array $data, ?string $parentKey = null): void
114114
}
115115

116116
$parent = $parentKey ?? self::$KEY_DATA;
117-
118-
/** @var array<string,mixed> $sessionInstance */
119-
$sessionInstance = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
120-
? $_SESSION[$instance]
121-
: [];
117+
$sessionInstance = $this->getSessionInstance($instance);
122118
$sessionInstance[$parent] = $data;
123119
$_SESSION[$instance] = $sessionInstance;
124120
}
@@ -138,12 +134,7 @@ public function setSessionVar(string $key, mixed $val, ?string $parentKey = null
138134
}
139135

140136
$parent = $parentKey ?? self::$KEY_DATA;
141-
142-
/** @var array<string,mixed> $sessionInstance */
143-
$sessionInstance = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
144-
? $_SESSION[$instance]
145-
: [];
146-
137+
$sessionInstance = $this->getSessionInstance($instance);
147138
/** @var array<string,mixed> $bucket */
148139
$bucket = isset($sessionInstance[$parent]) && is_array($sessionInstance[$parent])
149140
? $sessionInstance[$parent]
@@ -163,27 +154,38 @@ private function getValidInstance(): ?string
163154
}
164155

165156
/**
166-
* Return the session bucket array for the given instance and parent key,
167-
* or null if the session structure is missing or invalid.
157+
* Return $_SESSION[$instance] as a typed array, or $default if missing/invalid.
158+
*
159+
* @param array<string,mixed> $default
160+
* @return array<string,mixed>
161+
*/
162+
private function getSessionInstance(string $instance, array $default = []): array
163+
{
164+
/** @var array<string,mixed> $result */
165+
$result = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
166+
? $_SESSION[$instance]
167+
: $default;
168+
return $result;
169+
}
170+
171+
/**
172+
* Return the session bucket array for the given parent key,
173+
* or $default if the session structure is missing or invalid.
168174
*
175+
* @param array<string,mixed>|null $default
169176
* @return array<string,mixed>|null
170177
*/
171-
private function getSessionBucket(string $parentKey): ?array
178+
private function getSessionBucket(string $parentKey, ?array $default = null): ?array
172179
{
173180
$instance = $this->getValidInstance();
174181
if ($instance === null) {
175-
return null;
176-
}
177-
178-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
179-
return null;
182+
return $default;
180183
}
181184

182-
/** @var array<string,mixed> $sessionInstance */
183-
$sessionInstance = $_SESSION[$instance];
185+
$sessionInstance = $this->getSessionInstance($instance, []);
184186
$bucket = $sessionInstance[$parentKey] ?? null;
185187
/** @var array<string,mixed>|null */
186-
return is_array($bucket) ? $bucket : null;
188+
return is_array($bucket) ? $bucket : $default;
187189
}
188190

189191
/**

0 commit comments

Comments
 (0)