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 1919use function array_reduce ;
2020use function array_search ;
2121use function array_slice ;
22+ use function array_sum ;
2223use ArrayIterator ;
2324use BadMethodCallException ;
24- use function array_sum ;
2525use function count ;
2626use function in_array ;
27+ use function is_int ;
2728use Laudis \Neo4j \Contracts \CypherContainerInterface ;
29+ use OutOfBoundsException ;
2830use function sort ;
2931use function usort ;
3032
@@ -165,11 +167,15 @@ public function find($value)
165167 }
166168
167169 /**
168- * @return T|null
170+ * @return T
169171 */
170172 public function first ()
171173 {
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 ];
173179 }
174180
175181 /**
@@ -192,8 +198,8 @@ public function join(?string $glue = null): string
192198 public function last ()
193199 {
194200 $ 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 ' ) ;
197203 }
198204
199205 return $ this ->array [$ key ];
Original file line number Diff line number Diff line change @@ -144,11 +144,11 @@ public function jsonSerialize(): array
144144 /**
145145 * @return Pair<string, T>
146146 */
147- public function first (): ? Pair
147+ public function first (): Pair
148148 {
149149 $ key = array_key_first ($ this ->map );
150150 if (!is_string ($ key )) {
151- return null ;
151+ throw new BadMethodCallException ( ' Cannot grab first element from an empty map ' ) ;
152152 }
153153
154154 return new Pair ($ key , $ this ->map [$ key ]);
@@ -157,11 +157,11 @@ public function first(): ?Pair
157157 /**
158158 * @return Pair<string, T>
159159 */
160- public function last (): ? Pair
160+ public function last (): Pair
161161 {
162162 $ key = array_key_last ($ this ->map );
163163 if (!is_string ($ key )) {
164- return null ;
164+ throw new BadMethodCallException ( ' Cannot grab last element from an empty map ' ) ;
165165 }
166166
167167 return new Pair ($ key , $ this ->map [$ key ]);
You can’t perform that action at this time.
0 commit comments