{"id":16119972,"url":"https://github.com/uuf6429/expression-language-arrowfunc","last_synced_at":"2026-07-04T23:00:41.184Z","repository":{"id":47457697,"uuid":"72320999","full_name":"uuf6429/expression-language-arrowfunc","owner":"uuf6429","description":"➡️ Arrow function support in Symfony Expression Language","archived":false,"fork":false,"pushed_at":"2026-07-04T21:04:31.000Z","size":97,"stargazers_count":3,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-07-04T21:10:34.832Z","etag":null,"topics":["arrow-functions","expression-language","php","symfony","syntax","uuf6429"],"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/uuf6429.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-30T02:43:24.000Z","updated_at":"2026-07-04T20:53:01.000Z","dependencies_parsed_at":"2022-08-21T02:40:51.423Z","dependency_job_id":null,"html_url":"https://github.com/uuf6429/expression-language-arrowfunc","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/uuf6429/expression-language-arrowfunc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fexpression-language-arrowfunc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fexpression-language-arrowfunc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fexpression-language-arrowfunc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fexpression-language-arrowfunc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uuf6429","download_url":"https://codeload.github.com/uuf6429/expression-language-arrowfunc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fexpression-language-arrowfunc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35138078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-04T02:00:05.987Z","response_time":113,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["arrow-functions","expression-language","php","symfony","syntax","uuf6429"],"created_at":"2024-10-09T20:55:42.789Z","updated_at":"2026-07-04T23:00:41.161Z","avatar_url":"https://github.com/uuf6429.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔌 Symfony Expression Language Arrow Function\n\n[![CI](https://github.com/uuf6429/expression-language-arrowfunc/actions/workflows/ci.yml/badge.svg)](https://github.com/uuf6429/expression-language-arrowfunc/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/uuf6429/expression-language-arrowfunc/branch/main/graph/badge.svg)](https://codecov.io/gh/uuf6429/expression-language-arrowfunc)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%5E7.4%20%7C%7C%20%5E8-8892BF.svg)](https://php.net/)\n[![License](https://poser.pugx.org/uuf6429/expression-language-arrowfunc/license)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)\n[![Latest Stable Version](https://poser.pugx.org/uuf6429/expression-language-arrowfunc/v)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)\n[![Latest Unstable Version](https://poser.pugx.org/uuf6429/expression-language-arrowfunc/v/unstable)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)\n\nThis library extends Symfony [Expression Language component] to support _Arrow Function_ (also known as _Lambda\nFunctions_) syntax.\n\n## 🔌 Installation\n\nInstall via Composer:\n\n```shell\ncomposer require uuf6429/expression-language-arrowfunc\n```\n\n## 💡 Before You Start\n\n\u003cdetails\u003e\n\u003csummary\u003e1. How does it work?\u003c/summary\u003e\n\nEssentially, the library pre-processes expressions to replace callbacks with placeholders (variables), then generates\nnew variables (with the callback as value), which are finally later on used for executing the expression.\nConceptually:\n\n```mermaid\nflowchart TD\n\tA[\"items\u003cbr/\u003e\u0026nbsp;\u0026nbsp;.filter((item) -\u003e { item.type === 'T1' })\u003cbr/\u003e\u0026nbsp;\u0026nbsp;.map((item) -\u003e { item.name })\"] --\u003e B[\"Preprocess\"]\n\tB --\u003e C[\"\u003cb\u003eExpression:\u003c/b\u003e\u003cbr/\u003e\u003cbr/\u003eitems\u003cbr/\u003e\u0026nbsp;\u0026nbsp;.filter(__lambda_1)\u003cbr/\u003e\u0026nbsp;\u0026nbsp;.map(__lambda_2)\"]\n\tB --\u003e D[\"\u003cb\u003eVariables:\u003c/b\u003e\u003cbr/\u003e\u003cbr/\u003e$items = ... (original variable)\u003cbr/\u003e$__lambda_1 = fn (item) =\u003e item.type === 'T1'\u003cbr/\u003e$__lambda_2 = fn (item) =\u003e item.name\"]\n\tC --\u003e E[\"compile() or evaluate()\"]\n\tD --\u003e E\n\tclassDef default font-family: monospace;\n\tstyle A text-align: left;\n\tstyle C text-align: left;\n\tstyle D text-align: left;\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e2. What does the syntax look like?\u003c/summary\u003e\n\nBy default, the arrow function syntax looks like so:\n\n```text\n (a) -\u003e { a * 2 }\n  ▲  ▲      ▲\n  │  │      └── Function body is a single expression that can make\n  │  │          use of passed parameters or global variables.\n  │  └───────── The lambda operator - input parameters are to the\n  │             left and the output expression to the right.\n  └──────────── Comma-separated list of parameters passed to the\n                arrow function.\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e3. How safe is it?\u003c/summary\u003e\n\nSymfony Expression Language can be unsafe by design – if the result of expressions is used as a callable without being\nchecked, global callables, functions and static methods could be called arbitrarily from (potentially malicious)\nexpressions. This library acknowledges the risk and tries to mitigate it.\n\n**Problem Examples**\n\n1. Exposing functionality that accepts a callable to Expression Language:\n   ```php no-test\n   $el = new ExpressionLanguage();\n   $el-\u003eaddFunction(new ExpressionFunction(\n       'array_map',\n       static fn ($callback, $array) =\u003e sprintf('\\array_map(%s, %s)', $callback, $array),\n       static fn (array $variables, callable $callback, array $array) =\u003e array_map($callback, $array),\n   ));\n   \n   // Intended expression\n   $result = $el-\u003eevaluate('array_map([\" aa \"], \"trim\")');  // =\u003e ['aa']\n   \n   // Malicious expression\n   $result = $el-\u003eevaluate('array_map([123], \"exit\")');     // 💥 application exits with code 123\n   ```\n2. A naive implementation of arrow functions:\n   ```php no-test\n   $el = new ExpressionLanguage();\n   \n   // Intended expression\n   $filter = $el-\u003eevaluate('(value) -\u003e { value \u003e 20 }');\n   $values = array_filter([18, 23, 40], $filter);           // =\u003e [23, 49]\n   \n   // Malicious expression\n   $filter = $el-\u003eevaluate('\"exit\"');\n   $values = array_filter([18, 23, 40], $filter);           // 💥 application exits with code 18\n   ```\n\n**Solutions**\n\nThere are two viable solutions:\n\n1. Set the type declaration of methods or functions that will receive callbacks to `Closure` (*not `Callable`!*) - it\n   works, but it is prone to mistakes and frankly, quite risky.\n2. The engine wraps callbacks within an object that cannot be invoked by default – this is the safest option (and the\n   default). Usages need to be aware of this wrapping, drastically decreasing the risk of mistakes.\n\n\u003c/details\u003e\n\n## 🚀 Usage\n\n## Usage\n\nTwo different APIs are provided to suit your integration needs:\n\n\u003cdetails\u003e\n\u003csummary\u003e1. Ready-Made Drop-in Replacement\u003c/summary\u003e\n\nIf you just need a standard, drop-in replacement for Symfony's standard `ExpressionLanguage` class, use\n[`uuf6429\\ExpressionLanguage\\ExpressionLanguageWithArrowFunctions`]:\n\n```php\nuse uuf6429\\ExpressionLanguage\\ExpressionLanguageWithArrowFunctions;\n\n$el = new ExpressionLanguageWithArrowFunctions();\n\n$phpCode = $el-\u003ecompile('(val) -\u003e { val * 2 }', []);\nassert($phpCode === 'function ($val) { return ($val * 2); }');\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e2. Extensible Trait\u003c/summary\u003e\n\nIf you want to integrate this functionality with your own custom `ExpressionLanguage` implementation or combine it with\nother classes, use the trait [`uuf6429\\ExpressionLanguage\\ArrowFunctionTrait`]:\n\n```php\nuse Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage as SymfonyExpressionLanguage;\nuse Symfony\\Component\\ExpressionLanguage\\ParsedExpression as SymfonyParsedExpression;\nuse uuf6429\\ExpressionLanguage\\ArrowFunctionTrait;\n\nclass MyCustomExpressionLanguage\n{\n    use ArrowFunctionTrait;\n\n    public function evaluate($expression, $values = [])\n    {\n        return $this-\u003eevaluateWithArrowFunctions($expression, $values);\n    }\n\n    public function compile($expression, $names = [])\n    {\n        return $this-\u003ecompileWithArrowFunctions($expression, $names);\n    }\n\n    protected function compileWithoutArrowFunctions($expression, array $names = []): string\n    {\n        // TODO Implement method\n    }\n\n    protected function evaluateWithoutArrowFunctions($expression, array $values = [])\n    {\n        // TODO Implement method\n    }\n\n    protected function parseWithoutArrowFunctions($expression, array $names = []): SymfonyParsedExpression\n    {\n        // TODO Implement method\n    }\n\n    protected function lintWithoutArrowFunctions($expression, array $names = []): void\n    {\n        // TODO Implement method\n    }\n}\n```\n\n\u003c/details\u003e\n\n\u003e [!IMPORTANT]\n\u003e\n\u003e For safety reasons, callbacks are wrapped in a [`SafeCallable`] object. Your methods and functions need to expect\n\u003e and handle that object. This is to avoid expressions being able to \"break out\" and execute anything.\n\nHere's a more complete example:\n\n```php\nuse Symfony\\Component\\ExpressionLanguage\\ExpressionFunction;\nuse uuf6429\\ExpressionLanguage\\ExpressionLanguageWithArrowFunctions;\nuse uuf6429\\ExpressionLanguage\\SafeCallable;\n\n$el = new ExpressionLanguageWithArrowFunctions();\n\n// Expose array_map() as map()\n$el-\u003eaddFunction(new ExpressionFunction(\n   'map',\n   static fn (string $callbackExpr, string $arrayExpr) =\u003e sprintf('\\array_map(%s-\u003egetCallback(), %s)', $callbackExpr, $arrayExpr),\n   static fn (array $variables, SafeCallable $callback, array $array) =\u003e array_map($callback-\u003egetCallback(), $array),\n));\n\n// Compiling\n$phpCode = $el-\u003ecompile('map((val) -\u003e { val * 2 }, values)', ['values']);\nassert($phpCode === '\\array_map(function ($val) { return ($val * 2); }-\u003egetCallback(), $values)');\n\n// Evaluating\n$result = $el-\u003eevaluate('map((val) -\u003e { val * 2 }, values)', ['values' =\u003e [1, 2, 3]]);\nassert($result === [2, 4, 6]);\n```\n\n[Expression Language component]: https://symfony.com/doc/current/components/expression_language.html\n\n[`SafeCallable`]:https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/SafeCallable.php\n\n[`uuf6429\\ExpressionLanguage\\ExpressionLanguageWithArrowFunctions`]: https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/ExpressionLanguageWithArrowFunctions.php\n\n[`uuf6429\\ExpressionLanguage\\ArrowFunctionTrait`]: https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/ArrowFunctionTrait.php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuuf6429%2Fexpression-language-arrowfunc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuuf6429%2Fexpression-language-arrowfunc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuuf6429%2Fexpression-language-arrowfunc/lists"}