Skip to content

Commit

Permalink
bug #4567 Fix wrong array index (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Fix wrong array index

Commits
-------

7f08008 Fix wrong array index
  • Loading branch information
fabpot committed Feb 1, 2025
2 parents fff5638 + 7f08008 commit 3f9974e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/Node/Expression/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public function compile(Compiler $compiler): void
->subcompile($pair['key'])
->raw(' => ')
;
} else {
++$nextIndex;
}
++$nextIndex;

$compiler->subcompile($pair['value']);
}
Expand Down
35 changes: 33 additions & 2 deletions tests/Fixtures/expressions/array.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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--
Expand Down Expand Up @@ -126,3 +152,8 @@ ok
ok

OK

OK
OK
OK
OK

0 comments on commit 3f9974e

Please sign in to comment.