Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12559367
D3852.id9979.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D3852.id9979.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 11, 19:49 (11 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3156631
Default Alt Text
D3852.id9979.diff (3 KB)
Attached To
Mode
D3852: BaseCollection::first now returns an Option
Attached
Detach File
Event Timeline
Log In to Comment