Skip to content

Commit 6a56d94

Browse files
innocenzibrendt
andauthored
feat(support): support not specifying a value to ArrayHelper::pop and ArrayHelper::unshift (#692)
Co-authored-by: Brent Roose <[email protected]>
1 parent a0fffe9 commit 6a56d94

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Tempest/Support/src/ArrayHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function last(?Closure $filter = null): mixed
431431
*
432432
* @param mixed $value The popped value will be stored in this variable
433433
*/
434-
public function pop(mixed &$value): self
434+
public function pop(mixed &$value = null): self
435435
{
436436
$value = $this->last();
437437

@@ -443,7 +443,7 @@ public function pop(mixed &$value): self
443443
*
444444
* @param mixed $value The unshifted value will be stored in this variable
445445
*/
446-
public function unshift(mixed &$value): self
446+
public function unshift(mixed &$value = null): self
447447
{
448448
$value = $this->first();
449449

src/Tempest/Support/tests/ArrayHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public function test_pop(): void
166166

167167
$this->assertSame('c', $value);
168168
$this->assertTrue($array->equals(['a', 'b']));
169+
170+
$this->assertTrue(arr(['a', 'b', 'c'])->pop()->equals(['a', 'b']));
171+
$this->assertTrue(arr()->pop()->isEmpty());
172+
173+
arr()->pop($value);
174+
$this->assertNull($value);
169175
}
170176

171177
public function test_unshift(): void
@@ -174,6 +180,12 @@ public function test_unshift(): void
174180

175181
$this->assertSame('a', $value);
176182
$this->assertTrue($array->equals(['b', 'c']));
183+
184+
$this->assertTrue(arr(['a', 'b', 'c'])->unshift()->equals(['b', 'c']));
185+
$this->assertTrue(arr()->unshift()->isEmpty());
186+
187+
arr()->unshift($value);
188+
$this->assertNull($value);
177189
}
178190

179191
public function test_last(): void

0 commit comments

Comments
 (0)