PHPackages                             skd/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. skd/result

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

skd/result
==========

Implementing the Result Object pattern in PHP

v0.1(2y ago)07MITPHPPHP &gt;=8.1

Since Jan 1Pushed 2y ago1 watchersCompare

[ Source](https://github.com/dev-skvortsov/result)[ Packagist](https://packagist.org/packages/skd/result)[ RSS](/packages/skd-result/feed)WikiDiscussions 0.x Synced 1mo ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

The Result object
=================

[](#the-result-object)

This package allows you to work with the results of operations, including errors, in an object-oriented style, without using exceptions. For example, when using the 'Always valid domain model' approach. Result object allows you to accumulate errors and return them all at once ([Notification](https://martinfowler.com/eaaDev/Notification.html) pattern is inside). At the same time, the code becomes more understandable and logical.

Installation
------------

[](#installation)

```
composer require skd/result
```

Result
------

[](#result)

The Result class is an immutable generic class (using `@template` annotation) with 2 states: `Ok` and `Error`. The `OK` state means that there are no errors inside and there is some value (success operation result). The `Error` state means that an error or several errors were received during the operation.

Use `@return Result` annotation to tell the IDE and static analysis tool what type value is inside;

### Ok state

[](#ok-state)

Use static method `Result::ok` to return success operation result with some value inside

```
use Skd\Result\Result;

// some code
return Skd\Result\Result::ok($value);
```

Use method `Result::getValue(): T` to get the value. Note that calling this method on Error state will throw an exception. To prevent that you have to check the result state by calling one of methods `Result::isOk(): bool` or `Result::isError(): bool` before.

```
if($result->isOk()) {
    $value = $result->getValue();
}
```

### Error state

[](#error-state)

Use static method `Result:error(Notification $errors)` to return result with an error (errors). The non-empty [Notification](#notification)object must be passed as an argument. Passing an empty Notification object (no errors inside) will throw an exception.

```
use Skd\Result\Result;
use Skd\Result\Error\Notification;

// some code

$error = new Error(...);
$errors = new Notification($error);

return Skd\Result\Result::error($errors);
```

Use method `Result::getErrors(): Notification` to get an error (errors). Note that calling that method on `Ok` state will throw an exception. You can't change Notification object after Result object is initialized

```
$result = Result::error(new Notification(...))

// $errors here is a cloned copy, you cannot change the list of errors in the $result object
$errors = $result->getErrors();
```

Notification
------------

[](#notification)

Notification class is a Notification pattern realisation. It can be initialized in two ways: as an empty list or a list with one error inside

```
use Skd\Result\Error\Notification;
use Skd\Result\Error\Error;

// empty
$emptyNotification = new Notification();

// with one error
$error = new Error('code', 'message');
$notificationWithOneError = new Notification($error);
```

Use method `Notification::hasErrors(): bool` to check if there are error/errors inside the notification object

```
use Skd\Result\Error\Notification;

$errors = new Notification();
$errors->hasErrors(); // false

$errors = new Notification($error);
$errors->hasErrors(); // true
```

Use method `Notification::hasError(Error $error): bool` to check if the specific error is inside the notification object. It can be useful in Unit tests.

```
use Skd\Result\Error\Notification;

$errors = new Notification($error);

$errors->hasError($error); // true
$errors->hasError($anotherError); // false
```

### Errors Accumulating

[](#errors-accumulating)

Errors can be accumulated in a Notification object.

Use `Notification::addError(Error $error): void` to add some error in the list

```
use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;

$errors = new Notification();
// it can be initialized in both ways
$errors = new Notification($someError);

$errors->addError($anotherError);
```

Notification objects can be merged as well. It can be useful when you have to accumulate two or more results with errors. Note that only left object will be changed.

```
use Skd\Result\Error\Notification;

$notification = new Notification($error);
$anotherNotification = new Notification($anotherError);

$notification->merge($anotherNotification);

$notification->hasError($error); // true
$notification->hasError($anotherError); // true

// but
$anotherNotification->hasError($error); // false
```

### Example:

[](#example)

#### Some Value Object class

[](#some-value-object-class)

```
use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;
use Skd\Result\Result;

class SomeClass
{
    private function __construct(public readonly mixed $someField)
    {
    }

    /**
     * @return Result
     */
    public static function create(mixed $someValue): Result
    {
        $errors = new Notification();

        // some validation
        if (...) {
            $errors->addError();
        }

        // another validation (multiple errors)
        if (...) {
            $errors->addError(new Error('anotherCode', 'Another error message'));
        }

        if ($errors->hasErrors()) {
            return Result::error($errors)
        }

        return Result::ok(new self($someValue));
    }
}
```

You can replace `new Error('code', 'Error message')` with static factory:

```
final class SomeErrorsFactory
{
  public static function invalidValue(): DomainError
  {
    return new Error('code', 'Error message');
  }
}
```

#### Unit tests

[](#unit-tests)

```
public function testInvalidValue(): void
{
  $result = SomeClass::create($invalidValue);

  $this->assertTrue($result->isError());
  $this->assertTrue($result->getErrors()->hasError(SomeErrorsFactory::invalidValue()));
}
```

```

```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% 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

868d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0149e78ab17ee96247fc93e5a3560e6db7a76a49e641db1135f07e357fc885ab?d=identicon)[dev-skvortsov](/maintainers/dev-skvortsov)

---

Top Contributors

[![devkvortsov](https://avatars.githubusercontent.com/u/110227745?v=4)](https://github.com/devkvortsov "devkvortsov (9 commits)")[![dev-skvortsov](https://avatars.githubusercontent.com/u/94625725?v=4)](https://github.com/dev-skvortsov "dev-skvortsov (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/skd-result/health.svg)

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

###  Alternatives

[patricktalmadge/bootstrapper

Twitter Bootstrap markup generator

557407.2k4](/packages/patricktalmadge-bootstrapper)

PHPackages © 2026

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