From 23705e50735ef4ac6ccbe6eddca956ffdb50c616 Mon Sep 17 00:00:00 2001 From: qkdreyer Date: Mon, 26 Feb 2024 14:14:15 +0100 Subject: [PATCH] fix: prevent type error on choices array with optional keys --- Tests/Translation/Extractor/File/Fixture/MyFormType.php | 2 ++ Tests/Translation/Extractor/File/FormExtractorTest.php | 4 ++++ Translation/Extractor/File/FormExtractor.php | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/Translation/Extractor/File/Fixture/MyFormType.php b/Tests/Translation/Extractor/File/Fixture/MyFormType.php index 578de21f..67d40c3c 100644 --- a/Tests/Translation/Extractor/File/Fixture/MyFormType.php +++ b/Tests/Translation/Extractor/File/Fixture/MyFormType.php @@ -67,4 +67,6 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'empty_value' => ['year' => 'form.dueDate.empty.year', 'month' => 'form.dueDate.empty.month', 'day' => 'form.dueDate.empty.day'], ]); } + + const CHOICES = ['choices' => [null]]; } diff --git a/Tests/Translation/Extractor/File/FormExtractorTest.php b/Tests/Translation/Extractor/File/FormExtractorTest.php index 0f900f25..c4ebd69f 100644 --- a/Tests/Translation/Extractor/File/FormExtractorTest.php +++ b/Tests/Translation/Extractor/File/FormExtractorTest.php @@ -148,6 +148,10 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 67)); $expected->add($message); + $message = new Message(0); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, -1)); + $expected->add($message); + $this->assertEquals($expected, $this->extract('MyFormType.php')); } diff --git a/Translation/Extractor/File/FormExtractor.php b/Translation/Extractor/File/FormExtractor.php index b88ac436..e6caa1ec 100644 --- a/Translation/Extractor/File/FormExtractor.php +++ b/Translation/Extractor/File/FormExtractor.php @@ -241,10 +241,10 @@ protected function parseChoiceNode(Node $item, Node $node, $domain) return true; } - foreach ($item->value->items as $subItem) { + foreach ($item->value->items as $index => $subItem) { $newItem = clone $subItem; $newItem->key = $subItem->value; - $newItem->value = $subItem->key; + $newItem->value = $subItem->key ?? new Node\Scalar\Int_($index); $subItem = $newItem; $this->parseItem($subItem, $domain); }