Skip to content

Commit

Permalink
Requested changes, static analysis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devnix committed Dec 17, 2023
1 parent 1a069eb commit ef5599a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/Psl/Option/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,18 @@ public function contains(mixed $value): bool
/**
* Matches the contained option value with the provided closures and returns the result.
*
* @template TNone
* @template TSome
* @template Ts
*
* @param (Closure(): TNone) $none A closure to be called when the option is none.
* The closure must not accept any arguments and can return a value.
* Example: `fn() => 'Default value'`
* @param (Closure(T): TSome) $some A closure to be called when the option is some.
* The closure must accept the option value as its only argument and can return a value.
* Example: `fn($value) => $value + 10`
* @param (Closure(T): Ts) $some A closure to be called when the option is some.
* The closure must accept the option value as its only argument and can return a value.
* Example: `fn($value) => $value + 10`
* @param (Closure(): Ts) $none A closure to be called when the option is none.
* The closure must not accept any arguments and can return a value.
* Example: `fn() => 'Default value'`
*
* @return TNone|TSome The result of calling the appropriate closure.
* @return Ts The result of calling the appropriate closure.
*/
public function match(Closure $none, Closure $some): mixed
public function proceed(Closure $some, Closure $none): mixed
{
if ($this->option !== null) {
return $some($this->option[0]);
Expand Down
20 changes: 20 additions & 0 deletions tests/static-analysis/Option/proceed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Psl\Option;

function proceed(): void
{
/**
* @param Option\Option<int> $option
* @return non-empty-string
*/
function test_proceed(Option\Option $option): string
{
return $option->proceed(
static fn (int $value) => "There is $value of them.",
static fn () => 'There are none.',
);
}
}
6 changes: 3 additions & 3 deletions tests/unit/Option/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testContains(): void
static::assertFalse($option->contains(4));
}

public function testMatch(): void
public function testProceed(): void
{
$checked_divisor = static function (int $dividend, int $divisor): Option\Option {
if ($divisor === 0) {
Expand All @@ -95,9 +95,9 @@ public function testMatch(): void
};

$try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string {
return $checked_divisor($dividend, $divisor)->match(
none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor),
return $checked_divisor($dividend, $divisor)->proceed(
some: static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value),
none: static fn() => sprintf('%s / %s failed!', $dividend, $divisor),
);
};

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Option/SomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testContains(): void
static::assertTrue($option->contains(2));
}

public function testMatch(): void
public function testProceed(): void
{
$checked_divisor = static function (int $dividend, int $divisor): Option\Option {
if ($divisor === 0) {
Expand All @@ -92,9 +92,9 @@ public function testMatch(): void
};

$try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string {
return $checked_divisor($dividend, $divisor)->match(
none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor),
return $checked_divisor($dividend, $divisor)->proceed(
some: static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value),
none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor),
);
};

Expand Down

0 comments on commit ef5599a

Please sign in to comment.