This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.
Discussion
----------
Added token type and value to error message
Added the given token type and value to the error message in case that the token after '.' is neither a name nor a number.
I think it helps a lot to have more expressive verbose error messages (specially in this case with the dot `.` operator, since it's the concatenation operator in PHP and developers may mistakenly use it instead of tilde `~` operator which is the concatenation operator in Twig.
I also think that it's better to extract the error message generating script out of `TokenStream::expect` (the code I included at the end of this message) and make it a new method with more general flexibility to be able to use the same method with all these scenarios but I'm not sure if this level of abstraction meets the Twig source policy so I didn't try to do that.
```
sprintf('%sUnexpected token "%s"%s ("%s" expected%s).',
$message ? $message.'. ' : '',
Token::typeToEnglish($token->getType()),
$token->getValue() ? sprintf(' of value "%s"', $token->getValue()) : '',
Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : '')
```
Commits
-------
6c8da65b Added token type and value to error message
This PR was merged into the 2.x branch.
Discussion
----------
Fix custom escapers when using multiple Twig environments
Closes#3668
Commits
-------
9c03ef0d Fix custom escapers when using multiple Twig environments
This PR was merged into the 2.x branch.
Discussion
----------
Rename variable in SecurityPolicyInterface.php
Hi @fabpot
When using psalm, an error is reported when the name of the variable is not the same than the one in the interface.
This also could give issues with named params.
The securityPolicy implementation is using `property` as the name of the variable, which make a lot more sens https://github.com/twigphp/Twig/blob/3.x/src/Sandbox/SecurityPolicy.php#L110.
So I updated the interface. But I wasn't sure on which branch (1.x, 2.x, 3.x) I should have opened the PR.
I also wondering if you want me to rename $obj to $object maybe ?
Commits
-------
3f07c38c Rename variable
This PR was merged into the 2.x branch.
Discussion
----------
do not reuse internally generated variable names during parsing
fixes#3647
Commits
-------
1411c454 do not reuse internally generated variable names during parsing
This PR was merged into the 2.x branch.
Discussion
----------
Disallow non closures in `sort` filter when the sanbox mode is enabled
Commits
-------
2eb33080 Disallow non closures in `sort` filter when the sanbox mode is enabled
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.
Discussion
----------
Fix call to deprecated "convertToHtml" method
note: We can not rely on the new interface `ConverterInterface` because this interface already existed in [version 1.0](https://github.com/thephpleague/commonmark/blob/1.0/src/ConverterInterface.php)
Commits
-------
779fdd09 Fix call to deprecated "convertToHtml" method
This PR was merged into the 2.x branch.
Discussion
----------
Fix map example output
@fabpot sorry little error in my #3628 pull request 🙈
Commits
-------
97411730 Fix map example output
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.
Discussion
----------
Rename variables used in map method
As most people (including me) are lazy people to read text and mostly looks just at the code. Using `key` and `value` as variable make the map faster to understand which argument is which one :)
Commits
-------
b74cf2ae Rename variables used in map method
This PR was submitted for the 3.x branch but it was squashed and merged into the 2.x branch instead.
Discussion
----------
The deprecated null value for the method round has been changed to zero.
Commits
-------
7d0e5d6a The deprecated null value for the method round has been changed to zero.
This PR was merged into the 2.x branch.
Discussion
----------
Allow null for `nl2br`
Same as https://github.com/twigphp/Twig/pull/3617
Fixes `Passing null to parameter #1 ($string) of type string is deprecated` on PHP 8.1
Commits
-------
e9cd55de Allow null for nl2br, striptags and format filters
This PR was merged into the 2.x branch.
Discussion
----------
Re-add compat with PHP 7.1
Closes#3613
Commits
-------
7389f48a Add compat with PHP 7.1 back
This PR was merged into the 2.x branch.
Discussion
----------
Allow null when Twig expects a string
To ease the transition to PHP 8.1, Twig now explicitly accepts `null` in addition to strings in filters that expect strings.
Closes#3615Closes#3557Closes#3610Closes#3609
Commits
-------
92bc110b Allow null when Twig expects a string
This PR was merged into the 2.x branch.
Discussion
----------
Allow symfony/translation-contracts:^3
However,
I am just wondering why this dependency is required since it's not used anywhere in `twig/string-extra`? Or I am wrong ?
Closing https://github.com/twigphp/Twig/issues/3607
Commits
-------
0c1e4edc allow translation-contracts:^3
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
Optimize Environment::resolveTemplate() to be much faster when template overrides do not exist
Fixes partially #3595.
You can test this by adding an object into the context which implements `__debugInfo()` with a counter in it. Make a template with
```twig
{% include ['missing_template1', 'missing_template2', 'missing_template3', 'missing_template4', 'missing_template5'] %}
```
And check how many times an exception was thrown by reading the counter. The issue gets exponentially worse if you have nested templates with overrides in each of them.
Commits
-------
695423e1 Optimize Environment::resolveTemplate() to be much faster when template overrides do not exist
This PR was merged into the 2.x branch.
Discussion
----------
Use faster hash algorithm (xxh128) on PHP 8.1+
Twig uses the `hash` function at runtime to convert template paths class names, each time a template is loaded. In a large project with a high granularity of templates (hundreds of `include` per page), this can become the most time consuming function as reported by Blackfire:
<img src="https://user-images.githubusercontent.com/400034/140626949-c4876b7d-c91c-4e07-b2f4-8ebbcaa72f61.png" width="220">
To optimise this use-case, [PHP 8.1 supports the xxHash hash algorithms](https://php.watch/versions/8.1/xxHash). `xxh128` is 60x faster that `sha256` according to the [benchmarks](https://php.watch/articles/php-hash-benchmark), and guarantee a very [low risk of collision](http://cyan4973.github.io/xxHash/).
I'm not able to test with PHP 8.1 for now. Before going further I would like to validate performance impact on a large list of file names.
Commits
-------
4fcc6b1c Use faster hash algorithm (xxh128) on PHP 8.1
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
Hashing is not necessary to generate unique variable names during compilation
Trying to optimize calls to `hash` function (#3588), I found that some calls are made during compilation that are not necessary at all.
According to the last commits on this functions (57ff88255e & 6ab5fe9b8f), the internal variable names must be unique and deterministic. Using a simple sequence is enough.
This avoid CPU cycles during compilation. Which should not have any impact on production; but still interesting for dev&test.
Commits
-------
ba2b4e6d Hashing is not necessary to generate unique variable names during compilation