Commit Graph

214 Commits

Author SHA1 Message Date
Fabien Potencier ae4f284043 Fix various phpstan errors 2024-07-30 11:17:22 +02:00
Christian Flothmann 8602b7f3dc simplify an iterable check 2024-07-05 23:09:31 +02:00
Fabien Potencier 2dacaadd98 Tweak code, tests, and docs for shuffle 2024-07-05 09:39:44 +02:00
sarah-eit 17430ae5ac Add shuffle filter 2024-07-05 09:20:37 +02:00
Pierre c5c95a2d5c Issue #3828: Add sequence and mapping tests 2024-06-22 19:48:59 +02:00
Fabien Potencier c44be99a84 Rename array to sequence 2024-06-22 19:37:51 +02:00
Fabien Potencier b63bde3063 Optimize sprintf() calls for PHP 8.4 2024-06-21 08:16:55 +02:00
Fabien Potencier f6ce88df2f Fix constant return type 2024-05-11 20:51:17 +02:00
Fabien Potencier b222be9974 Fix CS 2024-05-01 16:06:53 +02:00
Fabien Potencier 0a275945af Make some CoreExtension methods part of the public API 2024-05-01 14:11:02 +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 82f8864e7b Fix a @return type hint 2024-04-30 15:38:55 +02:00
Fabien Potencier fa714ce6bd Fix CS 2024-04-30 15:30:51 +02:00
Fabien Potencier c2fc437f6a Fix a test 2024-04-30 15:23:35 +02:00
Fabien Potencier a71ae1923e Use ::class everywhere 2024-04-19 10:26:00 -04:00
Simon André 2d262bc35f Fix param name in docblock (minor) 2024-04-03 13:36:36 +02:00
Nicolas Grekas e70777405d Add #[YieldReady] to allow extensions to tell when they're ready for yielding 2024-02-15 10:27:26 +01:00
Fabien Potencier 7f5958c5e3 minor #3996 Remove redundant "instanceof \Countable" check (minor) (smnandre)
This PR was merged into the 3.x branch.

Discussion
----------

Remove redundant "instanceof \Countable" check (minor)

In [CoreExtension::lengthFilter](https://github.com/twigphp/Twig/compare/3.x...smnandre:fix/remove-check-countable?expand=1#diff-29e85e483c6ec4a9c2fd144820b6722c86df60d54175b355d85e806253313c1a), the check L1183 is redundant as "`$thing instanceof \Countable`" is already checked L1175.

```diff
    if ($thing instanceof \Countable || \is_array($thing) || $thing instanceof \SimpleXMLElement) {
        return \count($thing);
    }

    if ($thing instanceof \Traversable) {
        return iterator_count($thing);
    }

-    if (method_exists($thing, '__toString') && !$thing instanceof \Countable) {
+    if (method_exists($thing, '__toString')) {
        return mb_strlen((string) $thing, $env->getCharset());
    }
```

Commits
-------

2836af3d Remove redundant "$thing instanceof \Countable" check
2024-02-10 10:00:39 +01:00
Fabien Potencier f7f11ea410 Fix CS 2024-02-10 09:52:03 +01:00
Simon André 2836af3d3f Remove redundant "$thing instanceof \Countable" check
The check L1183 is redundant as "$thing instanceof \Countable" is already checked L1175.

```php
        if ($thing instanceof \Countable || \is_array($thing) || $thing instanceof \SimpleXMLElement) {
            return \count($thing);
        }

        if ($thing instanceof \Traversable) {
            return iterator_count($thing);
        }

        if (method_exists($thing, '__toString') && !$thing instanceof \Countable) {
            return mb_strlen((string) $thing, $env->getCharset());
        }
```
2024-02-10 02:32:40 +01:00
Fabien Potencier 2b2ac80d34 Fxi some errors reported by phpstan 2024-02-05 17:48:36 +01:00
Fabien Potencier d21666f728 Throws proper Twig exception when using cycle on an empty array 2024-01-11 21:47:04 +01:00
Malte Wunsch 3963851a41 Improve error message
It is not obvious that you cannot use the Twig function `constant` to get the special `::class` constants of classes like so: `{{ constant('Twig\\Extension\\CoreExtension::class') }}`, due to the underlying PHP function `\constant()`.

The previous error message in this case: 'Constant "Twig\Extension\CoreExtension::class" is undefined.' was not very helpful.
2023-12-22 08:51:25 +01:00
Fabien Potencier aa7c454e2d Fix CS 2023-12-10 20:34:32 +01:00
Fabien Potencier 54d34b969b Move functions for CoreExtension 2023-12-10 20:13:10 +01: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
Jérôme Tamarelle 6d715e2002 Remove unused variables and unreachable code 2023-10-08 21:18:06 +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
Ryan Weaver ba4fe3ba34 Adding support for the ...spread operator on arrays and hashes 2023-07-20 16:08:57 +02:00
Richard Henkenjohann b0cabc093c Update signature to acknowledge a TemplateWrapper 2023-07-01 16:42:59 +02:00
Jacob Richardson f136668933 Restores the leniency of the matches twig comparison, allowing null subject to result in a non-match.
Resolves BC break introduced in PR https://github.com/twigphp/Twig/pull/3687

As per pattern in https://github.com/twigphp/Twig/pull/3617
2023-02-07 17:07:16 +00:00
ju1ius 4f2f3f814e pass the current key to reduce filter's callback 2023-01-25 10:42:37 +01:00
Christophe Coevoet 44c46712b8 Fix error messages in sandboxed mode for has some and has every 2023-01-03 18:06:19 +01:00
Fabien Potencier a82e94d911 Add some tests 2022-12-27 12:31:54 +01:00
Matheo Daninos 61672c43f9 throwing syntaxt error when the matches regexp is not valid 2022-12-27 12:25:33 +01:00
Fabien Potencier d769ae90d1 feature #3670 Add "some" and "every" filters (Jean-Beru)
This PR was merged into the 3.x branch.

Discussion
----------

Add "some" and "every" filters

This PR adds 2 new tests inspired from Javascript's functions: [some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) and [every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).

Usage :
```twig
Value {{ ([1, 2, 3] has some v => v == 2) ? 'found' : 'not found' }}

Key {{ ([1, 2, 3] has some (v, k) => k == 2) ? 'found' : 'not found' }}

Every values {{ ([1, 2, 3] has every v => 42 > v) ? 'match' : 'do not match' }}

Every keys {{ ([1, 2, 3] has every (v, k) => 42 > k) ? 'match' : 'do not match' }}
```

Commits
-------

009bb4fd Add "some" and "every" filters
2022-12-26 17:18:29 +01:00
Fabien Potencier fe347bbf1b Merge branch '2.x' into 3.x
* 2.x:
  Fix tests
  Updates CoreExtension::twig_constant to check for definition first to avoid hard crash
2022-12-26 16:12:53 +01:00
Alex Henderson-Roche 56b31224bd Updates CoreExtension::twig_constant to check for definition first to avoid hard crash 2022-12-26 13:36:01 +01:00
hubert.lenoir 009bb4fd82 Add "some" and "every" filters 2022-03-25 11:04:50 +01:00
Kévin Dunglas 0d6bd45844 add support for the class constant on objects 2022-03-25 09:58:48 +01:00
Fabien Potencier 9e5ca747b6 Merge branch '2.x' into 3.x
* 2.x:
  Disallow non closures in `sort` filter when the sanbox mode is enabled
2022-02-04 07:56:23 +01:00
Fabien Potencier 2eb3308055 Disallow non closures in sort filter when the sanbox mode is enabled 2022-02-04 07:52:21 +01:00
Fabien Potencier 5981815757 Merge branch '2.x' into 3.x
* 2.x:
  Rename variables used in map method
  The deprecated null value for the method round has been changed to zero.
2022-01-14 12:47:50 +01:00