Skip to content

Option::fromArraysValue should have parameter $key typed as int|string (and possibly null|int|string) #73

Closed
@niconoe-

Description

@niconoe-

When looking at the current version (1.9.2), the PHPDoc of Option::fromArraysValue tells that parameter $key must be a string, while it could also be an int, in case we want to use Option::fromArraysValue with an indexed array rather than an associative one.

This would also make sense regarding the PHPDoc of the $array parameter of the exact same method.

If I may, I would like to go further and allow null for $key too, which would automatically leads to returning a None instance.

Let's have this:

$fooEntitites; // array<int, Entity>
$barEntity->foo_id; // int|null

// $barEntity->foo_id can be:
// - `null` if this Bar isn't bound to any Foo
// - `int` but not an existing key in $fooEntities for some reasons
// - `int` and existing key in $fooEntities

$foo = Option::fromArraysValue($fooEntities, $barEntity->foo_id)->getOrElse(null);

This last line is currently forbidden to me by PHPStan/Psalm because of the typehint not being aligned with your current version of your library.

I will try to provide a PR if you're interested in.

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions