{"id":19861259,"url":"https://github.com/rybakit/arguments-resolver","last_synced_at":"2025-05-02T04:30:32.647Z","repository":{"id":19403006,"uuid":"22644755","full_name":"rybakit/arguments-resolver","owner":"rybakit","description":"ArgumentsResolver allows you to determine the arguments to pass to a function or method.","archived":false,"fork":false,"pushed_at":"2021-06-22T14:22:23.000Z","size":128,"stargazers_count":26,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-24T03:02:04.451Z","etag":null,"topics":["arguments","callable","php","resolver"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rybakit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["rybakit"]}},"created_at":"2014-08-05T13:36:31.000Z","updated_at":"2021-08-06T03:29:43.000Z","dependencies_parsed_at":"2022-09-13T15:01:04.051Z","dependency_job_id":null,"html_url":"https://github.com/rybakit/arguments-resolver","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybakit%2Farguments-resolver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybakit%2Farguments-resolver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybakit%2Farguments-resolver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybakit%2Farguments-resolver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rybakit","download_url":"https://codeload.github.com/rybakit/arguments-resolver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251986646,"owners_count":21675950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arguments","callable","php","resolver"],"created_at":"2024-11-12T15:08:29.873Z","updated_at":"2025-05-02T04:30:32.309Z","avatar_url":"https://github.com/rybakit.png","language":"PHP","readme":"ArgumentsResolver\n=================\n![Quality Assurance](https://github.com/rybakit/arguments-resolver/workflows/QA/badge.svg)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/rybakit/arguments-resolver/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/rybakit/arguments-resolver/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/rybakit/arguments-resolver/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/rybakit/arguments-resolver/?branch=master)\n\nArgumentsResolver allows you to determine the arguments to pass to a function or method.\n\n\n## Installation\n\nThe recommended way to install ArgumentsResolver is through [Composer](http://getcomposer.org):\n\n```bash\ncomposer require rybakit/arguments-resolver\n```\n\n\n## Usage example\n\n```php\nuse ArgumentsResolver\\InDepthArgumentsResolver;\n\n$greet = function ($username, DateTime $date, $greeting = 'Hello %s!') {\n    // ...\n};\n\n$parameters = [\n    'Welcome %s!',\n    ['foo'],\n    new DateTime(),\n    'username' =\u003e 'Stranger',\n    'bar',\n];\n\n$arguments = (new InDepthArgumentsResolver($greet))-\u003eresolve($parameters);\nprint_r($arguments);\n```\n\nThe above example will output:\n\n```php\nArray\n(\n    [0] =\u003e Stranger\n    [1] =\u003e DateTime Object (...)\n    [2] =\u003e Welcome %s!\n)\n```\n\n\n## Resolvers\n\nThe library ships with two resolvers, the [InDepthArgumentsResolver](#indepthargumentsresolver) and [NamedArgumentsResolver](#namedargumentsresolver).\nThey both expect a function to be supplied as a single constructor argument. The function can be any [callable](http://php.net/manual/en/language.types.callable.php), [a string representing a class method](http://php.net/manual/en/reflectionmethod.construct.php#refsect1-reflectionmethod.construct-parameters) or an instance of [ReflectionFunctionAbstract](http://php.net/manual/en/class.reflectionfunctionabstract.php):\n\n```php\nnew InDepthArgumentsResolver(['MyClass', 'myMethod']);\nnew InDepthArgumentsResolver([new MyClass(), 'myMethod']);\nnew InDepthArgumentsResolver(['MyClass', 'myStaticMethod']);\nnew InDepthArgumentsResolver('MyClass::myStaticMethod');\nnew InDepthArgumentsResolver('MyClass::__construct');\nnew InDepthArgumentsResolver(['MyClass', '__construct']);\nnew InDepthArgumentsResolver(new MyInvokableClass());\nnew InDepthArgumentsResolver(function ($foo) {});\nnew InDepthArgumentsResolver('MyNamespace\\my_function');\nnew InDepthArgumentsResolver(new ReflectionMethod('MyClass', 'myMethod'));\nnew InDepthArgumentsResolver(new ReflectionFunction('MyNamespace\\my_function'));\n```\n\nThere is also an utility class which helps in creating a reflection instance:\n\n```php\nuse ArgumentsResolver\\ReflectionFactory;\n\n$reflection = ReflectionFactory::create('MyClass::__construct');\n$resolver = new InDepthArgumentsResolver($reflection);\n```\n\n\n#### InDepthArgumentsResolver\n\nIn the `InDepthArgumentsResolver`, the decision about whether an argument matched the parameter value or not\nis influenced by multiple factors, namely the argument's type, the class hierarchy (if it's an object),\nthe argument name and the argument position.\n\nTo clarify, consider each circumstance in turn:\n\n*Argument type*\n\n```php\nfunction foo(array $array, stdClass $object, callable $callable) {}\n\n(new InDepthArgumentsResolver('foo'))-\u003eresolve([\n    ...\n    function () {},    // $callable\n    ...\n    new stdClass(),    // $object\n    ...\n    [42],              // $array\n    ...\n]);\n```\n\n*Class hierarchy*\n\n```php\nfunction foo(Exception $e, RuntimeException $re) {}\n\n(new InDepthArgumentsResolver('foo'))-\u003eresolve([\n    ...\n    new RuntimeException(),    // $re\n    ...\n    new Exception(),           // $e\n    ...\n]);\n```\n\n*Argument name*\n\n```php\nfunction foo($a, $b) {}\n\n(new InDepthArgumentsResolver('foo'))-\u003eresolve([\n    ...\n    'c' =\u003e 3,\n    'b' =\u003e 2,    // $b\n    'a' =\u003e 1,    // $a\n    ...\n]);\n```\n\n*Argument position*\n\n```php\nfunction foo($a, $b) {}\n\n(new InDepthArgumentsResolver('foo'))-\u003eresolve([\n    1,   // $a\n    2,   // $b\n    ...\n]);\n```\n\n#### NamedArgumentsResolver\n\nThe `NamedArgumentsResolver` is a very simple resolver which does the matching only by the argument name.\nTherefore this requires parameters to be an associative array:\n\n```php\nfunction foo($a, array $b, $c = null) {}\n\n(new NamedArgumentsResolver('foo'))-\u003eresolve([\n    ...\n    'b' =\u003e [],       // $b\n    'a' =\u003e 1,        // $a\n    'c' =\u003e 'bar',    // $c\n    ...\n]);\n```\n\n\n## Tests\n\nArgumentsResolver uses [PHPUnit](http://phpunit.de) for unit testing.\nIn order to run the tests, you'll first need to setup the test suite using composer:\n\n```bash\ncomposer install\n```\n\nYou can then run the tests:\n\n```bash\nvendor/bin/phpunit\n```\n\n\n## License\n\nArgumentsResolver is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.\n","funding_links":["https://github.com/sponsors/rybakit"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frybakit%2Farguments-resolver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frybakit%2Farguments-resolver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frybakit%2Farguments-resolver/lists"}