PHPackages                             trehinos/thor-option - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. trehinos/thor-option

Abandoned → [trehinos/thor-common](/?search=trehinos%2Fthor-common)Library[Utility &amp; Helpers](/categories/utility)

trehinos/thor-option
====================

An option-type for PHP 8.1 and above.

v1.0.2(2y ago)0432MITPHPPHP ^8.1

Since Feb 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Trehinos/thor-option)[ Packagist](https://packagist.org/packages/trehinos/thor-option)[ RSS](/packages/trehinos-thor-option/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Option type in PHP 8.1+
=======================

[](#option-type-in-php-81)

This package let you use an option (`Thor\Maybe\Option`) type in PHP to handle cases when a value can or can not take a value.

The API of this module is intensively inspired by Rust's [Option type](https://doc.rust-lang.org/std/option/).

Say goodbye to `null` values
----------------------------

[](#say-goodbye-to-null-values)

With `Thor\Maybe\Option`, you can wrap any value (including `null`) and will never take back a `null`.

Examples
--------

[](#examples)

### Playing with some data

[](#playing-with-some-data)

```
use Thor\Maybe\Option;
use Thor\Maybe\Maybe;

$myOption = Option::from("data...");
// Or
$myOption = Option::some("data...");

if ($myOption->isNone()) {
    // Never
}
// Or
if ($myOption->isA(Maybe::NONE)) {
    // Never
}

// Unwrap the optional value
if ($myOption->is() === Maybe::SOME) {
    // Here we know we can unwrap().
    $myString = $myOption->unwrap();
}

// Echoes the string if it is not none, or an empty string if it is :
echo $myOption->matches(
    fn(string $str) => $str,
    fn() => '',
);
// Or
echo $myOption->unwrapOr('');
```

### Handling `NONE` values

[](#handling-none-values)

```
use Thor\Maybe\Option;

$myOption = Option::from(null);
$myOption = Option::none();

$value = $myOption->unwrap(); // Throws a RuntimeException
$value = $myOption->unwrapOrThrow(new Exception("Custom Exception"));
$value = $myOption->unwrapOrElse(fn() => 'default value from callable');
$value = $myOption->unwrapOr('default value');
```

Reference
---------

[](#reference)

### Maybe enumeration

[](#maybe-enumeration)

- Case `SOME` to represent the case when an option contains some value,
- Case `NONE` to represent the absence of value in an option.

### Option

[](#option)

#### Constructors

[](#constructors)

- `Option::from(mixed $value)` : create a new option with some value or none if `$value` is null,
- `Option::some(mixed $value)` : create a new option with some value,
- `Option::none()` : create a new option with none.

#### Informational methods

[](#informational-methods)

- `$myOption->is()` : returns a `Maybe::SOME` or a `Maybe::NONE`,
- `$myOption->isNone()` : returns `true` if the option is none,
- `$myOption->isSome()` : returns `true` if the option is some,
- `$myOption->isA(Maybe $maybe)` : returns `true` if the option is corresponding the $maybe case.

#### Match

[](#match)

> Do something with the value if the Option contains a value, or do something else if the value is none.

```
use Thor\Maybe\Option;
use Thor\Maybe\Maybe;

$myOption = Option::some("data...");

$myOption->matches(
    fn(string $str) => "My Option is Some($str)",
    fn() => 'My Option is None...',
);
```

#### Unwrap methods

[](#unwrap-methods)

- `$value = $myOption->unwrap()` : throws a RuntimeException if the value of the option is none,
- `$value = $myOption->unwrapOrThrow(new Exception("Custom Exception"))` : throws the specified `Throwable` if the value of the option is none,
- `$value = $myOption->unwrapOrElse(fn() => 'default value from callable')` : executes the callable in parameter if the value of the option is none and returns its returned value,
- `$value = $myOption->unwrapOr('default value')` : returns the specified value if the value of the option is none.

License
-------

[](#license)

Copyright 2024 Sébastien GELDREICH

License MIT

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

857d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3908278?v=4)[Trehinos](/maintainers/Trehinos)[@Trehinos](https://github.com/Trehinos)

---

Top Contributors

[![Trehinos](https://avatars.githubusercontent.com/u/3908278?v=4)](https://github.com/Trehinos "Trehinos (13 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trehinos-thor-option/health.svg)

```
[![Health](https://phpackages.com/badges/trehinos-thor-option/health.svg)](https://phpackages.com/packages/trehinos-thor-option)
```

###  Alternatives

[keiko/uuid-shortener

A simple shortener library for RFC 4122 compatible UUIDs. Change your 36 chars long UUID into it's shorter equivalent.

150227.4k3](/packages/keiko-uuid-shortener)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
