arielespinoza07 / result-pattern
A modern implementation of the Result pattern for handling operation outcomes and HTTP responses
Requires
- php: ^8.3
Requires (Dev)
- laravel/pint: ^1.24.0
- mockery/mockery: ^1.6.12
- peckphp/peck: ^0.1.3
- pestphp/pest: ^v3.8.2
- pestphp/pest-plugin-type-coverage: ^3.6
- phpstan/phpstan: ^2.1.22
- phpstan/phpstan-mockery: ^2.0.0
- rector/rector: ^2.1.2
README
A modern and simple implementation of the Result pattern for handling operation outcomes or HTTP responses.
**Requires PHP 8.3+ **
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:
- Basic Success and Failure Examples - Creating and using Success and Failure objects
- Try Method Examples - Using the
try
method to handle operations that might throw exceptions - onSuccess and onFailure Examples - Using callbacks for Success and Failure cases
- Map Method Examples - Transforming values inside Success results
- FlatMap Method Examples - Transforming Success results into new Result objects
- Fold Method Examples - Handling both Success and Failure cases with a single return value
- Error Handling Examples - Proper error handling techniques and best practices
- HTTP Response Code Available
Available Response Codes
- Informational Responses (1xx)
- Success Responses (2xx)
- Redirection Responses (3xx)
- Client Error Responses (4xx)
- Server Error Responses (5xx)
Development
Requirements
- PHP 8.3+
- Composer 2.0+
Installation
composer require arielespinoza07/result-pattern
Development Installation
git https://github.com/ArielEspinoza07/result-pattern.git
cd result-pattern
composer install
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
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