From 7f080086dc65f3dcb44fc0bfb4cf3152ac890ea0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 1 Feb 2025 16:15:31 +0100 Subject: [PATCH] Fix wrong array index --- src/Node/Expression/ArrayExpression.php | 3 +-- tests/Fixtures/expressions/array.test | 35 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Node/Expression/ArrayExpression.php b/src/Node/Expression/ArrayExpression.php index 20c4c7bc555..61a5063f384 100644 --- a/src/Node/Expression/ArrayExpression.php +++ b/src/Node/Expression/ArrayExpression.php @@ -116,9 +116,8 @@ public function compile(Compiler $compiler): void ->subcompile($pair['key']) ->raw(' => ') ; - } else { - ++$nextIndex; } + ++$nextIndex; $compiler->subcompile($pair['value']); } diff --git a/tests/Fixtures/expressions/array.test b/tests/Fixtures/expressions/array.test index eb7f400d96e..ac1c8ca0e36 100644 --- a/tests/Fixtures/expressions/array.test +++ b/tests/Fixtures/expressions/array.test @@ -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