File tree Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Original file line number Diff line number Diff line change 19
19
use function array_reduce ;
20
20
use function array_search ;
21
21
use function array_slice ;
22
+ use function array_sum ;
22
23
use ArrayIterator ;
23
24
use BadMethodCallException ;
24
- use function array_sum ;
25
25
use function count ;
26
26
use function in_array ;
27
+ use function is_int ;
27
28
use Laudis \Neo4j \Contracts \CypherContainerInterface ;
29
+ use OutOfBoundsException ;
28
30
use function sort ;
29
31
use function usort ;
30
32
@@ -165,11 +167,15 @@ public function find($value)
165
167
}
166
168
167
169
/**
168
- * @return T|null
170
+ * @return T
169
171
*/
170
172
public function first ()
171
173
{
172
- return $ this ->array [0 ] ?? null ;
174
+ if (!isset ($ this ->array [0 ])) {
175
+ throw new OutOfBoundsException ('Cannot grab first element of an empty list ' );
176
+ }
177
+
178
+ return $ this ->array [0 ];
173
179
}
174
180
175
181
/**
@@ -192,8 +198,8 @@ public function join(?string $glue = null): string
192
198
public function last ()
193
199
{
194
200
$ key = array_key_last ($ this ->array );
195
- if ($ key === null ) {
196
- return null ;
201
+ if (! is_int ( $ key) ) {
202
+ throw new BadMethodCallException ( ' Cannot grab last element from an empty list ' ) ;
197
203
}
198
204
199
205
return $ this ->array [$ key ];
Original file line number Diff line number Diff line change @@ -144,11 +144,11 @@ public function jsonSerialize(): array
144
144
/**
145
145
* @return Pair<string, T>
146
146
*/
147
- public function first (): ? Pair
147
+ public function first (): Pair
148
148
{
149
149
$ key = array_key_first ($ this ->map );
150
150
if (!is_string ($ key )) {
151
- return null ;
151
+ throw new BadMethodCallException ( ' Cannot grab first element from an empty map ' ) ;
152
152
}
153
153
154
154
return new Pair ($ key , $ this ->map [$ key ]);
@@ -157,11 +157,11 @@ public function first(): ?Pair
157
157
/**
158
158
* @return Pair<string, T>
159
159
*/
160
- public function last (): ? Pair
160
+ public function last (): Pair
161
161
{
162
162
$ key = array_key_last ($ this ->map );
163
163
if (!is_string ($ key )) {
164
- return null ;
164
+ throw new BadMethodCallException ( ' Cannot grab last element from an empty map ' ) ;
165
165
}
166
166
167
167
return new Pair ($ key , $ this ->map [$ key ]);
You can’t perform that action at this time.
0 commit comments