···224224 * Replaces the actual value in the option by the value given in the parameter, returning the old value if present
225225 *
226226 * @template NT
227227+ *
227228 * @return Option<T>
228229 */
229229- public function replace(mixed $value)
230230+ public function replace(mixed $value): self
230231 {
231232 $old = clone $this;
233233+ if ($this->isNone() && $value !== null) {
234234+ $this->isSome = true;
235235+ }
232236 $this->value = $value;
237237+238238+ return $old;
239239+ }
240240+241241+ /*
242242+ * Takes the value out of the option, leaving a `None` in its place
243243+ *
244244+ * return Option<T>
245245+ */
246246+ public function take(): self
247247+ {
248248+ $old = clone $this;
249249+ $this->value = null;
250250+ $this->isSome = false;
251251+252252+ return $old;
253253+ }
254254+255255+ /*
256256+ * Takes the value out of the option, but only if the predicate evaluates to `true`.
257257+ *
258258+ * @param callable(T): bool $predicate
259259+ * @return Option<T>
260260+ */
261261+ public function takeIf(callable $predicate): Option
262262+ {
233263 if ($this->isNone()) {
234234- $this->isSome = true;
264264+ return self::None();
265265+ }
266266+ if ($predicate($this->value)) {
267267+ return $this->take();
235268 }
236269237237- return $old;
270270+ return self::None();
238271 }
239272}
-4
tests/OptionTest.php
···193193194194 public function test_take(): void
195195 {
196196- $this->markTestIncomplete('TODO');
197197-198196 $x = Option::Some(2);
199197 $y = $x->take();
200198 $this->assertTrue($x->isNone());
···209207210208 public function test_take_if(): void
211209 {
212212- $this->markTestIncomplete('TODO');
213213-214210 $x = Option::Some(42);
215211 $prev = $x->takeIf(function (&$v) {
216212 if ($v === 42) {