Skip to content

Commit 4074471

Browse files
committed
Update Some.php
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
1 parent ce049e1 commit 4074471

File tree

1 file changed

+45
-88
lines changed

1 file changed

+45
-88
lines changed

src/Some.php

+45-88
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
namespace Ghostwriter\Option;
66

7-
use Closure;
8-
use Generator;
97
use Ghostwriter\Option\Exception\NullPointerException;
10-
use Ghostwriter\Option\Exception\OptionException;
118
use Ghostwriter\Option\Exception\ShouldNotHappenException;
129
use Ghostwriter\Option\Interface\NoneInterface;
1310
use Ghostwriter\Option\Interface\OptionInterface;
@@ -16,7 +13,7 @@
1613
use Tests\Unit\SomeTest;
1714
use Throwable;
1815

19-
use function is_iterable;
16+
use function get_debug_type;
2017
use function sprintf;
2118

2219
/**
@@ -28,10 +25,8 @@
2825
*/
2926
final readonly class Some implements SomeInterface
3027
{
31-
/**
32-
* @param TSome $value
33-
*/
34-
public function __construct(
28+
/** @param TSome $value */
29+
private function __construct(
3530
private mixed $value
3631
) {}
3732

@@ -42,19 +37,17 @@ public function __construct(
4237
*
4338
* @throws NullPointerException
4439
*
45-
* @return SomeInterface<TNew>
40+
* @return (TNew is SomeInterface ? TNew : SomeInterface<TNew>)
4641
*/
4742
#[Override]
4843
public static function new(mixed $value): SomeInterface
4944
{
5045
return match (true) {
51-
null === $value => throw new NullPointerException(),
46+
$value instanceof NoneInterface, null === $value => throw new NullPointerException(),
5247

5348
$value instanceof SomeInterface => $value,
5449

55-
default => /** @var SomeInterface<TNew> */ new self($value),
56-
57-
$value instanceof NoneInterface => throw new ShouldNotHappenException(),
50+
default => new self($value),
5851
};
5952
}
6053

@@ -64,8 +57,11 @@ public function and(OptionInterface $option): OptionInterface
6457
return $option;
6558
}
6659

60+
/**
61+
* @throws ShouldNotHappenException
62+
*/
6763
#[Override]
68-
public function andThen(Closure $function): OptionInterface
64+
public function andThen(callable $function): OptionInterface
6965
{
7066
/** @var null|OptionInterface<TSome> $result */
7167
$result = $function($this->value);
@@ -74,63 +70,62 @@ public function andThen(Closure $function): OptionInterface
7470
return $result;
7571
}
7672

77-
throw new OptionException(sprintf(
78-
'Closure passed to andThen() must return an instance of %s.',
73+
throw new ShouldNotHappenException(sprintf(
74+
'Callable passed to andThen() must return an instance of %s.',
7975
OptionInterface::class
8076
));
8177
}
8278

79+
/** @throws Throwable */
8380
#[Override]
84-
public function contains(mixed $value): bool
81+
public function expect(Throwable $throwable): mixed
8582
{
86-
if ($this instanceof NoneInterface) {
87-
return false;
88-
}
89-
90-
return $this->value === $value;
83+
return $this->value;
9184
}
9285

9386
/**
94-
* @throws Throwable
87+
* @throws ShouldNotHappenException
9588
*/
9689
#[Override]
97-
public function expect(Throwable $throwable): mixed
90+
public function filter(callable $function): OptionInterface
9891
{
99-
if ($this instanceof NoneInterface) {
100-
throw $throwable;
101-
}
92+
$result = $function($this->value);
10293

103-
return $this->value;
94+
return match ($result) {
95+
true => $this,
96+
false => None::new(),
97+
default => throw new ShouldNotHappenException(sprintf(
98+
'Callable passed to filter() must return a boolean, %s given.',
99+
get_debug_type($result)
100+
)),
101+
};
104102
}
105103

104+
/**
105+
* @throws NullPointerException
106+
*/
106107
#[Override]
107-
public function filter(Closure $function): OptionInterface
108+
public function get(): mixed
108109
{
109-
return match (true) {
110-
$function($this->value) => $this,
111-
default => None::new()
112-
};
110+
return $this->value;
113111
}
114112

115113
#[Override]
116-
public function flatten(): OptionInterface
114+
public function getOr(mixed $fallback): mixed
117115
{
118-
return match (true) {
119-
$this->value instanceof SomeInterface => $this->value,
120-
default => $this
121-
};
116+
return $this->value;
122117
}
123118

124119
#[Override]
125-
public function getIterator(): Generator
120+
public function getOrElse(callable $function): mixed
126121
{
127-
$value = $this->value;
122+
return $this->value;
123+
}
128124

129-
if (is_iterable($value)) {
130-
yield from $value;
131-
} else {
132-
yield $value;
133-
}
125+
#[Override]
126+
public function is(mixed $value): bool
127+
{
128+
return $this->value === $value;
134129
}
135130

136131
#[Override]
@@ -146,27 +141,19 @@ public function isSome(): bool
146141
}
147142

148143
#[Override]
149-
public function map(Closure $function): OptionInterface
144+
public function map(callable $function): OptionInterface
150145
{
151-
$value = $function($this->value);
152-
153-
return match (true) {
154-
$value instanceof OptionInterface => $value,
155-
156-
null === $value => None::new(),
157-
158-
default => self::new($value),
159-
};
146+
return Option::new($function($this->value));
160147
}
161148

162149
#[Override]
163-
public function mapOr(Closure $function, mixed $fallback): mixed
150+
public function mapOr(callable $function, mixed $fallback): mixed
164151
{
165152
return $function($this->value);
166153
}
167154

168155
#[Override]
169-
public function mapOrElse(Closure $function, Closure $fallback): mixed
156+
public function mapOrElse(callable $function, callable $fallback): mixed
170157
{
171158
return $function($this->value);
172159
}
@@ -178,38 +165,8 @@ public function or(OptionInterface $option): OptionInterface
178165
}
179166

180167
#[Override]
181-
public function orElse(Closure $function): OptionInterface
168+
public function orElse(callable $function): OptionInterface
182169
{
183170
return $this;
184171
}
185-
186-
#[Override]
187-
public function get(): mixed
188-
{
189-
return $this->value;
190-
}
191-
192-
#[Override]
193-
public function getOr(mixed $fallback): mixed
194-
{
195-
return $this->value;
196-
}
197-
198-
#[Override]
199-
public function getOrElse(Closure $function): mixed
200-
{
201-
return $this->value;
202-
}
203-
204-
#[Override]
205-
public static function nullable(mixed $value): OptionInterface
206-
{
207-
return match (true) {
208-
$value instanceof OptionInterface => $value,
209-
210-
null === $value => None::new(),
211-
212-
default => self::new($value),
213-
};
214-
}
215172
}

0 commit comments

Comments
 (0)