From 3c936f447b2b618d2018d274de6a9bcd057b64bc Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 1 Jan 2019 14:11:42 +0100 Subject: [PATCH] [VarExporter] fix exporting array indexes --- src/Symfony/Component/VarExporter/Internal/Exporter.php | 2 +- .../Tests/Fixtures/partially-indexed-array.php | 8 ++++++++ .../Component/VarExporter/Tests/VarExporterTest.php | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/partially-indexed-array.php diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php index bbb409e194..38a8e25088 100644 --- a/src/Symfony/Component/VarExporter/Internal/Exporter.php +++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php @@ -232,7 +232,7 @@ class Exporter if (!\is_int($k) || 1 !== $k - $j) { $code .= self::export($k, $subIndent).' => '; } - if (\is_int($k)) { + if (\is_int($k) && $k > $j) { $j = $k; } $code .= self::export($v, $subIndent).",\n"; diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/partially-indexed-array.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/partially-indexed-array.php new file mode 100644 index 0000000000..647aa7e80f --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/partially-indexed-array.php @@ -0,0 +1,8 @@ + true, + 1 => true, + 2 => true, + true, +]; diff --git a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php index 2d6c32a52e..c34a7e4fb7 100644 --- a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php +++ b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php @@ -112,6 +112,7 @@ class VarExporterTest extends TestCase yield array('bool', true, true); yield array('simple-array', array(123, array('abc')), true); + yield array('partially-indexed-array', array(5 => true, 1 => true, 2 => true, 6 => true), true); yield array('datetime', \DateTime::createFromFormat('U', 0)); $value = new \ArrayObject();