dantleech/argument-resolver
Resolve method arguments from an associative array
Maintainers
Requires
None
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.10.1
- phpunit/phpunit: ^7.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT e34fabf7d6e53e5194f745ad069c4a87cc4b34cc
- Daniel Leech <daniel.woop@dantleech.com>
This package is auto-updated.
Last update: 2026-06-09 21:51:33 UTC
README
Resolve and validate method arguments from an associative array.
- Resolves arguments including default values from an associative array.
- Throws exception if required arguments are missing.
- Throws exception if extra arguments are provided.
Example
class Foobar
{
public function execute(string $foobar, $barfoo = 'foobar')
{
}
}
$argumentResolver = new ArgumentResolver();
$arguments = $argumentResolver->resolveArguments(Foobar::class, 'execute', [
'foobar' => 'hello'
]);
var_dump($arguments);
// array(2) {
// [0] =>
// string(5) "hello"
// [1] =>
// string(6) "foobar"
// }
$result = call_user_func([$myClass, 'myMethod'], $arguments);
Motivation
It is often desired that service have some runtime invocation configuration which is provided by the user (similar to Symfony Forms and the Symfony OptionResolver component, which does something like:
$resolver = new OptionsResolver();
$resolver->setDefault([
'barfoo' => 'hello',
]);
$resolver->setRequired('foobar')l
$options = $resolver->resolve(['foobar' => 'hello']);
$myService->doSomething($options);
I have used this pattern in PHPBench and found it very useful for configuring services invocations at runtime. However it has drawbacks:
- Your service is passed an untyped array.
- The service has an implicit dependency on the options resolver.
This package does something similar to the options resolver, but uses the reflection information from the method to resolve the arguments.
Drawbacks
This method does mean that you cannot implement a interface for your service
(e.g. execute($blah, array $config) as each service will have it's own
signature execute($blah, $timeout = 1234, $iterations = 33).
Similar Libraries
https://github.com/sroze/ArgumentResolver does almost exactly the same thing
