diff --git a/CHANGELOG b/CHANGELOG index a3653427c97..2b504763edd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -21,6 +21,7 @@ * Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only * Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension` * Make `Environment::getRuntime()` fall back to the registered extension of the same class + * Speed up `in`, `first`, `last`, and `join` on arrays # 3.30.0 (2026-09-25) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 410597bbb1e..81b9693f479 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -793,6 +793,14 @@ public static function slice(string $charset, $item, $start, $length = null, $pr */ public static function first(string $charset, $item) { + if ([] === $item) { + return false; + } + + if (\is_array($item)) { + return \PHP_VERSION_ID >= 80500 ? array_first($item) : $item[array_key_first($item)]; + } + $elements = self::slice($charset, $item, 0, 1, false); return \is_string($elements) ? $elements : current($elements); @@ -809,6 +817,14 @@ public static function first(string $charset, $item) */ public static function last(string $charset, $item) { + if ([] === $item) { + return false; + } + + if (\is_array($item)) { + return \PHP_VERSION_ID >= 80500 ? array_last($item) : $item[array_key_last($item)]; + } + $elements = self::slice($charset, $item, -1, 1, false); return \is_string($elements) ? $elements : current($elements); @@ -836,12 +852,10 @@ public static function last(string $charset, $item) */ public static function join($value, $glue = '', $and = null): string { - if (!is_iterable($value)) { - $value = (array) $value; + if (!\is_array($value)) { + $value = is_iterable($value) ? self::toArray($value, false) : (array) $value; } - $value = self::toArray($value, false); - if (0 === \count($value)) { return ''; } @@ -851,10 +865,10 @@ public static function join($value, $glue = '', $and = null): string } if (1 === \count($value)) { - return $value[0]; + return $value[array_key_first($value)]; } - return implode($glue, \array_slice($value, 0, -1)).$and.$value[\count($value) - 1]; + return implode($glue, \array_slice($value, 0, -1)).$and.$value[array_key_last($value)]; } /** @@ -1102,8 +1116,11 @@ public static function inFilter($value, $compare) return \in_array($value, $compare, true); } + // two strings always end up on the "<=>" fallback of compare(), where "0 === <=>" is "==" + $valueIsString = \is_string($value); + foreach ($compare as $item) { - if (0 === self::compare($value, $item)) { + if ($valueIsString && \is_string($item) ? $value == $item : 0 === self::compare($value, $item)) { return true; } } diff --git a/tests/Extension/CoreTest.php b/tests/Extension/CoreTest.php index 2982858d1fd..31916cb17d2 100644 --- a/tests/Extension/CoreTest.php +++ b/tests/Extension/CoreTest.php @@ -197,6 +197,8 @@ public static function provideTwigFirstCases() return [ ['a', 'abc'], [1, [1, 2, 3]], + [false, []], + [0, [0, 1]], ['', null], ['', ''], ['a', new CoreTestIterator($i, array_keys($i), true, 3)], @@ -219,6 +221,8 @@ public static function provideTwigLastCases() return [ ['c', 'abc'], [3, [1, 2, 3]], + [false, []], + [0, [1, 0]], ['', null], ['', ''], ['c', new CoreTestIterator($i, array_keys($i), true)], diff --git a/tests/Fixtures/filters/first.test b/tests/Fixtures/filters/first.test index 6ffc1891116..c090d45a23e 100644 --- a/tests/Fixtures/filters/first.test +++ b/tests/Fixtures/filters/first.test @@ -8,6 +8,9 @@ {{ 'Ä€é'|first }} *{{ ''|first }}* *{{ null|first }}* +*{{ []|first }}* +*{{ [0, 1]|first }}* +*{{ {a: 0, b: 1}|first }}* --DATA-- return ['arr' => new \ArrayObject([1, 2, 3, 4])] --EXPECT-- @@ -18,3 +21,6 @@ return ['arr' => new \ArrayObject([1, 2, 3, 4])] Ä ** ** +** +*0* +*0* diff --git a/tests/Fixtures/filters/last.test b/tests/Fixtures/filters/last.test index 2fdc63b3346..1283e5e6d41 100644 --- a/tests/Fixtures/filters/last.test +++ b/tests/Fixtures/filters/last.test @@ -8,6 +8,9 @@ {{ 'Ä€é'|last }} *{{ ''|last }}* *{{ null|last }}* +*{{ []|last }}* +*{{ [1, 0]|last }}* +*{{ {a: 1, b: 0}|last }}* --DATA-- return ['arr' => new \ArrayObject([1, 2, 3, 4])] --EXPECT-- @@ -18,3 +21,6 @@ return ['arr' => new \ArrayObject([1, 2, 3, 4])] é ** ** +** +*0* +*0* diff --git a/tests/Fixtures/tests/in.test b/tests/Fixtures/tests/in.test index d4fcd5e8f9e..14a09a0cb19 100644 --- a/tests/Fixtures/tests/in.test +++ b/tests/Fixtures/tests/in.test @@ -62,6 +62,16 @@ Twig supports the in operator {{ foo.not not in ['not'] ? 'KO' : 'OK' }} {{ 'value'|not not in ['not value'] ? 'KO' : 'OK' }} {{ 'value'not in['not value'] ? 'OK' : 'KO' }} + +{{ '1' in ['1.0'] ? 'OK' : 'KO' }} +{{ '1.0' in ['1'] ? 'OK' : 'KO' }} +{{ '1e2' in ['100'] ? 'OK' : 'KO' }} +{{ ' 1' in ['1'] ? 'OK' : 'KO' }} +{{ 'abc' in ['ABC'] ? 'KO' : 'OK' }} +{{ '0' in ['false'] ? 'KO' : 'OK' }} +{{ '1' in [1] ? 'OK' : 'KO' }} +{{ '1' in [true] ? 'OK' : 'KO' }} +{{ '' in [null] ? 'OK' : 'KO' }} --DATA-- return ['bar' => 'bar', 'foo' => ['bar' => 'bar', 'not' => 'not'], 'dir_object' => new \SplFileInfo(__DIR__), 'object' => new \stdClass(), 'resource' => opendir(__DIR__), 'safe' => new \Twig\Markup('foo', 'UTF-8')] --EXPECT-- @@ -126,3 +136,13 @@ OK OK OK OK + +OK +OK +OK +OK +OK +OK +OK +OK +OK