Skip to content

Commit 3dae8f4

Browse files
committed
Fix remove() to use null check instead of truthy check, update docblocks
- `if (\$key)` was changed to `if (\$key !== null)` to correctly handle empty string ('') and '0' keys, which are falsy but valid - Updated `@param null \$key` docblocks to `@param string|null \$key` in both Segment and SegmentInterface Addresses CodeRabbit review comments on PR #99.
1 parent c1fcc42 commit 3dae8f4

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/Segment.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,20 @@ public function clear()
117117
/**
118118
* Remove a key from the segment, or remove the entire segment (including key) from the session
119119
*
120-
* @param null $key
120+
* @param string|null $key The key to remove, or null to clear the entire segment.
121121
*/
122122
public function remove(?string $key = null) {
123-
if ($this->resumeSession()) {
124-
if($key){
125-
if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){
126-
unset($_SESSION[$this->name][$key]);
127-
}
128-
} else {
129-
unset($_SESSION[$this->name]);
130-
}
123+
if (! $this->resumeSession()) {
124+
return;
125+
}
126+
if ($key === null) {
127+
unset($_SESSION[$this->name]);
128+
unset($_SESSION[Session::FLASH_NOW][$this->name]);
129+
unset($_SESSION[Session::FLASH_NEXT][$this->name]);
130+
return;
131+
}
132+
if (array_key_exists($key, $_SESSION[$this->name])) {
133+
unset($_SESSION[$this->name][$key]);
131134
}
132135
}
133136

src/SegmentInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function keepFlash();
138138
/**
139139
* Remove a key from the segment, or remove the entire segment (including key) from the session
140140
*
141-
* @param null $key
141+
* @param string|null $key The key to remove, or null to clear the entire segment.
142142
*/
143143
public function remove(?string $key = null);
144144
}

tests/SegmentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ public function testGetFlashDoesNotStartSession()
194194
$this->assertFalse($this->session->isStarted());
195195
}
196196

197+
public function testRemoveDoesNothingWhenSessionNotStarted(): void
198+
{
199+
$this->segment->remove('foo');
200+
$this->assertFalse($this->session->isStarted());
201+
}
202+
197203
public function testRemoveKey(){
198204
$this->segment->set('foo', 'bar');
199205
$this->segment->set('baz', 'dib');

0 commit comments

Comments
 (0)