Skip to content

Commit 8f835c3

Browse files
authored
Merge pull request #54 from ingenerator/1.x-bug-unique-map-null
Fix ability to store & retrieve null values in UniqueMap
2 parents 733c3bd + 88df25c commit 8f835c3

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Unreleased
22

3+
### v1.19.1 (2023-03-22)
4+
5+
* Fix a bug in UniqueMap where it throws on attempt to access an offset with a null value
6+
37
* Fix the docblock typehint for InitialisableSingletonTrait so that ::instance() to @return static
48

59
### v1.19.0 (2023-03-17)

src/ArrayHelpers/UniqueMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function offsetExists(mixed $offset): bool
3737

3838
public function offsetGet(mixed $offset): mixed
3939
{
40-
if ( ! isset($this->items[$offset])) {
40+
if ( ! array_key_exists($offset, $this->items)) {
4141
throw new \OutOfBoundsException('No item with key '.$offset);
4242
}
4343

test/unit/ArrayHelpers/UniqueMapTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public function test_it_returns_entries_by_key()
2020
[
2121
'foo' => 'some-foo',
2222
'bar' => $bar,
23+
'nully' => null,
2324
]
2425
);
2526
$this->assertSame($bar, $subject['bar']);
2627
$this->assertSame('some-foo', $subject['foo']);
28+
$this->assertSame(null, $subject['nully']);
2729
}
2830

2931
public function test_it_throws_on_access_to_undefined_key()
@@ -39,9 +41,11 @@ public function test_it_accepts_new_items()
3941
$subject = new UniqueMap(['any' => 'item']);
4042
$subject['new1'] = $new1;
4143
$subject['new2'] = 'some-string';
44+
$subject['new3'] = null;
4245

4346
$this->assertSame($new1, $subject['new1']);
4447
$this->assertSame('some-string', $subject['new2']);
48+
$this->assertSame(null, $subject['new3']);
4549
}
4650

4751
public function test_it_throws_on_attempt_to_overwrite_key()
@@ -95,9 +99,10 @@ public function test_it_is_iterable()
9599
$subject = new UniqueMap(['any' => 'item']);
96100
$subject['new'] = 'new1';
97101
$subject['other'] = 'new2';
102+
$subject['nully'] = null;
98103

99104
$this->assertSame(
100-
['any' => 'item', 'new' => 'new1', 'other' => 'new2'],
105+
['any' => 'item', 'new' => 'new1', 'other' => 'new2', 'nully' => null],
101106
iterator_to_array($subject)
102107
);
103108
}

0 commit comments

Comments
 (0)