daverandom/callback-validator
Validate PHP callable signatures against a prototype with variance support. Build a CallbackType from an existing callable or define it manually, then test other callables for parameter/return compatibility (contra/covariance, nullability) and inspect mismatches via __toString().
Install via composer require daverandom/callback-validator. Begin by validating a simple callback signature—e.g., for an event listener like function (UserRegistered $event): void. Create a prototype closure ($prototype = fn(UserRegistered $e): void {}), derive a CallbackType ($type = CallbackType::createFromCallable($prototype)), and validate candidates ($type->isSatisfiedBy($candidate)). First use case: enforce strict listener signatures in your own event system before registration.
function (array $payload): array) and validate user-provided plugins/middleware before registration in a service provider.Collection::macro(), validate the callback against a known signature to prevent runtime TypeError on misuse.config/app.php or package config, register callbacks (e.g., for custom resolvers), then validate them at boot time using CallbackType—fail fast if mismatched.CallbackType construction to relax validation—e.g., omit required parameters for optional hooks or allow mixed returns with ReturnType::COVARIANT.ChildClass where ParentClass is expected), but less specific types fail. Returns are covariant: string is acceptable where ?string is expected, but string is not acceptable where string is expected but candidate returns ?string.ParameterType to allow optional/variadic args (which this package does not support natively for ...$args).CallbackType::createFromCallable($candidate) and compare __toString() outputs. Build a helper to diff expected vs. actual signatures (e.g., echo "Expected: {$expected}\nGot: {$actual}\nMismatch: " . $expected->isSatisfiedBy($candidate) ? '✅' : '❌';).CallbackType instances in a registry during boot or use Laravel’s caching to store the validated types (e.g., in config() or custom cache key).Event::listen() already enforces return type hints at runtime—but only for closures with type hints. Use this package to enforce signature structure for untyped or loosely typed callbacks (e.g., from config or dynamic input), especially where void returns or strict argument counts matter.How can I help you explore Laravel packages today?