Skip to content

Commit 7652222

Browse files
NinerianOpencodeGitHub Copilot
committed
refactor: consolidate null-check into getSessionInstance and pass default through getSessionBucket
- getSessionInstance() now accepts ?string and handles null/empty instance internally, removing the need for a separate null-check before calling it - getSessionBucket() passes $default to getSessionInstance() instead of hardcoding [] and handles the instance-null case via getSessionInstance - setSessionVar() now uses getSessionBucket() for the sub-bucket lookup Co-Authored-By: Opencode <opencode@noreply.opencode.ai> Co-Authored-By: GitHub Copilot <copilot@noreply.github.com>
1 parent de5d16c commit 7652222

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

src/SessionHandling/SessionHandlerTrait.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ public function setSessionVar(string $key, mixed $val, ?string $parentKey = null
136136
$parent = $parentKey ?? self::$KEY_DATA;
137137
$sessionInstance = $this->getSessionInstance($instance);
138138
/** @var array<string,mixed> $bucket */
139-
$bucket = isset($sessionInstance[$parent]) && is_array($sessionInstance[$parent])
140-
? $sessionInstance[$parent]
141-
: [];
139+
$bucket = $this->getSessionBucket($parent, default: []) ?? [];
142140
$bucket[$key] = $val;
143141
$sessionInstance[$parent] = $bucket;
144142
$_SESSION[$instance] = $sessionInstance;
@@ -154,13 +152,18 @@ private function getValidInstance(): ?string
154152
}
155153

156154
/**
157-
* Return $_SESSION[$instance] as a typed array, or $default if missing/invalid.
155+
* Return $_SESSION[$instance] as a typed array, or $default if instance is
156+
* null/empty or the session entry is missing/invalid.
158157
*
159158
* @param array<string,mixed> $default
160159
* @return array<string,mixed>
161160
*/
162-
private function getSessionInstance(string $instance, array $default = []): array
161+
private function getSessionInstance(?string $instance, array $default = []): array
163162
{
163+
if ($instance === null || $instance === '') {
164+
return $default;
165+
}
166+
164167
/** @var array<string,mixed> $result */
165168
$result = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
166169
? $_SESSION[$instance]
@@ -177,12 +180,7 @@ private function getSessionInstance(string $instance, array $default = []): arra
177180
*/
178181
private function getSessionBucket(string $parentKey, ?array $default = null): ?array
179182
{
180-
$instance = $this->getValidInstance();
181-
if ($instance === null) {
182-
return $default;
183-
}
184-
185-
$sessionInstance = $this->getSessionInstance($instance, []);
183+
$sessionInstance = $this->getSessionInstance($this->getValidInstance(), $default ?? []);
186184
$bucket = $sessionInstance[$parentKey] ?? null;
187185
/** @var array<string,mixed>|null */
188186
return is_array($bucket) ? $bucket : $default;

0 commit comments

Comments
 (0)