Commit Graph

854 Commits

Author SHA1 Message Date
Fabien Potencier a42eba63c5 Use match instead of switch 2023-12-09 15:33:38 +01:00
Fabien Potencier fed8dd102b Use ::class 2023-12-09 15:33:38 +01:00
Fabien Potencier b9cf29d53d Remove obsolete code 2023-12-09 14:56:21 +01:00
Fabien Potencier 583d7b2d2c Remove hack to inline throwing an exception 2023-12-09 14:52:04 +01:00
Fabien Potencier 3d5ad9ac51 Remove obsolete code 2023-12-09 14:50:25 +01:00
Fabien Potencier 3472ae1241 Simplify code 2023-12-09 14:50:13 +01:00
Fabien Potencier f930a284fe Bump to 4.0 and require PHP 8.2 2023-12-09 13:45:50 +01:00
Fabien Potencier aeeec9a5e9 Bump version 2023-11-21 19:55:42 +01:00
Fabien Potencier 9d15f0ac07 Prepare the 3.8.0 release 2023-11-21 19:54:41 +01:00
Fabien Potencier 4be326aeeb Fix tests and CS 2023-11-21 14:57:52 +01:00
Fabien Potencier b285fccd59 bug #3830 Catch errors thrown during template rendering (richards-square)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Catch errors thrown during template rendering

Some errors, like not providing a function the proper number of arguments or division by zero, extend from `\Error` rather than `\Exception`. This PR catches these types of errors during template rendering and throws a `RuntimeError` in order to provide better debugging information.

Commits
-------

85bf01b4 Catch errors thrown during template rendering
2023-10-26 17:55:51 -07:00
Drew Richards 85bf01b4ab Catch errors thrown during template rendering 2023-10-26 17:55:48 -07:00
Fabien Potencier 9a602c7532 feature #3695 Add @codeCoverageIgnore to untestable compiled methods (markhuot)
This PR was merged into the 3.x branch.

Discussion
----------

Add `@codeCoverageIgnore` to untestable compiled methods

I've been experimenting with adding my compiled templates to my code coverage reports and it works largely as expected. Templates that get executed return percentages accurate to the number of lines the tests actually cover. The only exception are the "meta" methods on the compiled template that aren't necessarily called by the tests.

This PR adds ``@codeCoverageIgnore`` comments to the compiled template for all non-display methods so the coverage report only lists lines from `doDisplay`.

I'm assuming that I will need to update some of the Twig tests to account for these new comments, but am curious if you'd be open to this change.

An example screenshot below. The last line shows line 46 out of the compiled template is never executed. The compiled template's `getDebugInfo` even correctly informs me that line 46 maps to line 4 in my `.twig` so I'll work on updating the line numbering next.

![image](https://user-images.githubusercontent.com/48975/166098890-fbaf99ad-9aa4-4430-9f82-050f482a6777.png)

Commits
-------

1cf610bd Add `@codeCoverageIgnore` to untestable compiled methods
2023-10-26 17:52:38 -07:00
Fabien Potencier 248855b34b Merge branch '2.x' into 3.x
* 2.x:
  Fix premature loop exit in Security Policy lookup of allowed methods/properties
2023-10-26 17:38:13 -07:00
Yaakov Saxon 5e1838dbca Fix premature loop exit in Security Policy lookup of allowed methods/properties 2023-10-26 17:37:13 -07:00
Fabien Potencier 415f4cc3ee minor #3896 Compile Elvis operator into Elvis operator ?: (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Compile Elvis operator into Elvis operator `?:`

When using ternary operator without "then" part, the "condition" part is evaluated twice, which is inconsistent with how PHP works.
The Twig template `A ?: B` is currently compiled as `A ? A : B` in PHP. This PR change it to `A ?: B`.

If `A` is a complex expression, it improves performance to only execute the expression once.
If `A` is has a side effect (like updating a variable), the expression being executed twice could result in a bug. ([example in PHP](https://3v4l.org/LWZLR))

Example with ``@WebProfiler`/Collector/form.html.twig` [line 9](https://github.com/symfony/symfony/blob/e6d1ed4edb5ae197ec7d25ddaf64cfa456229504/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig#L9) (see deep [diff](https://www.diffchecker.com/jOm3dE13/))

```twig
{{ collector.data.nb_errors ?: collector.data.forms|length }}
```

Previous compilation:
```php
echo twig_escape_filter($this->env, (((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })())) ? ((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })())) : (twig_get_attribute($this->env, $this->source, (isset($context["collector"]) || array_key_exists("collector", $context) ? $context["collector"] : (function () { throw new RuntimeError('Variable "collector" does not exist.', 9, $this->source); })()), "countDefines", [], "any", false, false, false, 9))), "html", null, true);
```

After this change:
```php
echo twig_escape_filter($this->env, ((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })()) ?: twig_get_attribute($this->env, $this->source, (isset($context["collector"]) || array_key_exists("collector", $context) ? $context["collector"] : (function () { throw new RuntimeError('Variable "collector" does not exist.', 9, $this->source); })()), "countDefines", [], "any", false, false, false, 9)), "html", null, true);
```

Commits
-------

fb0d7495 Compile Elvis operator with Elvis operator
2023-10-22 14:57:13 +02:00
Jérôme Tamarelle 30b5a56625 Compile starts/ends with using PHP8 functions str_starts/ends_with 2023-10-21 19:44:29 +02:00
Jérôme Tamarelle fb0d7495a8 Compile Elvis operator with Elvis operator 2023-10-21 19:08:38 +02:00
Fabien Potencier 51cfad8039 minor #3855 Update signature to acknowledge a TemplateWrapper (richardhj)
This PR was merged into the 3.x branch.

Discussion
----------

Update signature to acknowledge a TemplateWrapper

`$name` legitimately can be of type `Twig\TemplateWrapper`. This is already considered in line 1331. This PR updates the method signature in the PHPDoc.

This PR is necessary because other developers may rely on the signature, see contao/contao#6169.

Commits
-------

b0cabc09 Update signature to acknowledge a TemplateWrapper
2023-10-20 17:52:20 +02:00
Fabien Potencier f1b5227477 minor #3884 Use PHP 8.0 functions with polyfill (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Use PHP 8.0 functions with polyfill

Backport some changes from #3881 into 3.x using `symfony/polyfill-php80`.

- `str_starts_with`
- `str_ends_with`
- `str_contains`
- `get_debug_type`

Benefits:
- Less conflicts when both 3.x and 4.x branches will be maintained.
- Performance gain for PHP 8.0+ ([83,7% of installs](https://packagist.org/packages/twig/twig/php-stats#3))

Commits
-------

1d5c0928 Use PHP 8.0 functions with polyfill
2023-10-20 17:49:50 +02:00
Jérôme Tamarelle 991f518d32 Replace calls to twig_test_iterable to is_iterable 2023-10-20 17:47:18 +02:00
Jérôme Tamarelle 6ca0d773c5 Convert Ternary to Elvis or Null Coalescing 2023-10-20 17:47:17 +02:00
Jérôme Tamarelle cb8a824784 Use is_iterable when possible 2023-10-20 17:46:53 +02:00
Fabien Potencier 78e52452ea minor #3886 Remove TemplateWrapper::render() 2nd parameter not used (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Remove `TemplateWrapper::render()` 2nd parameter not used

The 2nd virtual parameter of `TemplateWrapper::render()` was added by #2805.
`Template::render()` don't define and don't use this 2nd parameter.

Running the test suite, this method never get more than 1 parameter (I assumed it could be used by the compiled Template via the backtrace, but it isn't).

Commits
-------

c56b87b4 Remove TemplateWrapper::render 2nd parameter not used
2023-10-20 17:44:29 +02:00
Fabien Potencier eaf22ba98d Fix CS 2023-10-20 17:39:50 +02:00
Fabien Potencier fec3a7fd22 minor #3888 Remove unused variables and unreachable code (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Remove unused variables and unreachable code

I did some archaeology to find the origin of each useless line of code. Details in comments.

Commits
-------

6d715e20 Remove unused variables and unreachable code
2023-10-20 17:39:04 +02:00
Jérôme Tamarelle 6d715e2002 Remove unused variables and unreachable code 2023-10-08 21:18:06 +02:00
Christian Flothmann 41f1b0370a restore return type annotations 2023-10-08 20:59:56 +02:00
Jérôme Tamarelle c56b87b4d7 Remove TemplateWrapper::render 2nd parameter not used 2023-10-08 19:29:03 +02:00
Jérôme Tamarelle 1d5c09285f Use PHP 8.0 functions with polyfill 2023-10-08 17:11:07 +02:00
Fabien Potencier 5193653ecb Fix CS 2023-10-08 09:05:22 +02:00
Fabien Potencier fd8f61b626 Bump version 2023-08-28 13:10:15 +02:00
Fabien Potencier a0ce373a0c Prepare the 3.7.1 release 2023-08-28 13:09:02 +02:00
Vincent Langlet 1b58589d56 Fix callable phpdoc 2023-07-27 10:17:36 +02:00
Fabien Potencier 9a9580fff9 Bump version 2023-07-26 09:17:41 +02:00
Fabien Potencier 5cf942bbab Prepare the 3.7.0 release 2023-07-26 09:16:09 +02:00
Nicolas Grekas 75efa5e1ba Fix spread operator implementation 2023-07-20 18:11:45 +02:00
Fabien Potencier 09177a7006 feature #3839 Adding support for the ...spread operator on arrays and hashes (weaverryan)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Adding support for the ...spread operator on arrays and hashes

Hi!

Long time user, first time contributor of a real future. I hope I didn't miss anything - but this seemed quite straightforward in the end.

Notes:
* In PHP 7.3 or lower, using the spread operator will result in a Twig syntax error.
* In 8.0 or lower, using the spread operator on a hash will result in a Twig syntax error.

fabbot failures are unrelated, so I'm not touching them.

Cheers!

Commits
-------

ba4fe3ba Adding support for the ...spread operator on arrays and hashes
2023-07-20 16:08:59 +02:00
Ryan Weaver ba4fe3ba34 Adding support for the ...spread operator on arrays and hashes 2023-07-20 16:08:57 +02:00
Vincent Langlet cd68de5e06 Fix callable phpdoc for twig elements 2023-07-05 11:30:59 +02:00
Richard Henkenjohann b0cabc093c Update signature to acknowledge a TemplateWrapper 2023-07-01 16:42:59 +02:00
Fabien Potencier df75bfef37 Bump version to 3.6.2 2023-06-09 16:03:53 +02:00
Fabien Potencier 7e7d5839d4 Prepare the 3.6.1 release 2023-06-08 14:52:13 +02:00
Fabien Potencier 51ccbdc983 Bump version 2023-05-03 21:08:52 +02:00
Fabien Potencier 3fd3645601 Bump version 2023-05-03 21:08:22 +02:00
Fabien Potencier 106c170d08 Prepare the 3.6.0 release 2023-05-03 21:06:57 +02:00
Fabien Potencier fc02a6af3e Prepare the 2.15.5 release 2023-05-03 19:49:41 +02:00
Fabien Potencier e2d668551b Merge branch '2.x' into 3.x
* 2.x:
  Making the Lexer initialize itself lazily, to avoid loading the extension set early
2023-05-03 19:47:35 +02:00
Ryan Weaver 2f7e868017 Making the Lexer initialize itself lazily, to avoid loading the extension set early 2023-05-03 19:47:11 +02:00
Fabien Potencier 08d2037f26 Bump version to 3.6 2023-02-09 07:45:16 +01:00