| Conditions | 6 |
| Paths | 5 |
| Total Lines | 19 |
| Code Lines | 10 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 10 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | function iterable_last(iterable $iterable, bool $required = false) |
||
|
|
|||
| 15 | { |
||
| 16 | 8 | if (is_array($iterable) && $iterable !== []) { |
|
| 17 | 1 | return end($iterable); |
|
| 18 | } |
||
| 19 | |||
| 20 | 7 | $last = null; |
|
| 21 | 7 | $empty = true; // because $last can be any value including null. |
|
| 22 | |||
| 23 | 7 | foreach ($iterable as $value) { |
|
| 24 | 5 | $last = $value; |
|
| 25 | 5 | $empty = false; |
|
| 26 | } |
||
| 27 | |||
| 28 | 7 | if ($empty && $required) { |
|
| 29 | 1 | throw new \RangeException("Unable to get last element; iterable is empty"); |
|
| 30 | } |
||
| 31 | |||
| 32 | 6 | return $last; |
|
| 33 | } |
||
| 34 |