File tree Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -139,10 +139,10 @@ public function put($key, $value): MapInterface
139
139
/**
140
140
* @psalm-mutation-free
141
141
*/
142
- public function get ($ key )
142
+ public function get (string $ key )
143
143
{
144
- if (! array_key_exists ( $ key , $ this ->data )) {
145
- throw new OutOfBoundsException (sprintf ('There is no value stored for provided key: %s ' , ( string ) $ key ));
144
+ if (! $ this ->has ( $ key )) {
145
+ throw new OutOfBoundsException (sprintf ('There is no value stored for provided key: %s ' , $ key ));
146
146
}
147
147
148
148
return $ this ->data [$ key ];
@@ -267,4 +267,12 @@ public function map(callable $callback): MapInterface
267
267
{
268
268
return new GenericMap (array_map ($ callback , $ this ->data ));
269
269
}
270
+
271
+ /**
272
+ * @psalm-mutation-free
273
+ */
274
+ public function has (string $ key ): bool
275
+ {
276
+ return array_key_exists ($ key , $ this ->data );
277
+ }
270
278
}
Original file line number Diff line number Diff line change @@ -92,12 +92,11 @@ public function keys(): OrderedListInterface;
92
92
public function put ($ key , $ value ): MapInterface ;
93
93
94
94
/**
95
- * @psalm-param string $key
96
95
* @psalm-return TValue
97
96
* @throws OutOfBoundsException if key does not exist.
98
97
* @psalm-mutation-free
99
98
*/
100
- public function get ($ key );
99
+ public function get (string $ key );
101
100
102
101
/**
103
102
* @psalm-param MapInterface<TValue> $other
@@ -124,4 +123,9 @@ public function intersectUserAssoc(
124
123
?callable $ valueComparator = null ,
125
124
?callable $ keyComparator = null
126
125
): MapInterface ;
126
+
127
+ /**
128
+ * @psalm-mutation-free
129
+ */
130
+ public function has (string $ key ): bool ;
127
131
}
Original file line number Diff line number Diff line change @@ -467,4 +467,23 @@ public function testGetThrowsOutOfBoundsExceptionWhenKeyDoesNotExist(): void
467
467
$ this ->expectException (OutOfBoundsException::class);
468
468
$ map ->get ('foo ' );
469
469
}
470
+
471
+ public function testHasDetectsExistingKey (): void
472
+ {
473
+ $ map = new GenericMap (['foo ' => 'bar ' ]);
474
+
475
+ self ::assertTrue ($ map ->has ('foo ' ));
476
+ }
477
+
478
+ public function testHasReturnsFalseOnEmptyMap (): void
479
+ {
480
+ $ map = new GenericMap ([]);
481
+ self ::assertFalse ($ map ->has ('foo ' ));
482
+ }
483
+
484
+ public function testHasReturnsFalseForDueToCaseSensitivity (): void
485
+ {
486
+ $ map = new GenericMap (['foo ' => 'bar ' ]);
487
+ self ::assertFalse ($ map ->has ('Foo ' ));
488
+ }
470
489
}
You can’t perform that action at this time.
0 commit comments