Skip to content

Commit 3c7c97e

Browse files
committed
bug #4619 Fix wrong array index (again) (fabpot)
This PR was merged into the 3.x branch. Discussion ---------- Fix wrong array index (again) Commits ------- 09f9a22 Fix wrong array index (again)
2 parents 77a6da9 + 09f9a22 commit 3c7c97e

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

Diff for: CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 3.21.0 (2025-XX-XX)
22

3+
* Fix wrong array index
34
* Deprecate `Template::loadTemplate()`
45
* Fix testing and expression when it evaluates to an instance of `Markup`
56
* Add `ReturnPrimitiveTypeInterface` (and sub-interfaces for number, boolean, string, and array)

Diff for: src/Node/Expression/ArrayExpression.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,32 @@ public function compile(Compiler $compiler): void
7878
}
7979

8080
$compiler->raw('[');
81-
$first = true;
82-
$nextIndex = 0;
83-
foreach ($this->getKeyValuePairs() as $pair) {
84-
if (!$first) {
81+
$isSequence = true;
82+
foreach ($this->getKeyValuePairs() as $i => $pair) {
83+
if (0 !== $i) {
8584
$compiler->raw(', ');
8685
}
87-
$first = false;
8886

8987
$key = null;
9088
if ($pair['key'] instanceof ContextVariable) {
9189
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
92-
}
93-
if ($pair['key'] instanceof TempNameExpression) {
90+
} elseif ($pair['key'] instanceof TempNameExpression) {
9491
$key = $pair['key']->getAttribute('name');
9592
$pair['key'] = new ConstantExpression($key, $pair['key']->getTemplateLine());
96-
}
97-
if ($pair['key'] instanceof ConstantExpression) {
93+
} elseif ($pair['key'] instanceof ConstantExpression) {
9894
$key = $pair['key']->getAttribute('value');
9995
}
10096

101-
if ($nextIndex !== $key && !$pair['value'] instanceof SpreadUnary) {
97+
if ($key !== $i) {
98+
$isSequence = false;
99+
}
100+
101+
if (!$isSequence && !$pair['value'] instanceof SpreadUnary) {
102102
$compiler
103103
->subcompile($pair['key'])
104104
->raw(' => ')
105105
;
106106
}
107-
++$nextIndex;
108107

109108
$compiler->subcompile($pair['value']);
110109
}

Diff for: tests/Fixtures/expressions/array.test

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Twig supports array notation
5555
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
5656
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
5757
{{ trad == trad2 ? 'OK' : 'KO' }}
58+
{% set trad = {11: 'ABC', 2: 'DEF', 4: 'GHI', 3: 'JKL'} %}
59+
{% set trad2 = {'11': 'ABC', '2': 'DEF', '4': 'GHI', '3': 'JKL'} %}
60+
{{ trad == trad2 ? 'OK' : 'KO' }}
5861

5962
{# indexes are kept #}
6063
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
@@ -104,6 +107,7 @@ ok
104107
ok
105108
ok
106109

110+
OK
107111
OK
108112

109113
OK
@@ -151,6 +155,7 @@ ok
151155
ok
152156
ok
153157

158+
OK
154159
OK
155160

156161
OK

0 commit comments

Comments
 (0)