[Server] Stop Session::forget() from creating missing segments (#484)

This commit is contained in:
Christopher Hertel 2026-09-04 23:55:48 +02:00 • committed by GitHub
commit 4630d7424e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 5 deletions

View file

@ -110,15 +110,12 @@ class Session implements SessionInterface
while (\count($segments) > 1) { while (\count($segments) > 1) {
$segment = array_shift($segments); $segment = array_shift($segments);
if (!isset($data[$segment]) || !\is_array($data[$segment])) { if (!isset($data[$segment]) || !\is_array($data[$segment])) {
$data[$segment] = []; return;
} }
$data = &$data[$segment]; $data = &$data[$segment];
} }
$lastKey = array_shift($segments); unset($data[array_shift($segments)]);
if (isset($data[$lastKey])) {
unset($data[$lastKey]);
}
} }
public function clear(): void public function clear(): void

View file

@ -245,6 +245,21 @@ class SessionTest extends TestCase
$this->assertFalse($this->session->has('nonexistent')); $this->assertFalse($this->session->has('nonexistent'));
} }
public function testForgetDoesNotCreateIntermediateSegments(): void
{
$this->session->forget('a.b');
$this->assertSame([], $this->session->all());
}
public function testForgetDoesNotOverwriteNonArrayIntermediateValue(): void
{
$this->session->set('key', 'string_value');
$this->session->forget('key.nested');
$this->assertSame('string_value', $this->session->get('key'));
}
public function testSessionCanStoreVariousDataTypes(): void public function testSessionCanStoreVariousDataTypes(): void
{ {
$this->session->set('string', 'value'); $this->session->set('string', 'value');