Skip to content

Commit b17f285

Browse files
committed
made result of first and last methods more strict
1 parent f34d7e2 commit b17f285

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Types/CypherList.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
use function array_reduce;
2020
use function array_search;
2121
use function array_slice;
22+
use function array_sum;
2223
use ArrayIterator;
2324
use BadMethodCallException;
24-
use function array_sum;
2525
use function count;
2626
use function in_array;
27+
use function is_int;
2728
use Laudis\Neo4j\Contracts\CypherContainerInterface;
29+
use OutOfBoundsException;
2830
use function sort;
2931
use 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];

src/Types/CypherMap.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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]);

0 commit comments

Comments
 (0)