Fix wrong array index

This commit is contained in:
Fabien Potencier
2025-02-01 16:15:31 +01:00
parent fff563810a
commit 7f080086dc
2 changed files with 34 additions and 4 deletions
+1 -2
View File
@@ -116,9 +116,8 @@ class ArrayExpression extends AbstractExpression
->subcompile($pair['key'])
->raw(' => ')
;
} else {
++$nextIndex;
}
++$nextIndex;
$compiler->subcompile($pair['value']);
}
+33 -2
View File
@@ -55,11 +55,25 @@ Twig supports array notation
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}
{# indexes are kept #}
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
{{ { 1: "first", 0: "second" } == indices_1 ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == { '1': "first", 'foo': "second", '2': "third" } ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == indices_2 ? 'OK' : 'KO' }}
--DATA--
$objectStorage = new SplObjectStorage();
$object = new stdClass();
$objectStorage[$object] = 'foo';
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b']), 'object_storage' => $objectStorage, 'object' => $object]
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object_storage' => $objectStorage,
'object' => $object,
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--EXPECT--
1,2
foo,bar
@@ -90,9 +104,21 @@ ok
ok
ok
OK
OK
OK
OK
OK
--DATA--
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b']), 'object' => new stdClass()]
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object' => new stdClass(),
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--CONFIG--
return ['strict_variables' => false]
--EXPECT--
@@ -126,3 +152,8 @@ ok
ok
OK
OK
OK
OK
OK