8000 minor #30953 [Form] Refactor DefaultChoiceListFactory (vudaltsov) · symfony/symfony@76906ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 76906ab

Browse files
committed
minor #30953 [Form] Refactor DefaultChoiceListFactory (vudaltsov)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Form] Refactor DefaultChoiceListFactory | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a A small optimization (remove unnecessary `foreach` calls) and a naming improvement. ping @HeahDude , @stof #EUFOSSA Commits ------- 77424c8 Refactor DefaultChoiceListFactory
2 parents 2b01eaa + 77424c8 commit 76906ab

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
7070
// If the callable returns null, the choice is not added to any group
7171
if (\is_callable($groupBy)) {
7272
foreach ($choices as $value => $choice) {
73-
self::addChoiceViewGroupedBy(
73+
self::addChoiceViewsGroupedByCallable(
7474
$groupBy,
7575
$choice,
7676
(string) $value,
@@ -83,9 +83,23 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
8383
$otherViews
8484
);
8585
}
86+
87+
// Remove empty group views that may have been created by
88+
// addChoiceViewsGroupedByCallable()
89+
foreach ($preferredViews as $key => $view) {
90+
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
91+
unset($preferredViews[$key]);
92+
}
93+
}
94+
95+
foreach ($otherViews as $key => $view) {
96+
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
97+
unset($otherViews[$key]);
98+
}
99+
}
86100
} else {
87101
// Otherwise use the original structure of the choices
88-
self::addChoiceViewsGroupedBy(
102+
self::addChoiceViewsFromStructuredValues(
89103
$list->getStructuredValues(),
90104
$label,
91105
$choices,
@@ -98,20 +112,6 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
98112
);
99113
}
100114

101-
// Remove any empty group view that may have been created by
102-
// addChoiceViewGroupedBy()
103-
foreach ($preferredViews as $key => $view) {
104-
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
105-
unset($preferredViews[$key]);
106-
}
107-
}
108-
109-
foreach ($otherViews as $key => $view) {
110-
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
111-
unset($otherViews[$key]);
112-
}
113-
}
114-
115115
return new ChoiceListView($otherViews, $preferredViews);
116116
}
117117

@@ -150,9 +150,9 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
150150
}
151151
}
152152

153-
private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
153+
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
154154
{
155-
foreach ($groupBy as $key => $value) {
155+
foreach ($values as $key => $value) {
156156
if (null === $value) {
157157
continue;
158158
}
@@ -162,7 +162,7 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
162162
$preferredViewsForGroup = [];
163163
$otherViewsForGroup = [];
164164

165-
self::addChoiceViewsGroupedBy(
165+
self::addChoiceViewsFromStructuredValues(
166166
$value,
167167
$label,
168168
$choices,
@@ -200,7 +200,7 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
200200
}
201201
}
202202

203-
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
203+
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
204204
{
205205
$groupLabels = $groupBy($choice, $keys[$value], $value);
206206

0 commit comments

Comments
 (0)
0