- How do I install the Option type in a Laravel project?
- Run `composer require php-standard-library/option` in your project root. No Laravel-specific dependencies exist, so it integrates seamlessly with existing codebases. The package follows PSR-4 autoloading standards, so no additional configuration is needed for basic usage.
- Can I use Option with Laravel Eloquent models?
- Yes, wrap Eloquent results explicitly using `Option::fromNullable($model)` or `Option::from($model, fn() => null)`. For query builders, chain methods like `Model::where(...)->first()` and then wrap the result. Avoid mixing raw nulls and Options in the same domain to prevent ambiguity.
- What Laravel versions does this package support?
- The package is framework-agnostic and works with any PHP 8.1+ project, including Laravel 9.x and 10.x. No Laravel-specific dependencies mean it’s compatible with all modern Laravel releases. Test thoroughly in your target version, as some dynamic Laravel features (e.g., magic methods) may require adapters.
- How do I handle Option types in Laravel’s request validation?
- Replace nullable validation rules (e.g., `'field' => 'nullable|string'`) with explicit Option logic. Use `Option::from($request->input('optional_field'))` to wrap inputs, then chain methods like `map()` or `filter()` for conditional processing. This avoids nested `isset()` checks and makes intent clearer.
- Will using Option improve performance in Laravel?
- The runtime overhead is negligible—Option adds type safety without significant memory or CPU costs. However, wrapping every nullable value (e.g., `Option::from($user)`) introduces minor object instantiation. Benchmark critical paths to confirm the tradeoff is worth the bug reduction, especially in high-traffic APIs.
- How do I test code that returns Option types in Laravel?
- Use assertions like `assertTrue($option->isSome())` or `assertEquals($value, $option->unwrap())` in PHPUnit. For edge cases, test `None` handling (e.g., `assertTrue($option->isNone())`) and chained operations (e.g., `map()` on `None`). Mock dependencies returning `Option` to simulate optional data flows.
- What’s the best way to migrate from nullable types to Option in Laravel?
- Start with a single domain (e.g., `Order` entity) and refactor return types incrementally. Use adapters (e.g., `OptionAdapter::fromNullable($legacyValue)`) to coexist with old code. Update method signatures (e.g., `?User → Option<User>`) and leverage IDE refactoring tools to automate changes.
- Are there alternatives to Option for handling optional data in Laravel?
- For simple cases, nullable types (`?User`) or `@phpstan-ignore` annotations may suffice. Libraries like `league/value-object` or `spatie/array-to-object` offer similar patterns but lack the functional chaining (e.g., `map`, `filter`) that Option provides. Weigh the tradeoff between explicit semantics and complexity.
- How do I integrate Option with Laravel’s service container?
- Bind Option-wrapped services in your `AppServiceProvider` (e.g., `app->bind(Option::class, fn() => new Option(...))`). For repositories, return `Option<User>` instead of `?User` and resolve dependencies via constructor injection. Laravel’s DI will handle the rest without additional configuration.
- What tools or IDE support are available for Option in Laravel?
- IDE autocompletion for Option methods is limited compared to native PHP. Use PHPStan’s `Option` rules to enforce type safety (e.g., `phpstan --level=max`). For debugging, log `isSome()`/`isNone()` states or use custom error handlers to trace Option chains. Static analysis will catch most integration issues early.