Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 24 additions & 7 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 '';
}
Expand All @@ -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)];
}

/**
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\is_string($value) is already optimized by PHP, I'm not sure this is worth keeping the result in a variable.


foreach ($compare as $item) {
if (0 === self::compare($value, $item)) {
if ($valueIsString && \is_string($item) ? $value == $item : 0 === self::compare($value, $item)) {
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Extension/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand All @@ -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)],
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixtures/filters/first.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{{ 'Ä€é'|first }}
*{{ ''|first }}*
*{{ null|first }}*
*{{ []|first }}*
Comment thread
Kocal marked this conversation as resolved.
Resolved
*{{ [0, 1]|first }}*
*{{ {a: 0, b: 1}|first }}*
--DATA--
return ['arr' => new \ArrayObject([1, 2, 3, 4])]
--EXPECT--
Expand All @@ -18,3 +21,6 @@ return ['arr' => new \ArrayObject([1, 2, 3, 4])]
Ä
**
**
**
*0*
*0*
6 changes: 6 additions & 0 deletions tests/Fixtures/filters/last.test
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -18,3 +21,6 @@ return ['arr' => new \ArrayObject([1, 2, 3, 4])]
é
**
**
**
*0*
*0*
20 changes: 20 additions & 0 deletions tests/Fixtures/tests/in.test
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand Down Expand Up @@ -126,3 +136,13 @@ OK
OK
OK
OK

OK
OK
OK
OK
OK
OK
OK
OK
OK
Loading