Skip to content

Commit

Permalink
fix: prevent type error on choices array with optional keys
Browse files Browse the repository at this point in the history
  • Loading branch information
qkdreyer committed Feb 26, 2024
1 parent ad89a56 commit 23705e5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
4 changes: 4 additions & 0 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
4 changes: 2 additions & 2 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 23705e5

Please sign in to comment.