From 26ccafa38a88cdfd9f025faa8d114ac0e26225b3 Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Wed, 18 Oct 2023 16:19:44 +0200 Subject: [PATCH 1/7] feat(option): new `Option::apply()` method --- src/Psl/Option/Option.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Psl/Option/Option.php b/src/Psl/Option/Option.php index 3f60962c..8535139c 100644 --- a/src/Psl/Option/Option.php +++ b/src/Psl/Option/Option.php @@ -227,6 +227,22 @@ public function proceed(Closure $some, Closure $none): mixed return $none(); } + /** + * Applies a function to a contained value and returns the original `Option`. + * + * @param (Closure(T): void) $closure + * + * @return Option + */ + public function apply(Closure $closure): Option + { + if ($this->option !== null) { + $closure($this->option[0]); + } + + return $this; + } + /** * Maps an `Option` to `Option` by applying a function to a contained value. * From a2d93983932492ca82981c1be955dad452eacd0e Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Mon, 11 Dec 2023 17:14:56 +0100 Subject: [PATCH 2/7] Add unit tests --- tests/unit/Option/NoneTest.php | 12 ++++++++++++ tests/unit/Option/SomeTest.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tests/unit/Option/NoneTest.php b/tests/unit/Option/NoneTest.php index 43481f31..ca764589 100644 --- a/tests/unit/Option/NoneTest.php +++ b/tests/unit/Option/NoneTest.php @@ -95,6 +95,18 @@ public function testProceed(): void static::assertSame('There is no value', $result); } + public function testApply(): void + { + $i = 1; + + $option = Option\none(); + $option->apply(static function (int $value) use (&$i) { + $i += $value; + }); + + static::assertSame(1, $i); + } + public function testMap(): void { $option = Option\none(); diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index 3c0d2c3e..df77a897 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -94,6 +94,18 @@ public function testProceed(): void static::assertSame('Value is 1', $result); } + public function testApply(): void + { + $i = 1; + + $option = Option\some(2); + $option->apply(static function (int $value) use (&$i) { + $i += $value; + }); + + static::assertSame(3, $i); + } + public function testMap(): void { $option = Option\some(2); From dd53b9c81afb67b01223879cff87d8329ef0946d Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Mon, 11 Dec 2023 19:55:36 +0100 Subject: [PATCH 3/7] Requested changes --- tests/unit/Option/NoneTest.php | 10 ++++++---- tests/unit/Option/SomeTest.php | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/unit/Option/NoneTest.php b/tests/unit/Option/NoneTest.php index ca764589..6b5ec0cc 100644 --- a/tests/unit/Option/NoneTest.php +++ b/tests/unit/Option/NoneTest.php @@ -11,6 +11,7 @@ use Psl\Option; use Psl\Option\Exception\NoneException; use Psl\Str; +use Psl\Ref; final class NoneTest extends TestCase { @@ -97,14 +98,15 @@ public function testProceed(): void public function testApply(): void { - $i = 1; + $spy = new Ref(1); $option = Option\none(); - $option->apply(static function (int $value) use (&$i) { - $i += $value; + $option->apply(static function (int $value) use ($spy) { + $spy->value += $value; }); - static::assertSame(1, $i); + static::assertSame(1, $spy->value); + static::assertTrue($option->isNone()); } public function testMap(): void diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index df77a897..4b33e51d 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -10,6 +10,7 @@ use Psl\Comparison\Order; use Psl\Option; use Psl\Str; +use Psl\Ref; use Psl\Tests\Fixture; use Psl\Type; @@ -96,14 +97,15 @@ public function testProceed(): void public function testApply(): void { - $i = 1; + $spy = new Ref(1); $option = Option\some(2); - $option->apply(static function (int $value) use (&$i) { - $i += $value; + $option->apply(static function (int $value) use ($spy) { + $spy->value += $value; }); - static::assertSame(3, $i); + static::assertSame(3, $spy->value); + static::assertSame(2, $option->unwrap()); } public function testMap(): void From 00f8a2a9cc27a4dae558fac981ea06b01b19a28c Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Tue, 12 Dec 2023 16:22:53 +0100 Subject: [PATCH 4/7] Verify in unit test that a `None` option remains the same after calling `apply` --- tests/unit/Option/NoneTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/Option/NoneTest.php b/tests/unit/Option/NoneTest.php index 6b5ec0cc..d5aa52ef 100644 --- a/tests/unit/Option/NoneTest.php +++ b/tests/unit/Option/NoneTest.php @@ -101,12 +101,12 @@ public function testApply(): void $spy = new Ref(1); $option = Option\none(); - $option->apply(static function (int $value) use ($spy) { + $actual = $option->apply(static function (int $value) use ($spy) { $spy->value += $value; }); static::assertSame(1, $spy->value); - static::assertTrue($option->isNone()); + static::assertSame($option, $actual); } public function testMap(): void From 098c5b1dffb4e35cb2141cf16d8e75dd28e6fc8f Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Thu, 28 Dec 2023 19:34:50 +0100 Subject: [PATCH 5/7] Rewrote test with `$actual` --- tests/unit/Option/SomeTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index 4b33e51d..5d37ad28 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -98,14 +98,15 @@ public function testProceed(): void public function testApply(): void { $spy = new Ref(1); + $actual = 2; - $option = Option\some(2); + $option = Option\some($actual); $option->apply(static function (int $value) use ($spy) { $spy->value += $value; }); static::assertSame(3, $spy->value); - static::assertSame(2, $option->unwrap()); + static::assertSame($actual, $option->unwrap()); } public function testMap(): void From 3d78ebae03de33cde847d68d8fdb4d924675de16 Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Fri, 29 Dec 2023 11:09:29 +0100 Subject: [PATCH 6/7] Check that returned Option from `apply` is actually the same instance and value --- tests/unit/Option/SomeTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index 5d37ad28..06238195 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -98,15 +98,14 @@ public function testProceed(): void public function testApply(): void { $spy = new Ref(1); - $actual = 2; - $option = Option\some($actual); - $option->apply(static function (int $value) use ($spy) { + $option = Option\some(2); + $actual = $option->apply(static function (int $value) use ($spy) { $spy->value += $value; }); static::assertSame(3, $spy->value); - static::assertSame($actual, $option->unwrap()); + static::assertSame($actual, $option); } public function testMap(): void From d61e7a7c7ab177ede8438813059d8459da7800ef Mon Sep 17 00:00:00 2001 From: Pablo Largo Mohedano Date: Fri, 29 Dec 2023 11:18:13 +0100 Subject: [PATCH 7/7] Fix coding standards --- tests/unit/Option/NoneTest.php | 2 +- tests/unit/Option/SomeTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/Option/NoneTest.php b/tests/unit/Option/NoneTest.php index d5aa52ef..57c8372f 100644 --- a/tests/unit/Option/NoneTest.php +++ b/tests/unit/Option/NoneTest.php @@ -10,8 +10,8 @@ use Psl\Comparison\Order; use Psl\Option; use Psl\Option\Exception\NoneException; -use Psl\Str; use Psl\Ref; +use Psl\Str; final class NoneTest extends TestCase { diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index 06238195..26240c6b 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -9,8 +9,8 @@ use Psl\Comparison\Equable; use Psl\Comparison\Order; use Psl\Option; -use Psl\Str; use Psl\Ref; +use Psl\Str; use Psl\Tests\Fixture; use Psl\Type;