Page MenuHomeDevCentral

D3852.id9980.diff
No OneTemporary

D3852.id9980.diff

diff --git a/omnitools/src/Collections/WithCollection.php b/omnitools/src/Collections/WithCollection.php
--- a/omnitools/src/Collections/WithCollection.php
+++ b/omnitools/src/Collections/WithCollection.php
@@ -2,29 +2,28 @@
namespace Keruald\OmniTools\Collections;
+use Keruald\OmniTools\DataTypes\Option\None;
+use Keruald\OmniTools\DataTypes\Option\Option;
+use Keruald\OmniTools\DataTypes\Option\Some;
use Keruald\OmniTools\Reflection\CallableElement;
use InvalidArgumentException;
-use OutOfRangeException;
trait WithCollection {
abstract function count () : int;
abstract function toArray() : array;
- public function first () : mixed {
+ public function first () : Option {
foreach ($this->toArray() as $item) {
- return $item;
+ return new Some($item);
}
- throw new OutOfRangeException("The collection is empty.");
+ return new None;
}
public function firstOr (mixed $default) : mixed {
- return match ($this->count()) {
- 0 => $default,
- default => $this->first(),
- };
+ return $this->first()->getValueOr($default);
}
///
diff --git a/omnitools/tests/Collections/BitsVectorTest.php b/omnitools/tests/Collections/BitsVectorTest.php
--- a/omnitools/tests/Collections/BitsVectorTest.php
+++ b/omnitools/tests/Collections/BitsVectorTest.php
@@ -236,14 +236,16 @@
$bits = BitsVector::new(4);
$bits[2] = 1;
- $this->assertEquals(0, $bits->first());
+ $firstBit = $bits->first();
+ $this->assertTrue($firstBit->isSome());
+ $this->assertEquals(0, $firstBit->getValue());
}
public function testFirstWhenEmpty () : void {
$bits = BitsVector::new(0);
- $this->expectException(OutOfRangeException::class);
- $bits->first();
+ $firstBit = $bits->first();
+ $this->assertTrue($firstBit->isNone());
}
public function testFirstOr () : void {
diff --git a/omnitools/tests/Collections/HashMapTest.php b/omnitools/tests/Collections/HashMapTest.php
--- a/omnitools/tests/Collections/HashMapTest.php
+++ b/omnitools/tests/Collections/HashMapTest.php
@@ -530,14 +530,17 @@
///
public function testFirst () : void {
- $this->assertEquals("Iain Banks", $this->map->first());
+ $author = $this->map->first();
+
+ $this->assertTrue($author->isSome());
+ $this->assertEquals("Iain Banks", $author->getValue());
}
public function testFirstWhenEmpty () : void {
$map = new HashMap();
+ $author = $map->first();
- $this->expectException(OutOfRangeException::class);
- $map->first();
+ $this->assertTrue($author->isNone());
}
public function testFirstOr () : void {
diff --git a/omnitools/tests/Collections/VectorTest.php b/omnitools/tests/Collections/VectorTest.php
--- a/omnitools/tests/Collections/VectorTest.php
+++ b/omnitools/tests/Collections/VectorTest.php
@@ -433,14 +433,15 @@
///
public function testFirst () : void {
- $this->assertEquals(1, $this->vector->first());
+ $item = $this->vector->first();
+ $this->assertEquals(1, $item->getValue());
}
public function testFirstWhenEmpty () : void {
$vector = new Vector;
- $this->expectException(OutOfRangeException::class);
- $vector->first();
+ $item = $vector->first();
+ $this->assertTrue($item->isNone());
}
public function testFirstOr () : void {

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 12, 10:31 (17 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3156631
Default Alt Text
D3852.id9980.diff (3 KB)

Event Timeline