Skip to content

Commit eece7ba

Browse files
authored
Test and annotate that Arr::pull() works with int keys (#42927)
This pull request corrects the type hint of the argument `$key` in `Illuminate\Support\Arr::pull()` to allow int keys. This avoids false-positives from IDEs and static analysis tools. In order to ensure it actually works as intended, I added a test case.
1 parent 23df2a0 commit eece7ba

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public static function prepend($array, $value, $key = null)
510510
* Get a value from the array, and remove it.
511511
*
512512
* @param array $array
513-
* @param string $key
513+
* @param string|int $key
514514
* @param mixed $default
515515
* @return mixed
516516
*/

tests/Support/SupportArrTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,19 +620,25 @@ public function testPull()
620620
$array = ['name' => 'Desk', 'price' => 100];
621621
$name = Arr::pull($array, 'name');
622622
$this->assertSame('Desk', $name);
623-
$this->assertEquals(['price' => 100], $array);
623+
$this->assertSame(['price' => 100], $array);
624624

625625
// Only works on first level keys
626626
$array = ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane'];
627627
$name = Arr::pull($array, 'joe@example.com');
628628
$this->assertSame('Joe', $name);
629-
$this->assertEquals(['jane@localhost' => 'Jane'], $array);
629+
$this->assertSame(['jane@localhost' => 'Jane'], $array);
630630

631631
// Does not work for nested keys
632632
$array = ['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']];
633633
$name = Arr::pull($array, 'emails.joe@example.com');
634634
$this->assertNull($name);
635-
$this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
635+
$this->assertSame(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
636+
637+
// Works with int keys
638+
$array = ['First', 'Second'];
639+
$first = Arr::pull($array, 0);
640+
$this->assertSame('First', $first);
641+
$this->assertSame([1 => 'Second'], $array);
636642
}
637643

638644
public function testQuery()

0 commit comments

Comments
 (0)