PHPackages                             uuf6429/expression-language-arrowfunc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. uuf6429/expression-language-arrowfunc

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

uuf6429/expression-language-arrowfunc
=====================================

Arrow function support in Symfony Expression Language

1.0.6(3w ago)4223[2 issues](https://github.com/uuf6429/expression-language-arrowfunc/issues)MITPHPPHP ^7.4 || ^8CI passing

Since Oct 30Pushed 2w ago1 watchersCompare

[ Source](https://github.com/uuf6429/expression-language-arrowfunc)[ Packagist](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)[ Docs](https://github.com/uuf6429/expression-language-arrowfunc)[ RSS](/packages/uuf6429-expression-language-arrowfunc/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (9)Dependencies (9)Versions (12)Used By (0)

➡️ Symfony Expression Language Arrow Function
=============================================

[](#️-symfony-expression-language-arrow-function)

for Symfony Expression Language (5-8)

[![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)[![codecov](https://camo.githubusercontent.com/d4fc6d5b40b12a000cd56e14da98ed4ff05b4cbc4370f9f19ab83a48f610f6c0/68747470733a2f2f636f6465636f762e696f2f67682f757566363432392f65787072657373696f6e2d6c616e67756167652d6172726f7766756e632f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/uuf6429/expression-language-arrowfunc)[![Minimum PHP Version](https://camo.githubusercontent.com/091c5fe4143f6d3aee38fc19c54dbd8419d950a9c3803f460e4fb0fd50be67ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e34253230253743253743253230253545382d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/75a90afb5451b29c4ff98c957b2fce7ed3d9192edfda736a1a804a39eda3b57f/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f65787072657373696f6e2d6c616e67756167652d6172726f7766756e632f6c6963656e7365)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)[![Latest Stable Version](https://camo.githubusercontent.com/eb4505328eebe7074c894626748a76a7d910020e1aafdb7d64238f2325c6d08b/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f65787072657373696f6e2d6c616e67756167652d6172726f7766756e632f76)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)[![Latest Unstable Version](https://camo.githubusercontent.com/bce4e448491bf2a711791c749a67bfb840635286f454656d5614a5625903c1a6/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f65787072657373696f6e2d6c616e67756167652d6172726f7766756e632f762f756e737461626c65)](https://packagist.org/packages/uuf6429/expression-language-arrowfunc)

This library extends Symfony [Expression Language component](https://symfony.com/doc/current/components/expression_language.html) to support *Arrow Function* (also known as *Lambda Functions*) syntax.

🔌 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require uuf6429/expression-language-arrowfunc
```

💡 Before You Start
------------------

[](#-before-you-start)

1. How does it work?Essentially, the library pre-processes expressions to replace callbacks with placeholders (variables), then generates new variables (with the callback as value), which are finally later on used for executing the expression. Conceptually:

 ```
flowchart TD
	A["items&nbsp;&nbsp;.filter((item) -> { item.type === 'T1' })&nbsp;&nbsp;.map((item) -> { item.name })"] --> B["Preprocess"]
	B --> C["Expression:items&nbsp;&nbsp;.filter(__lambda_1)&nbsp;&nbsp;.map(__lambda_2)"]
	B --> D["Variables:$items = ... (original variable)$__lambda_1 = fn (item) => item.type === 'T1'$__lambda_2 = fn (item) => item.name"]
	C --> E["compile() or evaluate()"]
	D --> E
	classDef default font-family: monospace;
	style A text-align: left;
	style C text-align: left;
	style D text-align: left;
```

      Loading 2. What does the syntax look like?By default, the arrow function syntax looks like so:

```
 (a, b) -> { a * b }
   ▲    ▲      ▲
   │    │      └── Function body is a single expression that can make use of passed
   │    │          parameters or global variables, must be enclosed in curly braces.
   │    │
   │    └───────── The lambda operator - input parameters are to the left and the
   │               output expression to the right.
   │
   └────────────── Comma-separated list of parameters received by the arrow
                   function, surrounded by round brackets. In case of only one
                   argument the round brackets can be omitted.

```

3. How safe is it?Symfony Expression Language can be unsafe by design – if the result of expressions is used as a callable without being checked, global callables, functions and static methods could be called arbitrarily from (potentially malicious) expressions. This library acknowledges the risk and tries to mitigate it.

**Problem Examples**

1. Exposing functionality that accepts a callable to Expression Language: ```
    $el = new ExpressionLanguage();
    $el->addFunction(new ExpressionFunction(
        'array_map',
        static fn ($callback, $array) => sprintf('\array_map(%s, %s)', $callback, $array),
        static fn (array $variables, callable $callback, array $array) => array_map($callback, $array),
    ));

    // Intended expression
    $result = $el->evaluate('array_map([" aa "], "trim")');  // => ['aa']

    // Malicious expression
    $result = $el->evaluate('array_map([123], "exit")');     // 💥 application exits with code 123
    ```
2. A naive implementation of arrow functions: ```
    $el = new ExpressionLanguage();

    // Intended expression
    $filter = $el->evaluate('(value) -> { value > 20 }');
    $values = array_filter([18, 23, 40], $filter);           // => [23, 49]

    // Malicious expression
    $filter = $el->evaluate('"exit"');
    $values = array_filter([18, 23, 40], $filter);           // 💥 application exits with code 18
    ```

**Solutions**

There are two viable solutions:

1. Set the type declaration of methods or functions that will receive callbacks to `Closure` (*not `Callable`!*) - it works, but it is prone to mistakes and frankly, quite risky.
2. The engine wraps callbacks within an object that cannot be invoked by default – this is the safest option (and the default). Usages need to be aware of this wrapping, drastically decreasing the risk of mistakes.

🚀 Usage
-------

[](#-usage)

Usage
-----

[](#usage)

Two different APIs are provided to suit your integration needs:

1. Ready-Made Drop-in ReplacementIf you just need a standard, drop-in replacement for Symfony's standard `ExpressionLanguage` class, use [`uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunctions`](https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/ExpressionLanguageWithArrowFunctions.php):

```
use uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunctions;

$el = new ExpressionLanguageWithArrowFunctions();

$phpCode = $el->compile('(val) -> { val * 2 }', []);
assert($phpCode === 'function ($val) { return ($val * 2); }');
```

2. Extensible TraitIf you want to integrate this functionality with your own custom `ExpressionLanguage` implementation or combine it with other classes, use the trait [`uuf6429\ExpressionLanguage\ArrowFunctionTrait`](https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/ArrowFunctionTrait.php):

```
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as SymfonyExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ParsedExpression as SymfonyParsedExpression;
use uuf6429\ExpressionLanguage\ArrowFunctionTrait;

class MyCustomExpressionLanguage
{
    use ArrowFunctionTrait;

    public function evaluate($expression, $values = [])
    {
        return $this->evaluateWithArrowFunctions($expression, $values);
    }

    public function compile($expression, $names = [])
    {
        return $this->compileWithArrowFunctions($expression, $names);
    }

    protected function compileWithoutArrowFunctions($expression, array $names = []): string
    {
        // TODO Implement method
    }

    protected function evaluateWithoutArrowFunctions($expression, array $values = [])
    {
        // TODO Implement method
    }

    protected function parseWithoutArrowFunctions($expression, array $names = []): SymfonyParsedExpression
    {
        // TODO Implement method
    }

    protected function lintWithoutArrowFunctions($expression, array $names = []): void
    {
        // TODO Implement method
    }
}
```

Important

For safety reasons, any returned callbacks are wrapped in a [`SafeCallable`](https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/SafeCallable.php) object. Your methods and functions need to expect and handle that object. This is to avoid expressions being able to "break out" and execute anything.

Tip

The [`SafeCallable`](https://github.com/uuf6429/expression-language-arrowfunc/blob/main/src/SafeCallable.php) object also provides [information about the arrow function](https://github.com/search?q=repo%3Auuf6429%2Fexpression-language-arrowfunc+path%3A**%2FParsedExpression.php+TLambdaDefinition&type=code) source code via [`getLambdaInfo()`](https://github.com/search?q=repo%3Auuf6429%2Fexpression-language-arrowfunc+path%3A**%2FSafeCallable.php+getLambdaInfo&type=code).

Here's a more complete example:

```
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use uuf6429\ExpressionLanguage\ExpressionLanguageWithArrowFunctions;
use uuf6429\ExpressionLanguage\SafeCallable;

$el = new ExpressionLanguageWithArrowFunctions();

// Expose array_map() as map()
$el->addFunction(new ExpressionFunction(
   'map',
   static fn (string $callbackExpr, string $arrayExpr) => sprintf('\array_map(%s->getCallback(), %s)', $callbackExpr, $arrayExpr),
   static fn (array $variables, SafeCallable $callback, array $array) => array_map($callback->getCallback(), $array),
));

// Compiling
$phpCode = $el->compile('map((val) -> { val * 2 }, values)', ['values']);
assert($phpCode === '\array_map(function ($val) { return ($val * 2); }->getCallback(), $values)');

// Evaluating
$result = $el->evaluate('map((val) -> { val * 2 }, values)', ['values' => [1, 2, 3]]);
assert($result === [2, 4, 6]);
```

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance86

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~507 days

Recently: every ~2 days

Total

8

Last Release

22d ago

Major Versions

v0.1.0-alpha → 1.0.02026-07-04

PHP version history (2 changes)v0.1.0-alphaPHP &gt;=5.5.9

1.0.0PHP ^7.4 || ^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/450767af6ef832ad662c169bf718d6d25c025c08b2d91b810959d190bccebba1?d=identicon)[uuf6429](/maintainers/uuf6429)

---

Top Contributors

[![uuf6429](https://avatars.githubusercontent.com/u/230049?v=4)](https://github.com/uuf6429 "uuf6429 (25 commits)")

---

Tags

arrow-functionsexpression-languagephpsymfonysyntaxuuf6429symfonyparserexpressionclosureDSLlambdaexpression-languagearrow-function

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/uuf6429-expression-language-arrowfunc/health.svg)

```
[![Health](https://phpackages.com/badges/uuf6429-expression-language-arrowfunc/health.svg)](https://phpackages.com/packages/uuf6429-expression-language-arrowfunc)
```

###  Alternatives

[behat/gherkin

Gherkin DSL parser for PHP

1.1k187.8M157](/packages/behat-gherkin)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

12017.1k](/packages/rcsofttech-audit-trail-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M235](/packages/sulu-sulu)[nxp/math-executor

Simple math expressions calculator

2301.9M8](/packages/nxp-math-executor)[denissimon/formula-parser

Parsing and evaluating mathematical formulas given as strings.

80351.1k3](/packages/denissimon-formula-parser)[jeremeamia/functionparser

Function parser for PHP functions, methods, and closures

48176.1k6](/packages/jeremeamia-functionparser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
