Skip to content

Fix wrong array index (again) #4619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
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
@@ -1,5 +1,6 @@
# 3.21.0 (2025-XX-XX)

* Fix wrong array index
* Deprecate `Template::loadTemplate()`
* Fix testing and expression when it evaluates to an instance of `Markup`
* Add `ReturnPrimitiveTypeInterface` (and sub-interfaces for number, boolean, string, and array)
Expand Down
21 changes: 10 additions & 11 deletions src/Node/Expression/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,32 @@ public function compile(Compiler $compiler): void
}

$compiler->raw('[');
$first = true;
$nextIndex = 0;
foreach ($this->getKeyValuePairs() as $pair) {
if (!$first) {
$isSequence = true;
foreach ($this->getKeyValuePairs() as $i => $pair) {
if (0 !== $i) {
$compiler->raw(', ');
}
$first = false;

$key = null;
if ($pair['key'] instanceof ContextVariable) {
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
}
if ($pair['key'] instanceof TempNameExpression) {
} elseif ($pair['key'] instanceof TempNameExpression) {
$key = $pair['key']->getAttribute('name');
$pair['key'] = new ConstantExpression($key, $pair['key']->getTemplateLine());
}
if ($pair['key'] instanceof ConstantExpression) {
} elseif ($pair['key'] instanceof ConstantExpression) {
$key = $pair['key']->getAttribute('value');
}

if ($nextIndex !== $key && !$pair['value'] instanceof SpreadUnary) {
if ($key !== $i) {
$isSequence = false;
}

if (!$isSequence && !$pair['value'] instanceof SpreadUnary) {
$compiler
->subcompile($pair['key'])
->raw(' => ')
;
}
++$nextIndex;

$compiler->subcompile($pair['value']);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/expressions/array.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ 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' }}
{% set trad = {11: 'ABC', 2: 'DEF', 4: 'GHI', 3: 'JKL'} %}
{% set trad2 = {'11': 'ABC', '2': 'DEF', '4': 'GHI', '3': 'JKL'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}

{# indexes are kept #}
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
Expand Down Expand Up @@ -104,6 +107,7 @@ ok
ok
ok

OK
OK

OK
Expand Down Expand Up @@ -151,6 +155,7 @@ ok
ok
ok

OK
OK

OK
Expand Down