PHPackages                             jonathanr/php-optional-result - 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. jonathanr/php-optional-result

ActiveLibrary

jonathanr/php-optional-result
=============================

1.0.6(1y ago)110MITPHPPHP &gt;=8.2

Since Jul 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Jonathanrecly/PhpOptionalResult)[ Packagist](https://packagist.org/packages/jonathanr/php-optional-result)[ RSS](/packages/jonathanr-php-optional-result/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (8)Used By (0)

About PHPOptionalResult
-----------------------

[](#about-phpoptionalresult)

This package is an implementation of Rust return response.

Usage
-----

[](#usage)

*Look at examples directory*

### Optional response

[](#optional-response)

When need return value that can be nullable, you can use `Option::class`

#### Return with value :

[](#return-with-value-)

- `Option::some('AnyValue')`
- `Option::some(10)`
- `Option::some(['one', 'two', 'three'])`

You can replace parameter by any value you need.

#### No return value :

[](#no-return-value-)

- `Option::none()`

#### Methods allowed :

[](#methods-allowed-)

$value = `Option::some('any value'')`

`$value->isSome()` return boolean `$value->isNone()` return boolean

#### Get() method

[](#get-method)

- Get method return value when it's a Some return
- Get method throw NoneException when it's a None return

### Result Response

[](#result-response)

When need return an object that explain if process work great or had error, you can use `Result::class`

#### Return OK with value

[](#return-ok-with-value)

- `Result::ok('anyValue')`
- `Result::ok(10)`
- `Result::ok([1, 2, 3])`

You can replace parameter by any value you need, or by `Option::class`

##### Return Error with message

[](#return-error-with-message)

- `Result::err('an error occur')`

#### Get Method

[](#get-method-1)

- Get method return value when it's Ok return
- Get method error value when it's Err return

To get error message, getError() method is available

Example (*see examples directory*)
----------------------------------

[](#example-see-examples-directory)

### Before :

[](#before-)

```

/**
* @return string
*/
function askDomain(string $name): ?String
{
  try {
      $json = Http::get('myResolverApi?name='.$name)->json();
      if (isset($json['domain']) && ($json['domain'] !== null || $json['domain'] !== '')) {
      return $json['domain'];
      }
      return null;

  } catch (HttpException) {
    throw new Exception('API No respond');
  }
}

function isDomainExist(string $name): string
{
    try {
        $result = askDomain($name);

        if (!empty($result)) {
            return 'domain is' . $result;
        }

    } catch (Exception $e) {
        return '';
    }
}

echo isDomainExist('goooooooooooogle');

```

### After :

[](#after-)

```

/**
* @return Ok|Err
*/
function askDomain(string $name): Ok|Err
{
    try {
        $json = Http::get('myResolverApi?name='.$name)->json();
        if (isset($json['domain'])) {
            return Result::ok(Option::some($json['domain']));
        }

       return Result::ok(Option::none());

    } catch (HttpException) {
    return Result::Err(Option::none());
    }
}

function isDomainExist(string $name): string
{
    $result = askDomain($name);

    if ($result->isErr()) {
        return 'Not sure. API not respond';
    }

    if ($result->isOk() && $result->get()->isSome()) {
        return 'domain is'.$result->get()->isSome()->get();
    }

    return 'domain does not exist';
}

echo isDomainExist('goooooooooooogle');

```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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

7

Last Release

657d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

1.0.3PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/deefca6ba561828c27566965222ba3a46fb105e0ca2f18e2685e360957061365?d=identicon)[Jonathanrecly](/maintainers/Jonathanrecly)

---

Top Contributors

[![Jonathanrecly](https://avatars.githubusercontent.com/u/55309200?v=4)](https://github.com/Jonathanrecly "Jonathanrecly (24 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jonathanr-php-optional-result/health.svg)

```
[![Health](https://phpackages.com/badges/jonathanr-php-optional-result/health.svg)](https://phpackages.com/packages/jonathanr-php-optional-result)
```

PHPackages © 2026

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