fp4php/functional

Option::do implementation incorrect

klimick opened this issue · 1 comments

This snippet type checks by psalm, but at runtime it fails:

/**
 * @return Option<stdClass>
 */
function toSomething(mixed $_): Option
{
    return Option::none();
}

$items = [];

Option::do(function() use ($items) {
    $mapped = [];

    /** @psalm-suppress MixedAssignment */
    foreach ($items as $item) {
        $mapped[] = yield toSomething($item);
    }

    return $mapped;
});
PHP Fatal error:  Uncaught Error: Call to a member function isEmpty() on null in /home/app/vendor/whsv26/functional/src/Fp/Functional/Option/Option.php:82
Stack trace:
#0 /home/app/run.php(26): Fp\Functional\Option\Option::do()
#1 {main}
  thrown in /home/app/vendor/whsv26/functional/src/Fp/Functional/Option/Option.php on line 82

Process finished with exit code 255

This only happens if no yield has been called in Option::do.

fixed