62 Commits

Author SHA1 Message Date
Fabien Potencier c459ef0bdd Release destructuring temporaries after assignment 2026-08-27 13:32:19 +02:00
Fabien Potencier d7f8b4eb1c Redesign macro calls and argument handling 2026-07-30 13:53:24 +02:00
Fabien Potencier b762bc94b9 Make the sandbox a first-class citizen with a dedicated Sandbox class 2026-07-30 12:05:47 +02:00
Fabien Potencier be36fee09e Rename macro variable AST nodes 2026-07-28 11:53:50 +02:00
Fabien Potencier ef9c43187a Fix incompatible identifier() signature in test stubs 2026-07-12 15:15:28 +02:00
Fabien Potencier 9c6d76b61c Add void return type hint even in tests 2026-07-12 13:43:08 +02:00
Fabien Potencier 87093aab9e Allow calling a macro with a dynamic name via the dot operator 2026-06-06 17:10:52 +02:00
Fabien Potencier 8a4b77920a Add PHPUnit attributes alongside annotations to silence doc-comment metadata deprecations on PHPUnit 11 2026-06-03 18:26:39 +02:00
Fabien Potencier ea3f7a2844 Validate macro name in MacroReferenceExpression constructor
The name passed to MacroReferenceExpression is emitted as raw PHP in
compile() via "->{$name}(...)". Callers were expected to validate
the name, but a missing check led to CVE-2026-XXXXX (PHP code injection
via _self / import macro reference): defense-in-depth, validate the
name in the constructor so the class is safe by construction.
2026-05-19 23:42:31 +02:00
Fabien Potencier 5462817da0 Add a needs_is_sandboxed option for filters, functions, and tests 2026-05-16 18:52:17 +01:00
Fabien Potencier 751a187f07 feature #4748 Support short-circuiting in null-safe operator chains (HypeMC)
This PR was merged into the 3.x branch.

Discussion
----------

Support short-circuiting in null-safe operator chains

This PR adds short-circuiting for null-safe operator chains, using the same rules as PHP, `PropertyAccess`, and the `ExpressionLanguage`.

Previously, only the immediate null-safe access was guarded. With this change, as soon as a `null` is encountered at a null-safe access, the rest of the chain is skipped.

My approach was to move the null check outside of the `getAttribute()` calls so the expression can immediately return `null`, eg:

```twig
foo?.bar.baz
```

Before:

```php
yield $this->env
    ->getRuntime('Twig\Runtime\EscaperRuntime')
    ->escape(
        CoreExtension::getAttribute(
            $this->env,
            $this->source,
            (
                null === (
                    $_v0 = (
                        isset($context['foo']) || array_key_exists('foo', $context)
                            ? $context['foo']
                            : throw new RuntimeError('Variable "foo" does not exist.', 3, $this->source)
                    )
                )
                    ? null
                    : CoreExtension::getAttribute(
                        $this->env,
                        $this->source,
                        $_v0,
                        'bar',
                        [],
                        'any',
                        false,
                        false,
                        false,
                        3
                    )
            ),
            'baz',
            [],
            'any',
            false,
            false,
            false,
            3
        ),
        'html',
        null,
        true
    );
```

Now:

```php
yield $this->env
    ->getRuntime('Twig\Runtime\EscaperRuntime')
    ->escape(
        (
            null === (
                $_v0 = (
                    isset($context['foo']) || array_key_exists('foo', $context)
                        ? $context['foo']
                        : throw new RuntimeError('Variable "foo" does not exist.', 3, $this->source)
                )
            )
                ? null
                : CoreExtension::getAttribute(
                    $this->env,
                    $this->source,
                    CoreExtension::getAttribute(
                        $this->env,
                        $this->source,
                        $_v0,
                        'bar',
                        [],
                        'any',
                        false,
                        false,
                        false,
                        3
                    ),
                    'baz',
                    [],
                    'any',
                    false,
                    false,
                    false,
                    3
                )
        ),
        'html',
        null,
        true
    );
```

Commits
-------

d56e8e2dba Support short-circuiting in null-safe operator chains
2026-02-08 19:02:03 +01:00
Fabien Potencier 861215c507 Fix CS 2026-02-07 09:07:38 +01:00
HypeMC d56e8e2dba Support short-circuiting in null-safe operator chains 2026-02-06 22:36:57 +01:00
Felds Liscia 263c04fd1f Add null-safe operator 2026-01-17 14:57:47 +01:00
Fabien Potencier 8391928896 Fix deprecations 2026-01-12 09:52:14 +01:00
Nicolas Grekas 85a4817128 CS fixes 2025-07-29 10:07:07 +02:00
Fabien Potencier 2d84abfd08 Make the defined test implementation more generic 2025-02-21 08:36:21 +01:00
Fabien Potencier 445f74cfe0 [SECURITY] Fix a security issue where escaping was missing when using ?? 2025-01-29 07:52:07 +01:00
Fabien Potencier 76062c8d51 Fix CS 2025-01-19 16:54:05 +01:00
Fabien Potencier efd12ef0cc Optimize NameExpression compilation 2025-01-02 12:32:36 +01:00
Fabien Potencier 4f8ba93600 Enforce AbstractBinary for all binary operators 2024-11-29 16:51:09 +01:00
Fabien Potencier fc15e7ccbc Rename Node classes related to variables 2024-10-23 12:36:57 +02:00
Fabien Potencier 8b278986b8 Deprecate using Node directly, introduce EmptyNode and Nodes 2024-09-27 07:42:41 +02:00
Fabien Potencier 2ae0c0d38c Fix CS 2024-09-06 12:33:38 +02:00
Alexandre Daubois 797e490356 Improve exception expectations reliability 2024-09-04 15:22:48 +02:00
Alexander M. Turek f555a33caf Migrate NodeTestCase to static data providers 2024-09-03 13:51:42 +02:00
Fabien Potencier 31037d0e51 Refactor code 2024-08-20 15:39:54 +02:00
Fabien Potencier 0823d23488 Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments 2024-08-15 23:00:02 +02:00
Fabien Potencier e1705f8831 Extract a new CallableArgumentsExtractor class 2024-08-15 20:37:59 +02:00
Fabien Potencier 3156d8093e Move FunctionExpression/FilterExpression/TestExpression attributes from compilation time to parsing time 2024-08-14 18:45:35 +02:00
Fabien Potencier 8a8f22131c Simplify tests 2024-08-10 18:25:26 +02:00
Fabien Potencier f5e10e10f1 Fix CS 2024-08-07 19:34:09 +02:00
Fabien Potencier 6e55b5f956 Simplify usage of RawFilter 2024-07-28 11:10:04 +02:00
Fabien Potencier ebcd5034ee Refactor some tests 2024-07-24 08:44:11 +02:00
Fabien Potencier a656293915 minor #4150 Replace template loader mocks by ArrayLoader (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Replace template loader mocks by ArrayLoader

Commits
-------

e4e95f5b Replace template loader mocks by ArrayLoader
2024-07-24 08:31:51 +02:00
Fabien Potencier e4e95f5b86 Replace template loader mocks by ArrayLoader 2024-07-24 08:27:23 +02:00
Fabien Potencier d2eab12e70 Add support for first class callables 2024-07-14 13:56:14 +02:00
Fabien Potencier b63bde3063 Optimize sprintf() calls for PHP 8.4 2024-06-21 08:16:55 +02:00
Fabien Potencier 283475b102 Move some static extension methods to non-static 2024-05-01 13:48:35 +02:00
Fabien Potencier c63f695e8a Add needs_charset option for filters and functions 2024-05-01 13:12:48 +02:00
Fabien Potencier 16acdf69fe Rename some internal methods 2024-05-01 10:19:22 +02:00
Fabien Potencier 5148d10515 Fix some inconsistencies 2024-04-28 13:15:26 +02:00
Fabien Potencier 2b2ac80d34 Fxi some errors reported by phpstan 2024-02-05 17:48:36 +01:00
Fabien Potencier 54d34b969b Move functions for CoreExtension 2023-12-10 20:13:10 +01:00
Ryan Weaver ba4fe3ba34 Adding support for the ...spread operator on arrays and hashes 2023-07-20 16:08:57 +02:00
Fabien Potencier f2cb718bdb Fix CS 2022-08-12 08:33:05 +02:00
Ben Thomson d1457a40b6 Allow inherited magic method to still run with calling class
If a static method cannot be resolved to the calling class, but the calling class has, or inherits, a `__callStatic` handler, this allows the `__callStatic` handler to be used with the calling class, and not the inherited class as would occur with reflection. This allows systems such as Laravel facades to still work.

Fixes https://github.com/twigphp/Twig/issues/3716
2022-07-06 10:46:46 +02:00
Fabien Potencier 3aba62185d Removed unneeded ext refs 2022-06-02 20:36:38 +02:00
ju1ius e333ccc9f6 Fixes CallExpression::reflectCallable() throwing TypeError 2022-06-02 20:33:33 +02:00
Nicolas Grekas dd34e9f5c9 Fix optimizing non-public named closures 2022-05-16 17:47:23 +02:00