Skip to content

Commit 046aff2

Browse files
minor #33132 [Form] Add type declarations to private DefaultChoiceListFactory methods (vudaltsov)
This PR was merged into the 4.4 branch. Discussion ---------- [Form] Add type declarations to private DefaultChoiceListFactory methods | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a These are the type declarations that can be safely added to private methods of the `DefaultChoiceListFactory` without breaking BC. Commits ------- 9fc6ba6 Add type declarations to private DefaultChoiceListFactory methods
2 parents 3ec9106 + 9fc6ba6 commit 046aff2

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
5353
$choices = $list->getChoices();
5454
$keys = $list->getOriginalKeys();
5555

56-
if (!\is_callable($preferredChoices) && !empty($preferredChoices)) {
57-
// make sure we have keys that reflect order
58-
$preferredChoices = array_values($preferredChoices);
59-
$preferredChoices = static function ($choice) use ($preferredChoices) {
60-
return array_search($choice, $preferredChoices, true);
61-
};
56+
if (!\is_callable($preferredChoices)) {
57+
if (empty($preferredChoices)) {
58+
$preferredChoices = null;
59+
} else {
60+
// make sure we have keys that reflect order
61+
$preferredChoices = array_values($preferredChoices);
62+
$preferredChoices = static function ($choice) use ($preferredChoices) {
63+
return array_search($choice, $preferredChoices, true);
64+
};
65+
}
6266
}
6367

6468
// The names are generated from an incrementing integer by default
@@ -76,7 +80,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
7680
self::addChoiceViewsGroupedByCallable(
7781
$groupBy,
7882
$choice,
79-
(string) $value,
83+
$value,
8084
$label,
8185
$keys,
8286
$index,
@@ -126,7 +130,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
126130
return new ChoiceListView($otherViews, $preferredViews);
127131
}
128132

129-
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
133+
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
130134
{
131135
// $value may be an integer or a string, since it's stored in the array
132136
// keys. We want to guarantee it's a string though.
@@ -154,15 +158,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
154158
);
155159

156160
// $isPreferred may be null if no choices are preferred
157-
if ($isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) {
161+
if (null !== $isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) {
158162
$preferredViews[$nextIndex] = $view;
159163
$preferredViewsOrder[$nextIndex] = $preferredKey;
160164
}
161165

162166
$otherViews[$nextIndex] = $view;
163167
}
164168

165-
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
169+
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
166170
{
167171
foreach ($values as $key => $value) {
168172
if (null === $value) {
@@ -214,7 +218,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
214218
}
215219
}
216220

217-
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
221+
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
218222
{
219223
$groupLabels = $groupBy($choice, $keys[$value], $value);
220224

0 commit comments

Comments
 (0)