Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PHP 8.0 functions with polyfill #3884

Merged
merged 1 commit into from Oct 20, 2023
Merged

Conversation

GromNaN
Copy link
Contributor

@GromNaN GromNaN commented Oct 8, 2023

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)

@fabpot
Copy link
Contributor

fabpot commented Oct 20, 2023

Thank you @GromNaN.

@fabpot fabpot merged commit f1b5227 into twigphp:3.x Oct 20, 2023
57 of 58 checks passed
@GromNaN GromNaN deleted the php80-polyfill branch October 20, 2023 15:50
fabpot added a commit that referenced this pull request Oct 22, 2023
…tr_ends_with` (GromNaN)

This PR was merged into the 3.x branch.

Discussion
----------

Compile `starts/ends with` using `str_starts_with` and `str_ends_with`

Since we now require PHP 8.0 polyfill #3884, we can use `str_starts_with` and `str_ends_with` to compile `starts with` and `ends with` expressions.

Example with `bootstrap_4_layout.html.twig` [line 6-7](https://github.com/symfony/symfony/blob/e6d1ed4edb5ae197ec7d25ddaf64cfa456229504/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L6-L7) (see deep [diff](https://www.diffchecker.com/18CtPeja/)):

```twig
    {%- set prepend = not (money_pattern starts with '{{') -%}
    {%- set append = not (money_pattern ends with '}}') -%}
```

Previous compilation:
```php
// line 6
$context["prepend"] =  !(is_string($__internal_compile_0 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 6, $this->source); })())) && is_string($__internal_compile_1 = "{{") && ('' === $__internal_compile_1 || 0 === strpos($__internal_compile_0, $__internal_compile_1)));
// line 7
$context["append"] =  !(is_string($__internal_compile_2 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 7, $this->source); })())) && is_string($__internal_compile_3 = "}}") && ('' === $__internal_compile_3 || $__internal_compile_3 === substr($__internal_compile_2, -strlen($__internal_compile_3))));
```

With this change:
```php
// line 6
$context["prepend"] =  !(is_string($__internal_compile_0 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 6, $this->source); })())) && is_string($__internal_compile_1 = "{{") && str_starts_with($__internal_compile_0, $__internal_compile_1));
// line 7
$context["append"] =  !(is_string($__internal_compile_2 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 7, $this->source); })())) && is_string($__internal_compile_3 = "}}") && str_ends_with($__internal_compile_2, $__internal_compile_3));
```

Commits
-------

30b5a56 Compile starts/ends with using PHP8 functions str_starts/ends_with
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants