respect / parameter
Resolves function/constructor parameters via type and name lookup
1.0.0
2026-03-22 12:18 UTC
Requires
- php: ^8.5
- psr/container: ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.5 || ^13.0
- respect/coding-standard: ^5.0
This package is auto-updated.
Last update: 2026-03-22 12:20:42 UTC
README
Resolves function and constructor parameters from a PSR-11 container by type.
Install
composer require respect/parameter
Usage
Resolve from a container
For each parameter the resolver tries, in order:
- Positional argument of matching type
- Container match by type (non-builtin)
- Next positional argument
- Default value
null
use Respect\Parameter\Resolver; function notify(Mailer $mailer, Logger $logger, string $to, string $subject = 'Hi') { // ... } $resolver = new Resolver($container); $args = $resolver->resolve(new ReflectionFunction('notify'), ['bob@example.com']); // ['mailer' => Mailer, 'logger' => Logger, 'to' => 'bob@example.com', 'subject' => 'Hi']
Results are keyed by parameter name, so you can spread them with named arguments:
notify(...$args);
Resolve with named arguments
When arguments are keyed by name (e.g. from configuration):
$args = $resolver->resolveNamed( $constructor, ['username' => 'admin', 'password' => 'secret'], ); // Named args take precedence, gaps filled from container by name and type
Reflect any callable
Convert any callable form into a ReflectionFunctionAbstract:
use Respect\Parameter\Resolver; Resolver::reflectCallable(fn() => ...); // Closure Resolver::reflectCallable([$obj, 'method']); // Array callable Resolver::reflectCallable(new Invocable()); // __invoke object Resolver::reflectCallable('strlen'); // Function name Resolver::reflectCallable('DateTime::createFromFormat'); // Static method
Check accepted types
Resolver::acceptsType($reflection, LoggerInterface::class); // true/false
API
| Method | Type | Description |
|---|---|---|
resolve($reflection, $positional) |
instance | Resolve parameters from positional args + container. Returns array<string, mixed> keyed by parameter name |
resolveNamed($reflection, $named) |
instance | Resolve from named args (priority) + container. Returns array<string, mixed> keyed by parameter name |
reflectCallable($callable) |
static | Any callable to ReflectionFunctionAbstract |
acceptsType($reflection, $type) |
static | Check if any parameter accepts a type |
License
ISC. See LICENSE.