spatie/better-types
Reflection-powered type checking for PHP: verify whether a ReflectionType or method signature accepts given arguments (including unions/nullables and named params). Useful for dispatching/overload-like method selection and safer dynamic calls.
string|int), and return type declarations. This is particularly valuable for:
is_a()/instanceof fall short for complex type checks (e.g., array{string, int} or DateTimeInterface|Carbon).Illuminate\Support\Traits\ForwardsCalls for dynamic method handling.if (BetterType::is($value, new ArrayType([StringType::class, IntType::class])))).instanceof/is_a() logic remains functional; new code can leverage BetterType for stricter checks.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance Overhead | Runtime type checks add minimal overhead (~microseconds per call). | Benchmark critical paths; avoid in hot loops (e.g., bulk operations). |
| Type System Complexity | Union types with nested generics (e.g., array<int, array<string>>) may require verbose syntax. |
Document common patterns; provide helper methods for frequent use cases (e.g., BetterType::isArrayOfStrings()). |
| PHP Version Dependency | Requires PHP 8.0+ (for union types). | Enforce PHP 8.1+ in project (Laravel 9+). |
| False Positives/Negatives | Custom type checks (e.g., DateTimeInterface) may misbehave with proxies. |
Test with Laravel’s service container and common proxied classes (e.g., Eloquent models). |
| Testing Impact | May require updating tests to use new type assertions. | Gradual migration: start with non-critical paths. |
array_merge hacks with ArrayType checks)?"instanceof/is_a() to BetterType? (e.g., coding standards, pair programming).BetterType for project-specific types (e.g., custom value objects)?BetterType::is($request->input('age'), IntType::class)).JsonType::fromJson($request->all())).if (BetterType::is($user, UserType::class))).BetterType::assert($result, ArrayType::of(StringType::class))).is_array($value) && array_key_exists('id', $value) with BetterType::is($value, ArrayType::withKey('id', StringType::class)).if (is_object($var) && method_exists($var, 'getName'))).BetterType for stricter contracts (e.g., if (BetterType::is($var, NameableInterfaceType::class))).BetterType::assert().instanceof/is_a() in favor of BetterType where applicable.spatie/laravel-permission, laravel/framework).spatie/array-to-object for type-safe conversions.Spatie\BetterTypes\Type for project-specific types (e.g., CustomUserType).| Priority | Integration Point | Example Use Case | Dependencies |
|---|---|---|---|
| High | Form Requests | Validate union types in AuthorizesRequests. |
Laravel 9+, PHP 8.1+ |
| High | API Contracts | Runtime validation of JSON payloads. | spatie/better-types, symfony/serializer |
| Medium | Domain Services | Enforce type safety in business logic. | Custom value objects |
| Medium | Testing | Replace assertIsArray() with BetterType::assert(). |
Pest/PHPUnit |
| Low | Legacy Code Refactoring | Replace is_a() hacks in old controllers. |
Code coverage tools |
string where an int is expected).instanceof logic.spatie/better-types updates (though MIT license minimizes risk).BetterType::is($value, Type::class)) mirrors PHP’s native syntax.User|Guest union type")."Expected array{string, int}, got array{string, bool}").try-catch blocks for graceful degradation or log warnings.How can I help you explore Laravel packages today?