Skip to content

Commit

Permalink
Test and annotate that Arr::pull() works with int keys (#42927)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spawnia authored Jun 23, 2022
1 parent 23df2a0 commit eece7ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public static function prepend($array, $value, $key = null)
* Get a value from the array, and remove it.
*
* @param array $array
* @param string $key
* @param string|int $key
* @param mixed $default
* @return mixed
*/
Expand Down
12 changes: 9 additions & 3 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,19 +620,25 @@ public function testPull()
$array = ['name' => 'Desk', 'price' => 100];
$name = Arr::pull($array, 'name');
$this->assertSame('Desk', $name);
$this->assertEquals(['price' => 100], $array);
$this->assertSame(['price' => 100], $array);

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

// Does not work for nested keys
$array = ['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']];
$name = Arr::pull($array, 'emails.joe@example.com');
$this->assertNull($name);
$this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
$this->assertSame(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);

// Works with int keys
$array = ['First', 'Second'];
$first = Arr::pull($array, 0);
$this->assertSame('First', $first);
$this->assertSame([1 => 'Second'], $array);
}

public function testQuery()
Expand Down

0 comments on commit eece7ba

Please sign in to comment.