Skip to content

Commit 90fa1ae

Browse files
committed
[Functional] deprecate cond and condLazy. Option::when and Option::unless have been added.
1 parent 5d534bf commit 90fa1ae

File tree

9 files changed

+74
-10
lines changed

9 files changed

+74
-10
lines changed

src/Fp/Collections/ArrayList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ public function toArrayList(): ArrayList
146146
*/
147147
public function toNonEmptyArrayList(): Option
148148
{
149-
return Option::cond(
149+
$that = $this;
150+
return Option::when(
150151
$this->isNonEmpty(),
151-
new NonEmptyArrayList($this)
152+
fn() => new NonEmptyArrayList($that)
152153
);
153154
}
154155

src/Fp/Collections/LinkedList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public function toNonEmptyArrayList(): Option
144144
{
145145
$arrayList = $this->toArrayList();
146146

147-
return Option::cond(
147+
return Option::when(
148148
$arrayList->isNonEmpty(),
149-
new NonEmptyArrayList($arrayList)
149+
fn() => new NonEmptyArrayList($arrayList)
150150
);
151151
}
152152

src/Fp/Collections/NonEmptyArrayList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function collect(iterable $source): Option
5454
{
5555
$arrayList = ArrayList::collect($source);
5656

57-
return Option::condLazy(
57+
return Option::when(
5858
$arrayList->isNonEmpty(),
5959
fn() => new self($arrayList)
6060
);

src/Fp/Collections/NonEmptyHashMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function collectPairs(iterable $source): Option
9292

9393
$isEmpty = empty($hashTable->table);
9494

95-
return Option::condLazy(!$isEmpty, fn() => new HashMap($hashTable))
95+
return Option::when(!$isEmpty, fn() => new HashMap($hashTable))
9696
->map(fn(HashMap $map) => new self($map));
9797
}
9898

src/Fp/Collections/NonEmptyHashSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(private HashSet $set)
4848
public static function collect(iterable $source): Option
4949
{
5050
$hashset = HashSet::collect($source);
51-
return Option::condLazy(!$hashset->isEmpty(), fn() => new self($hashset));
51+
return Option::when(!$hashset->isEmpty(), fn() => new self($hashset));
5252
}
5353

5454
/**

src/Fp/Functional/Option/Option.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ public function toRight(callable $left): Either
617617
}
618618

619619
/**
620+
* @deprecated use {@see Option::when}
621+
*
620622
* Fabric method.
621623
*
622624
* ```php
@@ -640,6 +642,8 @@ public static function cond(bool $condition, mixed $some): Option
640642
}
641643

642644
/**
645+
* @deprecated use {@see Option::when}
646+
*
643647
* Fabric method.
644648
*
645649
* ```php
@@ -665,6 +669,56 @@ public static function condLazy(bool $condition, callable $some): Option
665669
: self::none();
666670
}
667671

672+
/**
673+
* Fabric method.
674+
*
675+
* ```php
676+
* >>> Option::when(true, fn() => 1);
677+
* => Some(1)
678+
*
679+
* >>> Option::when(false, fn() => 1);
680+
* => None
681+
* ```
682+
*
683+
* Create {@see Some} from value if given condition is true
684+
* Create {@see None} if given condition is false
685+
*
686+
* @psalm-pure
687+
* @psalm-template AI
688+
* @psalm-param pure-callable(): AI $callback
689+
* @psalm-return Option<AI>
690+
*/
691+
public static function when(bool $condition, callable $callback): Option
692+
{
693+
return $condition
694+
? self::some($callback())
695+
: self::none();
696+
}
697+
698+
/**
699+
* Fabric method.
700+
*
701+
* ```php
702+
* >>> Option::unless(false, fn() => 1);
703+
* => Some(1)
704+
*
705+
* >>> Option::unless(true, fn() => 1);
706+
* => None
707+
* ```
708+
*
709+
* Create {@see Some} from value if given condition is false
710+
* Create {@see None} if given condition is true
711+
*
712+
* @psalm-pure
713+
* @psalm-template AI
714+
* @psalm-param pure-callable(): AI $callback
715+
* @psalm-return Option<AI>
716+
*/
717+
public static function unless(bool $condition, callable $callback): Option
718+
{
719+
return self::when(!$condition, $callback);
720+
}
721+
668722
/**
669723
* Convert optional value to ArrayList collection
670724
* via transformation function $callback.

src/Fp/Functions/Collection/At.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function at(iterable $collection, int|string $key): Option
3030
{
3131
return Option::some($collection)
3232
->filter(fn($coll) => is_array($coll))
33-
->flatMap(fn(array $coll) => Option::condLazy(array_key_exists($key, $coll), fn() => $coll[$key]))
33+
->flatMap(fn(array $coll) => Option::when(array_key_exists($key, $coll), fn() => $coll[$key]))
3434
->orElse(fn() => AtOperation::of($collection)($key));
3535
}
3636

src/Fp/Streams/Stream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,9 @@ public function toNonEmptyArrayList(): Option
667667
{
668668
$arrayList = $this->leaf(ArrayList::collect($this->emitter));
669669

670-
return Option::cond(
670+
return Option::when(
671671
$arrayList->isNonEmpty(),
672-
new NonEmptyArrayList($arrayList)
672+
fn() => new NonEmptyArrayList($arrayList)
673673
);
674674
}
675675

tests/Runtime/Classes/Option/OptionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ public function testCond(): void
187187
$this->assertNull(Option::cond(false, 'some')->get());
188188
}
189189

190+
public function testWhenAndUnless(): void
191+
{
192+
$this->assertEquals('some', Option::when(true, fn() => 'some')->get());
193+
$this->assertNull(Option::when(false, fn() => throw new Error())->get());
194+
195+
$this->assertEquals('some', Option::unless(false, fn() => 'some')->get());
196+
$this->assertNull(Option::unless(true, fn() => throw new Error())->get());
197+
}
198+
190199
public function testTap(): void
191200
{
192201
$this->assertEquals(1, Option::some(1)->tap(fn($e) => $e)->get());

0 commit comments

Comments
 (0)