arielespinoza07/result-pattern

A modern PHP 8.3+ implementation of the Result pattern for handling operation outcomes and HTTP responses

v2.0.0 2025-06-10 19:16 UTC

This package is auto-updated.

Last update: 2025-06-10 19:16:22 UTC


README

Build Status Total Downloads Latest Stable Version License

A modern and simple PHP 8.3+ implementation of the Result pattern for handling operation outcomes and HTTP responses.

Features

  • 🛡️ Type-safe result handling with strict type hints
  • 🌐 Complete HTTP response status codes support (1xx to 5xx)
  • 🔒 Immutable objects using PHP 8.3+ readonly classes
  • 🎯 SOLID principles adherence
  • 🧩 Composable and extensible design
  • 📝 Comprehensive test suite with Pest PHP

Project Structure

src/
├── Enums/
│   └── HttpResponseStatusCode.php
├── Failure.php
├── Success.php
└── Result.php

Installation

composer require arielespinoza07/result-pattern

Documentation

The following documentation files provide examples and usage patterns for the Result pattern:

Available Response Codes

Informational Responses (1xx)

  • Continue (100)
  • SwitchingProtocols (101)
  • Processing (102)
  • EarlyHints (103)

Success Responses (2xx)

  • Ok (200)
  • Created (201)
  • Accepted (202)
  • NonAuthoritativeInformation (203)
  • NoContent (204)
  • ResetContent (205)
  • PartialContent (206)
  • MultiStatus (207)
  • AlreadyReported (208)
  • ImUsed (226)

Redirection Responses (3xx)

  • MultipleChoices (300)
  • MovedPermanently (301)
  • Found (302)
  • SeeOther (303)
  • NotModified (304)
  • UseProxy (305)
  • TemporaryRedirect (307)
  • PermanentRedirect (308)

Client Error Responses (4xx)

  • BadRequest (400)
  • Unauthorized (401)
  • PaymentRequired (402)
  • Forbidden (403)
  • NotFound (404)
  • MethodNotAllowed (405)
  • NotAcceptable (406)
  • ProxyAuthenticationRequired (407)
  • RequestTimeout (408)
  • Conflict (409)
  • Gone (410)
  • LengthRequired (411)
  • PreconditionFailed (412)
  • PayloadTooLarge (413)
  • UriTooLong (414)
  • UnsupportedMediaType (415)
  • RangeNotSatisfiable (416)
  • ExpectationFailed (417)
  • ImATeapot (418)
  • MisdirectedRequest (421)
  • UnprocessableEntity (422)
  • Locked (423)
  • FailedDependency (424)
  • TooEarly (425)
  • UpgradeRequired (426)
  • PreconditionRequired (428)
  • TooManyRequests (429)
  • RequestHeaderFieldsTooLarge (431)
  • UnavailableForLegalReasons (451)

Server Error Responses (5xx)

  • InternalServerError (500)
  • NotImplemented (501)
  • BadGateway (502)
  • ServiceUnavailable (503)
  • GatewayTimeout (504)
  • HttpVersionNotSupported (505)
  • VariantAlsoNegotiates (506)
  • InsufficientStorage (507)
  • LoopDetected (508)
  • NotExtended (510)
  • NetworkAuthenticationRequired (511)

Development

Requirements

  • PHP 8.3+
  • Composer 2.0+

Quality Tools

This package uses several tools to ensure code quality:

# Run all checks
composer test

# Run specific checks
composer test:lint     # Check code style
composer test:types    # Run static analysis
composer test:unit     # Run unit tests
composer test:coverage # Check test coverage

Continuous Integration

GitHub Actions automatically run the following checks on push and pull requests:

  • Static analysis with PHPStan (level max)
  • Unit tests with Pest PHP
  • Code style with Laravel Pint
  • Type coverage check
  • Typo check with Peck

Development

Requirements

  • PHP 8.3 or higher
  • Composer 2.0 or higher

Installation

composer require arielespinoza07/result-pattern

Development Installation

git https://github.com/ArielEspinoza07/result-pattern.git
cd result-pattern
composer install

Testing

This package uses Pest PHP for testing. To run the tests:

composer test

To generate a coverage report:

composer test:coverage

The coverage report will be available in the coverage directory.

Development Tools

This package uses several development tools: to ensure code quality and maintainability:

Code Quality Tools

  • Pest PHP: Modern Testing Framework with custom expectations

    composer test           # Run tests
    composer test:coverage  # Run tests with coverage report
  • Laravel Pint: PSR-12 Code Style Fixer

    composer pint      # Fix code style
    composer pint:test # Check code style
  • PHPStan: Static Analysis (Level 9)

    composer analyse   # Run static analysis
  • Rector: PHP 8.3 Compatibility and Code Quality

    composer rector    # Run code quality checks

License

MIT License