Skip to content

Commit 6b23810

Browse files
NinerianOpencodeGitHub Copilot
committed
refactor: extract session validation helpers in SessionHandlerTrait
Replace repeated instance guard and $_SESSION structure checks with two private helpers: getValidInstance() and getSessionBucket(). This eliminates the duplicated boilerplate across hasSessionVar, getSessionVar, getSessionData, setSessionData, and setSessionVar. Co-Authored-By: Opencode <opencode@noreply.opencode.ai> Co-Authored-By: GitHub Copilot <copilot@noreply.github.com>
1 parent fd02600 commit 6b23810

1 file changed

Lines changed: 58 additions & 69 deletions

File tree

src/SessionHandling/SessionHandlerTrait.php

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,10 @@ protected function closeSession(): void
6464
*/
6565
public function hasSessionVar(string $key, ?string $parentKey = null): bool
6666
{
67-
$instance = $this->pluginInstanceId;
68-
if ($instance === null || $instance === '') {
69-
return false;
70-
}
71-
7267
$parent = $parentKey ?? self::$KEY_DATA;
68+
$bucket = $this->getSessionBucket($parent);
7369

74-
// Ensure $_SESSION has the expected structure
75-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
76-
return false;
77-
}
78-
79-
if (!isset($_SESSION[$instance][$parent]) || !is_array($_SESSION[$instance][$parent])) {
80-
return false;
81-
}
82-
83-
return isset($_SESSION[$instance][$parent][$key]);
70+
return $bucket !== null && isset($bucket[$key]);
8471
}
8572

8673
/**
@@ -93,23 +80,10 @@ public function hasSessionVar(string $key, ?string $parentKey = null): bool
9380
*/
9481
public function getSessionVar(string $key, ?string $parentKey = null)
9582
{
96-
$instance = $this->pluginInstanceId;
97-
if ($instance === null || $instance === '') {
98-
return null;
99-
}
100-
10183
$parent = $parentKey ?? self::$KEY_DATA;
84+
$bucket = $this->getSessionBucket($parent);
10285

103-
// Ensure $_SESSION has the expected structure
104-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
105-
return null;
106-
}
107-
108-
if (!isset($_SESSION[$instance][$parent]) || !is_array($_SESSION[$instance][$parent])) {
109-
return null;
110-
}
111-
112-
return $_SESSION[$instance][$parent][$key] ?? null;
86+
return $bucket[$key] ?? null;
11387
}
11488

11589
/**
@@ -121,82 +95,97 @@ public function getSessionVar(string $key, ?string $parentKey = null)
12195
*/
12296
public function getSessionData(?string $parentKey = null): array
12397
{
124-
$instance = $this->pluginInstanceId;
125-
if ($instance === null || $instance === '') {
126-
return [];
127-
}
128-
12998
$parent = $parentKey ?? self::$KEY_DATA;
13099

131-
// Ensure $_SESSION has the expected structure
132-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
133-
return [];
134-
}
135-
136-
$data = $_SESSION[$instance][$parent] ?? [];
137-
return is_array($data) ? $data : [];
100+
return $this->getSessionBucket($parent) ?? [];
138101
}
139102

140103
/**
141104
* Set all session variables.
142105
*
143-
* @param mixed $data
144-
* @param string|null $parentKey
145-
*
146-
*/
147-
/**
148106
* @param array<string,mixed> $data
107+
* @param string|null $parentKey
149108
*/
150109
public function setSessionData(array $data, ?string $parentKey = null): void
151110
{
152-
$instance = $this->pluginInstanceId;
153-
if ($instance === null || $instance === '') {
111+
$instance = $this->getValidInstance();
112+
if ($instance === null) {
154113
return;
155114
}
156115

157116
$parent = $parentKey ?? self::$KEY_DATA;
158117

159-
// Ensure $_SESSION has the expected structure
160-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
161-
$_SESSION[$instance] = [];
162-
}
163-
164-
$_SESSION[$instance][$parent] = $data;
118+
/** @var array<string,mixed> $sessionInstance */
119+
$sessionInstance = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
120+
? $_SESSION[$instance]
121+
: [];
122+
$sessionInstance[$parent] = $data;
123+
$_SESSION[$instance] = $sessionInstance;
165124
}
166125

167126
/**
168127
* Set a session variable.
169128
*
170-
* @param mixed $key
171-
* @param mixed $val
172-
* @param string|null $parentKey
173-
*/
174-
/**
175129
* @param string $key
176130
* @param mixed $val
131+
* @param string|null $parentKey
177132
*/
178133
public function setSessionVar(string $key, mixed $val, ?string $parentKey = null): void
179134
{
180-
$instance = $this->pluginInstanceId;
181-
if ($instance === null || $instance === '') {
135+
$instance = $this->getValidInstance();
136+
if ($instance === null) {
182137
return;
183138
}
184139

185140
$parent = $parentKey ?? self::$KEY_DATA;
186141

187-
// Ensure $_SESSION has the expected structure
188-
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
189-
$_SESSION[$instance] = [];
142+
/** @var array<string,mixed> $sessionInstance */
143+
$sessionInstance = isset($_SESSION[$instance]) && is_array($_SESSION[$instance])
144+
? $_SESSION[$instance]
145+
: [];
146+
147+
/** @var array<string,mixed> $bucket */
148+
$bucket = isset($sessionInstance[$parent]) && is_array($sessionInstance[$parent])
149+
? $sessionInstance[$parent]
150+
: [];
151+
$bucket[$key] = $val;
152+
$sessionInstance[$parent] = $bucket;
153+
$_SESSION[$instance] = $sessionInstance;
154+
}
155+
156+
/**
157+
* Return the validated plugin instance ID, or null if unset/empty.
158+
*/
159+
private function getValidInstance(): ?string
160+
{
161+
$instance = $this->pluginInstanceId;
162+
return ($instance !== null && $instance !== '') ? $instance : null;
163+
}
164+
165+
/**
166+
* Return the session bucket array for the given instance and parent key,
167+
* or null if the session structure is missing or invalid.
168+
*
169+
* @return array<string,mixed>|null
170+
*/
171+
private function getSessionBucket(string $parentKey): ?array
172+
{
173+
$instance = $this->getValidInstance();
174+
if ($instance === null) {
175+
return null;
190176
}
191177

192-
if (!isset($_SESSION[$instance][$parent]) || !is_array($_SESSION[$instance][$parent])) {
193-
$_SESSION[$instance][$parent] = [];
178+
if (!isset($_SESSION[$instance]) || !is_array($_SESSION[$instance])) {
179+
return null;
194180
}
195181

196-
$_SESSION[$instance][$parent][$key] = $val;
182+
/** @var array<string,mixed> $sessionInstance */
183+
$sessionInstance = $_SESSION[$instance];
184+
$bucket = $sessionInstance[$parentKey] ?? null;
185+
/** @var array<string,mixed>|null */
186+
return is_array($bucket) ? $bucket : null;
197187
}
198188

199-
200189
/**
201190
* Destroy the session with the given id
202191
*

0 commit comments

Comments
 (0)